blob: 797edc6f326f4990947deebba2af6aa5a469b88e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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))
}
}
|