//! Save the contents of the map to disk use super::Command; use super::{CmdParseError, FromArgs}; use crate::map::MapData; use crate::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 Save { destination: PathBuf, } impl FromArgs for Save { fn from_args(args: &[&str]) -> Result { if args.len() != 1 { return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1)); } Ok(Self { destination: PathBuf::from(args[0]), }) } } impl Command for Save { fn process(&self, editor: &mut Editor) -> Result { let data = MapData::extract_data(editor.map()); match data.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 )), } } }