serde_loader/
json5.rs

1use crate::{
2    common::*,
3    file::{FileDumper, FileLoader, FilePath},
4};
5
6pub type Json5Path<T> = FilePath<T, Json5Dumper, Json5Loader>;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct Json5Dumper {
10    _private: [u8; 0],
11}
12
13impl<T> FileDumper<T> for Json5Dumper
14where
15    T: Serialize,
16{
17    type Error = anyhow::Error;
18
19    fn dump<P>(p: P, value: &T) -> Result<(), Self::Error>
20    where
21        P: AsRef<Path>,
22    {
23        let text = json5::to_string(value)?;
24        fs::write(p, &text)?;
25        Ok(())
26    }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub struct Json5Loader {
31    _private: [u8; 0],
32}
33
34impl<T> FileLoader<T> for Json5Loader
35where
36    T: DeserializeOwned,
37{
38    type Error = anyhow::Error;
39
40    fn load<P>(p: P) -> Result<T, Self::Error>
41    where
42        P: AsRef<Path>,
43    {
44        let text = fs::read_to_string(p)?;
45        let value = json5::from_str(&text)?;
46        Ok(value)
47    }
48}