My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/games/rstnode/rst-core/src/config/io.rs

45 lines
969 B

//! 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())?)
}