libcnb_common/
toml_file.rs1use serde::{Serialize, de::DeserializeOwned};
2use std::{fs, path::Path};
3
4#[derive(thiserror::Error, Debug)]
6pub enum TomlFileError {
7 #[error("I/O error while reading/writing TOML file: {0}")]
8 IoError(#[from] std::io::Error),
9
10 #[error("TOML deserialization error while reading TOML file: {0}")]
11 TomlDeserializationError(#[from] toml::de::Error),
12
13 #[error("TOML serialization error while writing TOML file: {0}")]
14 TomlSerializationError(#[from] toml::ser::Error),
15}
16
17pub fn write_toml_file(
23 value: &impl Serialize,
24 path: impl AsRef<Path>,
25) -> Result<(), TomlFileError> {
26 fs::write(path, toml::to_string(value)?)?;
27
28 Ok(())
29}
30
31pub fn read_toml_file<A: DeserializeOwned>(path: impl AsRef<Path>) -> Result<A, TomlFileError> {
37 let contents = fs::read_to_string(path)?;
38 Ok(toml::from_str(&contents)?)
39}