1use format_ende::FormatDecoder;
32use format_ende::FormatEncoder;
33use format_ende::FormatInfo;
34
35#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
37pub struct JsonFormat {
38 pub pretty: bool,
39}
40
41impl JsonFormat {
42 pub const fn new() -> Self {
43 Self { pretty: false }
44 }
45}
46
47impl Default for JsonFormat {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl FormatInfo for JsonFormat {
54 fn file_extension(&self) -> &str {
55 "json"
56 }
57}
58
59impl<V> FormatEncoder<V> for JsonFormat
60where
61 V: serde::Serialize + ?Sized,
62{
63 type EncodeError = serde_json::Error;
64
65 fn encode(
66 &mut self,
67 mut writer: impl std::io::Write,
68 value: &V,
69 ) -> Result<(), Self::EncodeError> {
70 if self.pretty {
71 serde_json::to_writer_pretty(&mut writer, value)
72 } else {
73 serde_json::to_writer(&mut writer, value)
74 }
75 }
76}
77
78impl<V> FormatDecoder<V> for JsonFormat
79where
80 V: serde::de::DeserializeOwned,
81{
82 type DecodeError = serde_json::Error;
83
84 fn decode(&mut self, reader: impl std::io::Read) -> Result<V, Self::DecodeError> {
85 serde_json::from_reader(reader)
86 }
87}