module_util/file/
yaml.rs

1use std::fs;
2use std::io;
3use std::path::Path;
4
5use module::Error;
6use serde::de::DeserializeOwned;
7
8use super::{Format, Module};
9
10/// A [`Format`] for [YAML] modules.
11///
12/// Uses [`serde_yaml`] under the hood.
13///
14/// [YAML]: https://yaml.org/
15#[derive(Debug, Default, Clone, Copy)]
16pub struct Yaml;
17
18impl Format for Yaml {
19    fn read<T>(&mut self, path: &Path) -> Result<Module<T>, Error>
20    where
21        T: DeserializeOwned,
22    {
23        let reader = fs::File::options()
24            .read(true)
25            .open(path)
26            .map(io::BufReader::new)
27            .map_err(Error::custom)?;
28
29        let module = serde_yaml::from_reader(reader).map_err(Error::custom)?;
30        Ok(module)
31    }
32}