sequent_repl/commands/
run.rs

1//! Evaluation of the remaining events in the timeline.
2
3use std::borrow::Cow;
4use std::marker::PhantomData;
5use sequent::SimulationError;
6use revolver::command::{ApplyCommandError, ApplyOutcome, Command, Description, NamedCommandParser, ParseCommandError};
7use revolver::looper::Looper;
8use revolver::terminal::Terminal;
9use crate::Context;
10
11/// Command to evaluate the remaining events in the timeline. By completion, the simulation state will
12/// reflect the sequential application of all events.
13pub struct Run<S, C> {
14    __phantom_data: PhantomData<(S, C)>
15}
16
17impl<S, C> Default for Run<S, C> {
18    fn default() -> Self {
19        Self {
20            __phantom_data: PhantomData::default()
21        }
22    }
23}
24
25impl<S, C: Context<State = S>, T: Terminal> Command<T> for Run<S, C> {
26    type Context = C;
27    type Error = SimulationError<S>;
28
29    fn apply(&mut self, looper: &mut Looper<C, SimulationError<S>, T>) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
30        let (terminal, _, context) = looper.split();
31        context.sim().run().map_err(ApplyCommandError::Application)?;
32        context.print_state(terminal)?;
33        Ok(ApplyOutcome::Applied)
34    }
35}
36
37/// Parser for [`Run`].
38pub struct Parser<S, C> {
39    __phantom_data: PhantomData<(S, C)>
40}
41
42impl<S, C> Default for Parser<S, C> {
43    fn default() -> Self {
44        Self {
45            __phantom_data: PhantomData::default()
46        }
47    }
48}
49
50impl<S: 'static, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C> {
51    type Context = C;
52    type Error = SimulationError<S>;
53
54    fn parse(&self, s: &str) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
55        self.parse_no_args(s, Run::default)
56    }
57
58    fn shorthand(&self) -> Option<Cow<'static, str>> {
59        Some("r".into())
60    }
61
62    fn name(&self) -> Cow<'static, str> {
63        "run".into()
64    }
65
66    fn description(&self) -> Description {
67        Description {
68            purpose: "Evaluates the remaining events in the timeline.".into(),
69            usage: Cow::default(),
70            examples: Vec::default()
71        }
72    }
73}
74
75#[cfg(test)]
76mod tests;