sequent_repl/commands/
print.rs

1//! Printing of the simulation state.
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 that prints the current simulation state to the terminal device.
12pub struct Print<S, C> {
13    __phantom_data: PhantomData<(S, C)>
14}
15
16impl<S, C> Default for Print<S, C> {
17    fn default() -> Self {
18        Self {
19            __phantom_data: PhantomData::default()
20        }
21    }
22}
23
24impl<S, C: Context<State = S>, T: Terminal> Command<T> for Print<S, C> {
25    type Context = C;
26    type Error = SimulationError<S>;
27
28    fn apply(&mut self, looper: &mut Looper<C, SimulationError<S>, T>) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
29        let (terminal, _, context) = looper.split();
30        context.print_state(terminal)?;
31        Ok(ApplyOutcome::Applied)
32    }
33}
34
35/// Parser for [`Print`].
36pub struct Parser<S, C> {
37    __phantom_data: PhantomData<(S, C)>
38}
39
40impl<S, C> Default for Parser<S, C> {
41    fn default() -> Self {
42        Self {
43            __phantom_data: PhantomData::default()
44        }
45    }
46}
47
48impl<S: 'static, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C> {
49    type Context = C;
50    type Error = SimulationError<S>;
51
52    fn parse(&self, s: &str) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
53        self.parse_no_args(s, Print::default)
54    }
55
56    fn shorthand(&self) -> Option<Cow<'static, str>> {
57        Some("p".into())
58    }
59
60    fn name(&self) -> Cow<'static, str> {
61        "print".into()
62    }
63
64    fn description(&self) -> Description {
65        Description {
66            purpose: "Prints the current simulation state.".into(),
67            usage: Cow::default(),
68            examples: Vec::default()
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests;