1pub mod structure;
2use crate::error::structured_file::StructuredFileError;
3use crate::error::structured_file::StructuredFileError::ReadJsonFileFailed;
4use crate::error::structured_file::StructuredFileError::{
5 DeserializeJsonFileFailed, SerializeJsonFileFailed,
6};
7use serde::Serialize;
8use std::path::Path;
9
10pub fn load_json_file<T: for<'a> serde::de::Deserialize<'a>>(
11 path: &Path,
12) -> Result<T, StructuredFileError> {
13 let content = crate::fs::read(path).map_err(ReadJsonFileFailed)?;
14
15 serde_json::from_slice(content.as_ref())
16 .map_err(|err| DeserializeJsonFileFailed(Box::new(path.to_path_buf()), err))
17}
18
19pub fn save_json_file<T: Serialize>(path: &Path, value: &T) -> Result<(), StructuredFileError> {
20 let content = serde_json::to_string_pretty(&value)
21 .map_err(|err| SerializeJsonFileFailed(Box::new(path.to_path_buf()), err))?;
22 crate::fs::write(path, content)?;
23 Ok(())
24}