diff options
Diffstat (limited to 'games/rstnode/rst-core/src/config/io.rs')
-rw-r--r-- | games/rstnode/rst-core/src/config/io.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/config/io.rs b/games/rstnode/rst-core/src/config/io.rs new file mode 100644 index 000000000000..07aa7e7b3d3f --- /dev/null +++ b/games/rstnode/rst-core/src/config/io.rs @@ -0,0 +1,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())?) +} |