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 | |
| 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')
| -rw-r--r-- | src/cli/cmd/edit.rs | 35 | ||||
| -rw-r--r-- | src/cli/cmd/mod.rs | 59 | ||||
| -rw-r--r-- | src/cli/cmd/read.rs | 38 | ||||
| -rw-r--r-- | src/cli/cmd/save.rs | 42 |
4 files changed, 174 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 new file mode 100644 index 0000000..42e865a --- /dev/null +++ b/src/cli/cmd/mod.rs @@ -0,0 +1,59 @@ +//! 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; +use std::ops::RangeInclusive; + +/// Errors that can occur when parsing a command. This is for syntax checking, the +/// semantics are checked when trying to execute the command. +#[allow(missing_docs)] +#[derive(thiserror::Error, Debug)] +pub enum CmdParseError { + #[error("no command specified")] + StringEmpty, + #[error("the command {0} is unknown")] + NoSuchCmd(String), + #[error("wrong number of arguments. Expected in range {1:?}, but received {0}")] + WrongNumberOfArgs(usize, RangeInclusive<usize>), + #[error("{0} cannot be converted into a {1}, which is required")] + InvalidArgType(String, &'static str), +} + +/// Attempts to parse a command from the given string. If it is unsuccessful, it returns a +/// [CmdParseError]. +pub fn parse_command(string: &str) -> Result<Box<dyn Command>, CmdParseError> { + if string.is_empty() { + return Err(CmdParseError::StringEmpty); + } + + 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())), + } +} + +/// Indicates that this entity (command) can be created from arguments. Make sure to check what is +/// expected, to pass the arguments to the correct command. +pub trait FromArgs: Sized { + /// Creates a new instance from the arguments provided. If for whatever reason the syntax of the + /// given arguments is correct an [ArgParseError] is returned. + fn from_args(args: &[&str]) -> Result<Self, CmdParseError>; +} + +/// A common trait for all commands. +pub trait Command { + /// Process this command on the provided context. Returns either a string with the output of the + /// command when everything went right with it, or an error string explaining what went wrong, + /// which can be displayed to the user. + fn process(&self, editor: &mut Editor) -> Result<String, String>; +} 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 + )) + } +} 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 + )), + } + } +} |
