aboutsummaryrefslogtreecommitdiff
path: root/src/client/cli/cmd/write.rs
blob: 37d5a0a3441a3c2ab505cf3b489b3327d0d893f2 (plain) (blame)
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
//! Save the contents of the map to disk

use super::Command;
use super::{CmdParseError, FromArgs};
use crate::client::Editor;
use std::path::PathBuf;

/// The save command can take any destination in the filesystem the user can write to. Processing
/// will then save the map contents to that destination, overwriting anything that may be there.
pub struct Write {
    destination: PathBuf,
}

impl FromArgs for Write {
    fn from_args(args: &[&str]) -> Result<Self, CmdParseError> {
        if args.len() != 1 {
            return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1));
        }

        Ok(Self {
            destination: PathBuf::from(args[0]),
        })
    }
}

impl Command for Write {
    fn process(&self, editor: &mut Editor) -> Result<String, String> {
        let world = editor.map().clone_as_world();

        match world.write_to_file(&self.destination) {
            Ok(_) => Ok(format!(
                "Successfully wrote contents to `{:?}`",
                &self.destination
            )),
            Err(e) => Err(format!(
                "Unable to write to `{:?}`. Error: {:?}",
                &self.destination, e
            )),
        }
    }
}