//! 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; /// 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 { 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 { 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().set_data(data); Ok(format!("Map data from {:?} loaded.", &self.file)) } }