module_util/file/toml.rs
1use std::fs;
2use std::path::Path;
3
4use module::Error;
5use serde::de::DeserializeOwned;
6
7use super::{Format, Module};
8
9/// A [`Format`] for [TOML] modules.
10///
11/// Uses [`toml`] under the hood.
12///
13/// [TOML]: https://toml.io/en/
14#[derive(Debug, Default, Clone, Copy)]
15pub struct Toml;
16
17impl Format for Toml {
18 fn read<T>(&mut self, path: &Path) -> Result<Module<T>, Error>
19 where
20 T: DeserializeOwned,
21 {
22 let data = fs::read_to_string(path).map_err(Error::custom)?;
23 let module = toml::from_str(&data).map_err(Error::custom)?;
24 Ok(module)
25 }
26}