2021-12-13 19:40:41 +03:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
|
|
|
/// read data from yaml as struct T
|
2021-12-19 18:56:16 +03:00
|
|
|
pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
|
|
|
|
let yaml_str = fs::read_to_string(path).unwrap_or("".into());
|
|
|
|
serde_yaml::from_str::<T>(&yaml_str).unwrap_or(T::default())
|
2021-12-13 19:40:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// - save the data to the file
|
|
|
|
/// - can set `prefix` string to add some comments
|
|
|
|
pub fn save_yaml<T: Serialize>(
|
|
|
|
path: PathBuf,
|
|
|
|
data: &T,
|
|
|
|
prefix: Option<&str>,
|
|
|
|
) -> Result<(), String> {
|
2022-01-07 18:29:20 +03:00
|
|
|
match serde_yaml::to_string(data) {
|
|
|
|
Ok(data_str) => {
|
|
|
|
let yaml_str = match prefix {
|
|
|
|
Some(prefix) => format!("{}{}", prefix, data_str),
|
|
|
|
None => data_str,
|
|
|
|
};
|
|
|
|
|
|
|
|
let path_str = path.as_os_str().to_string_lossy().to_string();
|
|
|
|
match fs::write(path, yaml_str.as_bytes()) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(format!("can not save file `{}`", path_str)),
|
|
|
|
}
|
2021-12-13 19:40:41 +03:00
|
|
|
}
|
2022-01-07 18:29:20 +03:00
|
|
|
Err(_) => Err("can not convert the data to yaml".into()),
|
2021-12-13 19:40:41 +03:00
|
|
|
}
|
|
|
|
}
|