diff options
Diffstat (limited to 'src/map/data.rs')
| -rw-r--r-- | src/map/data.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/map/data.rs b/src/map/data.rs new file mode 100644 index 0000000..f978081 --- /dev/null +++ b/src/map/data.rs @@ -0,0 +1,64 @@ +use super::{IconData, PolygonRoomData, RectRoomData, WallData}; +use ron::de::from_reader; +use ron::ser::{to_string_pretty, PrettyConfig}; +use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::io::{self, Write}; +use std::path::Path; + +/// The serialisable and deserialisable parts of the map. This can be created to get a version of the +/// map which is persistifiable or sendable/receivable without data overhead or data that might make +/// it easily corruptable. +#[derive(Serialize, Deserialize)] +pub struct MapData { + pub(super) rect_rooms: Vec<RectRoomData>, + pub(super) polygon_rooms: Vec<PolygonRoomData>, + pub(super) walls: Vec<WallData>, + pub(super) icons: Vec<IconData>, +} + +impl MapData { + pub fn new( + rect_rooms: Vec<RectRoomData>, + polygon_rooms: Vec<PolygonRoomData>, + walls: Vec<WallData>, + icons: Vec<IconData>, + ) -> Self { + Self { + rect_rooms, + polygon_rooms, + walls, + icons, + } + } + + pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<Self> { + let file = File::open(&path)?; + let data: Self = match from_reader(file) { + Ok(data) => data, + Err(err) => { + return Err(io::Error::new(io::ErrorKind::InvalidData, err)); + } + }; + + Ok(data) + } + + pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { + let mut file = File::create(&path)?; + + let pretty_conf = PrettyConfig::new() + .with_depth_limit(4) + .with_decimal_floats(true) + .with_separate_tuple_members(true) + .with_indentor("\t".to_owned()); + let string = match to_string_pretty(&self, pretty_conf) { + Ok(string) => string, + Err(err) => { + return Err(io::Error::new(io::ErrorKind::InvalidInput, err)); + } + }; + + file.write_all(&string.as_bytes()) + } +} |
