aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/config/io.rs
blob: 07aa7e7b3d3f6f5278ca6c33ad7efd1b92a08d57 (plain)
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
//! I/O utilities to load configurations from disk

use super::MapCfg;
use serde_yaml::from_str;
use std::{
    fs::File,
    io::{self, Read},
    path::Path,
};

/// Encode error state while loading a map configuration
pub enum Error {
    Io(String),
    Parsing(String),
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Self::Io(e.to_string())
    }
}

impl From<serde_yaml::Error> for Error {
    fn from(e: serde_yaml::Error) -> Self {
        Self::Parsing(e.to_string())
    }
}

/// Load a map YAML configuration from disk
///
/// This file format is structured according to the configuration
/// types in [config](crate::config).  An example configuration is
/// provided below.
///
/// ```yaml
/// nodes:
///   - 
/// ```
pub fn load_map<'p>(p: impl Into<&'p Path>) -> Result<MapCfg, Error> {
    let mut f = File::open(p.into())?;
    let mut s = String::new();
    f.read_to_string(&mut s)?;

    Ok(from_str(s.as_str())?)
}