aboutsummaryrefslogtreecommitdiff
path: root/src/map/data.rs
blob: f9780816a6da77936cec4cbfa401dcc7b88a24f4 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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())
    }
}