diff options
| author | Arne Dußin | 2021-01-06 22:56:37 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-01-06 22:56:37 +0100 |
| commit | fa1afb6be3ba2d521eb0791edc0bb8e631a85327 (patch) | |
| tree | e0a365444784efaaeb1eea6373b34559b6d57fbc /src/cli/cmd/save.rs | |
| parent | 1c81d7c70fe891e6ded49d49d6a09f04ce74dd6e (diff) | |
| parent | 30b23db9e86fdf72a4e7de72213df274ce19123e (diff) | |
| download | graf_karto-fa1afb6be3ba2d521eb0791edc0bb8e631a85327.tar.gz graf_karto-fa1afb6be3ba2d521eb0791edc0bb8e631a85327.zip | |
Merge branch 'master' into snapping
Diffstat (limited to 'src/cli/cmd/save.rs')
| -rw-r--r-- | src/cli/cmd/save.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/cli/cmd/save.rs b/src/cli/cmd/save.rs new file mode 100644 index 0000000..2c022cf --- /dev/null +++ b/src/cli/cmd/save.rs @@ -0,0 +1,42 @@ +//! 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<Self, CmdParseError> { + 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<String, String> { + 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 + )), + } + } +} |
