<commit_before>use std::error;
use std::fmt;

use event::{Event, React};
use render::MetaRenderer;
use super::application::{Application, Execution};
use super::context::{Context, RenderContextView, UpdateContextView};

pub type BoxActivity<T, R> = Box<Activity<T, R>>;

pub enum Transition<T, R>
    where T: React,
          R: MetaRenderer
{
    None,
    Push(BoxActivity<T, R>),
    Pop,
    Abort,
}

impl<T, R> Transition<T, R>
    where T: React,
          R: MetaRenderer
{
    fn to_execution(&self) -> Execution {
        match *self {
            Transition::None | Transition::Push(..) | Transition::Pop => Execution::Continue,
            Transition::Abort => Execution::Abort,
        }
    }
}

pub trait Activity<T, R>: React
    where T: ActivityState<R>,
          R: MetaRenderer
{
    // TODO: What sort of `Result` (if any) should these functions yield?
    fn update(&mut self, context: &mut UpdateContextView<State = T, Window = R::Window>)
              -> Transition<T, R>;
    fn render(&mut self, context: &mut RenderContextView<R, State = T, Window = R::Window>);

    fn pause(&mut self) {}
    fn resume(&mut self) {}
    fn stop(&mut self) {}
}

pub trait ActivityState<R>: React + Sized
    where R: MetaRenderer
{
    // TODO: This seems like a hacky way to get an initial `Activity` onto the
    //       stack. Is there a better way?
    fn start(context: &mut Context<Self, R>) -> BoxActivity<Self, R>;
}

pub struct ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    stack: Vec<BoxActivity<T, R>>,
}

impl<T, R> ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    fn peek_mut(&mut self) -> Option<&mut Activity<T, R>> {
        if let Some(activity) = self.stack.last_mut() { // Cannot use `map`.
            Some(activity.as_mut())
        }
        else {
            None
        }
    }

    fn push(&mut self, activity: BoxActivity<T, R>) {
        if let Some(activity) = self.peek_mut() {
            activity.pause();
        }
        self.stack.push(activity);
    }

    fn pop(&mut self) -> bool {
        self.stack.pop().map(|mut activity| {
            activity.stop();
            if let Some(activity) = self.peek_mut() {
                activity.resume()
            }
        }).is_some()
    }

    fn abort(&mut self) {
        while self.pop() {}
    }
}

impl<T, R> Application<T, R> for ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    type UpdateError = ActivityStackError;
    type RenderError = ActivityStackError;

    fn start(context: &mut Context<T, R>) -> Self {
        ActivityStack {
            stack: vec![T::start(context)],
        }
    }

    fn update<C>(&mut self, context: &mut C) -> Result<Execution, Self::UpdateError>
        where C: UpdateContextView<State = T, Window = R::Window>
    {
        let transition = if let Some(activity) = self.peek_mut() {
            activity.update(context)
        }
        else {
            Transition::Abort
        };
        let execution = transition.to_execution();
        match transition {
            Transition::None => {}
            Transition::Push(activity) => { self.push(activity); }
            Transition::Pop => { self.pop(); }
            Transition::Abort => { self.abort(); }
        }
        Ok(execution)
    }

    fn render<C>(&mut self, context: &mut C) -> Result<(), Self::RenderError>
        where C: RenderContextView<R, State = T, Window = R::Window>
    {
        if let Some(activity) = self.peek_mut() {
            activity.render(context);
        }
        Ok(())
    }

    fn stop(mut self) {
        self.abort();
    }
}

impl<T, R> React for ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    fn react(&mut self, event: &Event) {
        if let Some(activity) = self.peek_mut() {
            activity.react(event);
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum ActivityError {
}

impl fmt::Display for ActivityError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;

        write!(formatter, "{}", self.description())
    }
}

impl error::Error for ActivityError {
    fn description(&self) -> &str {
        ""
    }
}

#[derive(Clone, Copy, Debug)]
pub enum ActivityStackError {
}

impl fmt::Display for ActivityStackError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;

        write!(formatter, "{}", self.description())
    }
}

impl error::Error for ActivityStackError {
    fn description(&self) -> &str {
        ""
    }
}<commit_msg>Yield `Result`s from `Activity`s.<commit_after>use std::convert::From;
use std::error;
use std::fmt;

use event::{Event, React};
use render::MetaRenderer;
use super::application::{Application, Execution};
use super::context::{Context, RenderContextView, UpdateContextView};

