From 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 15 Dec 2020 00:46:54 +0100 Subject: Refactor to make interaction between tools easier --- src/map/data.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/map/data.rs (limited to 'src/map/data.rs') 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, + pub(super) polygon_rooms: Vec, + pub(super) walls: Vec, + pub(super) icons: Vec, +} + +impl MapData { + pub fn new( + rect_rooms: Vec, + polygon_rooms: Vec, + walls: Vec, + icons: Vec, + ) -> Self { + Self { + rect_rooms, + polygon_rooms, + walls, + icons, + } + } + + pub fn load_from_file>(&mut self, path: P) -> io::Result { + 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>(&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()) + } +} -- cgit v1.2.3-70-g09d2