aboutsummaryrefslogtreecommitdiff
path: root/src/client/cli/cmd/edit.rs
diff options
context:
space:
mode:
authorArne Dußin2021-01-27 14:01:50 +0100
committerArne Dußin2021-02-02 22:16:15 +0100
commitf92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch)
tree20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/cli/cmd/edit.rs
parentc60a6d07efb120724b308e29e8e70f27c87c952d (diff)
downloadgraf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz
graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/cli/cmd/edit.rs')
-rw-r--r--src/client/cli/cmd/edit.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/client/cli/cmd/edit.rs b/src/client/cli/cmd/edit.rs
new file mode 100644
index 0000000..7a02959
--- /dev/null
+++ b/src/client/cli/cmd/edit.rs
@@ -0,0 +1,41 @@
+//! Replace the contents of the currently edited map with contents from a file.
+
+use super::Command;
+use super::{CmdParseError, FromArgs};
+use crate::client::map::MapData;
+use crate::client::Editor;
+use std::path::PathBuf;
+
+/// Command to load a file from the disk and replace the current editor contents with it's info.
+pub struct Edit {
+ file: PathBuf,
+}
+
+impl FromArgs for Edit {
+ fn from_args(args: &[&str]) -> Result<Self, CmdParseError> {
+ if args.len() != 1 {
+ return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1));
+ }
+
+ Ok(Self {
+ file: PathBuf::from(args[0]),
+ })
+ }
+}
+
+impl Command for Edit {
+ fn process(&self, editor: &mut Editor) -> Result<String, String> {
+ let data = match MapData::load_from_file(&self.file) {
+ Ok(data) => data,
+ Err(err) => {
+ return Err(format!(
+ "Unable to read file: {:?}, reason: {:?}",
+ &self.file, err
+ ))
+ }
+ };
+
+ editor.map_mut().set_data(data);
+ Ok(format!("Map data from {:?} loaded.", &self.file))
+ }
+}