pub type BoxActivity<T, R> = Box<Activity<T, R>>;

pub enum Transition<T, R>
    where T: React,
          R: MetaRenderer
{
    None,
    Push(BoxActivity<T, R>),
    Pop,
    Abort,
}

impl<T, R> Transition<T, R>
    where T: React,
          R: MetaRenderer
{
    fn to_execution(&self) -> Execution {
        match *self {
            Transition::None | Transition::Push(..) | Transition::Pop => Execution::Continue,
            Transition::Abort => Execution::Abort,
        }
    }
}

pub trait Activity<T, R>: React
    where T: ActivityState<R>,
          R: MetaRenderer
{
    fn update(&mut self, context: &mut UpdateContextView<State = T, Window = R::Window>)
              -> Result<Transition<T, R>, ActivityError>;
    fn render(&mut self, context: &mut RenderContextView<R, State = T, Window = R::Window>)
              -> Result<(), ActivityError>;

    fn pause(&mut self) {}
    fn resume(&mut self) {}
    fn stop(&mut self) {}
}

pub trait ActivityState<R>: React + Sized
    where R: MetaRenderer
{
    // TODO: This seems like a hacky way to get an initial `Activity` onto the
    //       stack. Is there a better way?
    fn start(context: &mut Context<Self, R>) -> BoxActivity<Self, R>;
}

pub struct ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    stack: Vec<BoxActivity<T, R>>,
}

impl<T, R> ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    fn peek_mut(&mut self) -> Option<&mut Activity<T, R>> {
        if let Some(activity) = self.stack.last_mut() { // Cannot use `map`.
            Some(activity.as_mut())
        }
        else {
            None
        }
    }

    fn push(&mut self, activity: BoxActivity<T, R>) {
        if let Some(activity) = self.peek_mut() {
            activity.pause();
        }
        self.stack.push(activity);
    }

    fn pop(&mut self) -> bool {
        self.stack.pop().map(|mut activity| {
            activity.stop();
            if let Some(activity) = self.peek_mut() {
                activity.resume()
            }
        }).is_some()
    }

    fn abort(&mut self) {
        while self.pop() {}
    }
}

impl<T, R> Application<T, R> for ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    type UpdateError = ActivityStackError;
    type RenderError = ActivityStackError;

    fn start(context: &mut Context<T, R>) -> Self {
        ActivityStack {
            stack: vec![T::start(context)],
        }
    }

    fn update<C>(&mut self, context: &mut C) -> Result<Execution, Self::UpdateError>
        where C: UpdateContextView<State = T, Window = R::Window>
    {
        let transition = if let Some(activity) = self.peek_mut() {
            activity.update(context)?
        }
        else {
            Transition::Abort
        };
        let execution = transition.to_execution();
        match transition {
            Transition::None => {}
            Transition::Push(activity) => { self.push(activity); }
            Transition::Pop => { self.pop(); }
            Transition::Abort => { self.abort(); }
        }
        Ok(execution)
    }

    fn render<C>(&mut self, context: &mut C) -> Result<(), Self::RenderError>
        where C: RenderContextView<R, State = T, Window = R::Window>
    {
        self.peek_mut().map_or(Ok(()), |activity| {
            activity.render(context).map_err(|error| error.into())
        })
    }

    fn stop(mut self) {
        self.abort();
    }
}

impl<T, R> React for ActivityStack<T, R>
    where T: ActivityState<R>,
          R: MetaRenderer
{
    fn react(&mut self, event: &Event) {
        if let Some(activity) = self.peek_mut() {
            activity.react(event);
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub struct ActivityError;

impl fmt::Display for ActivityError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;

        write!(formatter, "{}", self.description())
    }
}

impl error::Error for ActivityError {
    fn description(&self) -> &str {
        ""
    }
}

#[derive(Clone, Copy, Debug)]
pub struct ActivityStackError;

impl fmt::Display for ActivityStackError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;

        write!(formatter, "{}", self.description())
    }
}

impl error::Error for ActivityStackError {
    fn description(&self) -> &str {
        ""
    }
}

impl From<ActivityError> for ActivityStackError {
    fn from(_: ActivityError) -> Self {
        ActivityStackError
    }
}
{'fraction_non_alphanumeric': 0.14520547945205478, 'fraction_numerical': 0.0, 'mean_word_length': 2.3537102473498233, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2819138', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2917, 'n_words': 750}