aboutsummaryrefslogtreecommitdiff
path: root/src/client/cli/cmd/edit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/cli/cmd/edit.rs')
-rw-r--r--src/client/cli/cmd/edit.rs53
1 files changed, 53 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..1cfb530
--- /dev/null
+++ b/src/client/cli/cmd/edit.rs
@@ -0,0 +1,53 @@
+//! Replace the contents of the currently edited map with contents from a file.
+
+use super::Command;
+use super::{CmdParseError, FromArgs};
+use crate::client::Editor;
+use crate::net::Cargo;
+use crate::world::World;
+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 world = match World::load_from_file(&self.file) {
+ Ok(world) => world,
+ Err(err) => {
+ return Err(format!(
+ "Unable to read file: {:?}, reason: {:?}",
+ &self.file, err
+ ))
+ }
+ };
+
+ // Clear all data from the world, afterwards add all components from the file.
+ editor.server().send(Cargo::ClearAll);
+ for (_, icon) in world.icons().iter() {
+ editor.server().send(Cargo::AddIcon(icon.clone()));
+ }
+ for (_, room) in world.rooms().iter() {
+ editor.server().send(Cargo::AddRoom(room.clone()));
+ }
+ for (_, wall) in world.walls().iter() {
+ editor.server().send(Cargo::AddWall(wall.clone()));
+ }
+
+ Ok(format!("Map data from {:?} loaded.", &self.file))
+ }
+}