aboutsummaryrefslogtreecommitdiff
path: root/src/client/cli/cmd/read.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/cli/cmd/read.rs')
-rw-r--r--src/client/cli/cmd/read.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/client/cli/cmd/read.rs b/src/client/cli/cmd/read.rs
new file mode 100644
index 0000000..313530a
--- /dev/null
+++ b/src/client/cli/cmd/read.rs
@@ -0,0 +1,44 @@
+//! Read the contents of a file and add it to the currently edited map.
+
+use super::Command;
+use super::{CmdParseError, FromArgs};
+use crate::client::Editor;
+use crate::map::MapData;
+use std::path::PathBuf;
+
+/// Command to read a file from the system
+pub struct Read {
+ file: PathBuf,
+}
+
+impl FromArgs for Read {
+ 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 Read {
+ 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().add_data(data);
+ Ok(format!(
+ "Map data from {:?} read and added to the current buffer.",
+ &self.file
+ ))
+ }
+}