aboutsummaryrefslogtreecommitdiff
path: root/src/cli/cmd
diff options
context:
space:
mode:
authorArne Dußin2021-01-06 22:47:34 +0100
committerArne Dußin2021-01-06 22:47:34 +0100
commit0eada0bdcb36a9907c6c928aa707ed6bef03c02f (patch)
treed686f1d7c41695d4e7bac9c190377bd634c36250 /src/cli/cmd
parent61d255a420c9d977b46670e7fa9e7735d2acf819 (diff)
downloadgraf_karto-0eada0bdcb36a9907c6c928aa707ed6bef03c02f.tar.gz
graf_karto-0eada0bdcb36a9907c6c928aa707ed6bef03c02f.zip
Add loading capabilities back
Diffstat (limited to 'src/cli/cmd')
-rw-r--r--src/cli/cmd/edit.rs35
-rw-r--r--src/cli/cmd/mod.rs6
-rw-r--r--src/cli/cmd/read.rs38
3 files changed, 79 insertions, 0 deletions
diff --git a/src/cli/cmd/edit.rs b/src/cli/cmd/edit.rs
new file mode 100644
index 0000000..797edc6
--- /dev/null
+++ b/src/cli/cmd/edit.rs
@@ -0,0 +1,35 @@
+//! Replace the contents of the currently edited map with contents from a file.
+
+use super::Command;
+use super::{CmdParseError, FromArgs};
+use crate::map::MapData;
+use crate::Editor;
+use std::path::PathBuf;
+
+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: {:?}", &self.file)),
+ };
+
+ editor.map_mut().set_data(data);
+ Ok(format!("Map data from {:?} loaded.", &self.file))
+ }
+}
diff --git a/src/cli/cmd/mod.rs b/src/cli/cmd/mod.rs
index 29063e8..42e865a 100644
--- a/src/cli/cmd/mod.rs
+++ b/src/cli/cmd/mod.rs
@@ -1,7 +1,11 @@
//! The commands that can be performed in the CLI
+pub mod edit;
+pub mod read;
pub mod save;
+pub use edit::*;
+pub use read::*;
pub use save::*;
use crate::Editor;
@@ -32,6 +36,8 @@ pub fn parse_command(string: &str) -> Result<Box<dyn Command>, CmdParseError> {
let parts: Vec<&str> = string.split_whitespace().collect();
match parts[0] {
"w" => Ok(Box::new(Save::from_args(&parts[1..])?)),
+ "e" => Ok(Box::new(Edit::from_args(&parts[1..])?)),
+ "r" => Ok(Box::new(Read::from_args(&parts[1..])?)),
other => Err(CmdParseError::NoSuchCmd(other.to_owned())),
}
}
diff --git a/src/cli/cmd/read.rs b/src/cli/cmd/read.rs
new file mode 100644
index 0000000..4ac671c
--- /dev/null
+++ b/src/cli/cmd/read.rs
@@ -0,0 +1,38 @@
+//! Read the contents of a file and add it to the currently edited map.
+
+use super::Command;
+use super::{CmdParseError, FromArgs};
+use crate::map::MapData;
+use crate::Editor;
+use std::path::PathBuf;
+
+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: {:?}", &self.file)),
+ };
+
+ editor.map_mut().add_data(data);
+ Ok(format!(
+ "Map data from {:?} read and added to the current buffer.",
+ &self.file
+ ))
+ }
+}