diff options
| author | Arne Dußin | 2020-12-21 21:12:01 +0100 |
|---|---|---|
| committer | GitHub | 2020-12-21 21:12:01 +0100 |
| commit | c435f278eddcada279fdc424120e4a1448843c20 (patch) | |
| tree | be9a5601e99608966d4ccd146c3bfb3a70c7fc02 /src/map/data.rs | |
| parent | 3bc690803fb59493ea8180fd630d65b3e26642d0 (diff) | |
| parent | 82d11b7d3e15d8175accf7579db1fbe528fc6583 (diff) | |
| download | graf_karto-c435f278eddcada279fdc424120e4a1448843c20.tar.gz graf_karto-c435f278eddcada279fdc424120e4a1448843c20.zip | |
Merge pull request #24 from LordSentox/refactor
Refactor to make interaction between tools easier
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()) + } +} |
