sequent_repl/commands/
load.rs

1//! Loading of a simulation from a YAML document.
2
3use crate::{Context};
4use sequent::persistence::yaml;
5use sequent::{SimulationError};
6use revolver::command::{
7    ApplyCommandError, ApplyOutcome, Command, Description, Example, NamedCommandParser,
8    ParseCommandError,
9};
10use revolver::looper::Looper;
11use revolver::terminal::Terminal;
12use serde::Deserialize;
13use std::borrow::Cow;
14use std::marker::PhantomData;
15use std::path::PathBuf;
16
17/// Command that will load the simulation from a user-specified YAML file. Upon completion, the
18/// simulation will be reset to the initial state, as per the loaded file, and the cursor
19/// position reset to 0.
20pub struct Load<S, C> {
21    path: String,
22    __phantom_data: PhantomData<(S, C)>
23}
24
25impl<S, C> Load<S, C> {
26    pub fn new(path: String) -> Self {
27        Self {
28            path,
29            __phantom_data: PhantomData::default(),
30        }
31    }
32}
33
34impl<S, C: Context<State = S>, T: Terminal> Command<T> for Load<S, C>
35where
36    for<'de> S: Clone + Deserialize<'de>,
37{
38    type Context = C;
39    type Error = SimulationError<S>;
40
41    fn apply(
42        &mut self,
43        looper: &mut Looper<C, SimulationError<S>, T>,
44    ) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
45        let path = PathBuf::from(&self.path);
46        let decoder = looper.context().decoder();
47        let scenario = yaml::read_from_file(decoder, path)
48            .map_err(SimulationError::from)
49            .map_err(ApplyCommandError::Application)?;
50        looper.context().sim().set_scenario(scenario);
51        looper
52            .terminal()
53            .print_line(&format!("Loaded scenario from '{}'.", self.path))?;
54        Ok(ApplyOutcome::Applied)
55    }
56}
57
58/// Parser for [`Load`].
59pub struct Parser<S, C> {
60    __phantom_data: PhantomData<(S, C)>
61}
62
63impl<S, C> Default for Parser<S, C> {
64    fn default() -> Self {
65        Self {
66            __phantom_data: PhantomData::default()
67        }
68    }
69}
70
71impl<S, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C>
72where
73    for<'de> S: Clone + Deserialize<'de> + 'static,
74{
75    type Context = C;
76    type Error = SimulationError<S>;
77
78    fn parse(
79        &self,
80        s: &str,
81    ) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
82        if s.is_empty() {
83            return Err(ParseCommandError("empty arguments to 'load'".into()));
84        }
85        let path = s.into();
86        Ok(Box::new(Load::new(path)))
87    }
88
89    fn shorthand(&self) -> Option<Cow<'static, str>> {
90        None
91    }
92
93    fn name(&self) -> Cow<'static, str> {
94        "load".into()
95    }
96
97    fn description(&self) -> Description {
98        Description {
99            purpose: "Loads a scenario from a file.".into(),
100            usage: "<path>".into(),
101            examples: vec![Example {
102                scenario: "load from a file named 'trixie.yaml' in the working directory".into(),
103                command: "trixie.yaml".into(),
104            }],
105        }
106    }
107}
108
109#[cfg(test)]
110mod tests;