spacegate_config/service/config_format/
json.rs

1use std::ffi::{OsStr, OsString};
2
3use crate::BoxError;
4
5use super::ConfigFormat;
6#[derive(Debug, Clone)]
7pub struct Json {
8    pub extension: OsString,
9    pub pretty: bool,
10}
11
12impl Default for Json {
13    fn default() -> Self {
14        Self {
15            extension: OsString::from("json"),
16            pretty: true,
17        }
18    }
19}
20
21impl ConfigFormat for Json {
22    fn extension(&self) -> &OsStr {
23        &self.extension
24    }
25    fn de<T: serde::de::DeserializeOwned>(&self, slice: &[u8]) -> Result<T, BoxError> {
26        Ok(serde_json::from_slice(slice)?)
27    }
28    fn ser<T: serde::Serialize>(&self, t: &T) -> Result<Vec<u8>, BoxError> {
29        if self.pretty {
30            Ok(serde_json::to_vec_pretty(t)?)
31        } else {
32            Ok(serde_json::to_vec(t)?)
33        }
34    }
35}