//! 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::net::Cargo; use crate::world::World; 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 { 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 { let world = match World::load_from_file(&self.file) { Ok(data) => data, Err(err) => { return Err(format!( "Unable to read file: {:?}, reason: {:?}", &self.file, err )) } }; // Send all components of the file to the server. 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 {:?} read and added to the current buffer.", &self.file )) } }