1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Saving of the current scenario to a YAML document.

use crate::commands::prompt::YesNo;
use crate::Context;
use sequent::persistence::yaml;
use sequent::SimulationError;
use revolver::command::{
    ApplyCommandError, ApplyOutcome, Command, Description, Example, NamedCommandParser,
    ParseCommandError,
};
use revolver::looper::Looper;
use revolver::terminal::Terminal;
use serde::ser::Serialize;
use std::borrow::Cow;
use std::path::PathBuf;

/// Command to save the scenario to a user-specified output file. If the file exists, a yes/no prompt
/// will be presented before overwriting it.
pub struct Save {
    path: String,
}

impl<S: Clone + Serialize, C: Context<S>, T: Terminal> Command<C, SimulationError<S>, T> for Save {
    fn apply(
        &mut self,
        looper: &mut Looper<C, SimulationError<S>, T>,
    ) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
        let path = PathBuf::from(&self.path);
        if path.exists() {
            let response = looper
                .terminal()
                .read_from_str_default("Output file exists. Overwrite? [y/N]: ")?;

            if let YesNo::No = response {
                return Ok(ApplyOutcome::Skipped);
            }
        }
        yaml::write_to_file(looper.context().sim().scenario(), path)
            .map_err(SimulationError::from)
            .map_err(ApplyCommandError::Application)?;

        looper
            .terminal()
            .print_line(&format!("Saved scenario to '{}'.", self.path))?;
        Ok(ApplyOutcome::Applied)
    }
}

/// Parser for [`Save`].
pub struct Parser;

impl<S: Clone + Serialize, C: Context<S>, T: Terminal> NamedCommandParser<C, SimulationError<S>, T>
    for Parser
{
    fn parse(
        &self,
        s: &str,
    ) -> Result<Box<dyn Command<C, SimulationError<S>, T>>, ParseCommandError> {
        if s.is_empty() {
            return Err(ParseCommandError("empty arguments to 'save'".into()));
        }
        let path = s.into();
        Ok(Box::new(Save { path }))
    }

    fn shorthand(&self) -> Option<Cow<'static, str>> {
        None
    }

    fn name(&self) -> Cow<'static, str> {
        "save".into()
    }

    fn description(&self) -> Description {
        Description {
            purpose: "Saves the current scenario to a file.".into(),
            usage: "<path>".into(),
            examples: vec![Example {
                scenario: "save to a file named 'trixie.yaml' in the working directory".into(),
                command: "trixie.yaml".into(),
            }],
        }
    }
}

#[cfg(test)]
mod tests;