1use std::error::Error;
27use std::io;
28
29pub trait FormatInfo {
31 fn file_extension(&self) -> &str;
33
34 fn is_utf8(&self) -> bool {
36 false
37 }
38}
39
40pub trait FormatEncoder<V: ?Sized> {
42 type EncodeError;
44
45 fn encode(&mut self, writer: impl io::Write, value: &V) -> Result<(), Self::EncodeError>;
47}
48
49pub trait FormatDecoder<V> {
51 type DecodeError;
53
54 fn decode(&mut self, reader: impl io::Read) -> Result<V, Self::DecodeError>;
56}
57
58pub trait FormatEncoderDyn<V: ?Sized, E = Box<dyn Error>> {
60 fn encode_dyn(&mut self, writer: &mut dyn io::Write, value: &V) -> Result<(), E>;
62}
63
64pub trait FormatDecoderDyn<V, E = Box<dyn Error>> {
66 fn decode_dyn(&mut self, reader: &mut dyn io::Read) -> Result<V, E>;
68}
69
70impl<T, V> FormatEncoder<V> for &mut T
71where
72 T: ?Sized,
73 V: ?Sized,
74 T: FormatEncoder<V>,
75{
76 type EncodeError = T::EncodeError;
77
78 fn encode(&mut self, writer: impl io::Write, value: &V) -> Result<(), Self::EncodeError> {
79 T::encode(self, writer, value)
80 }
81}
82
83impl<T, V> FormatEncoder<V> for Box<T>
84where
85 T: ?Sized,
86 V: ?Sized,
87 T: FormatEncoder<V>,
88{
89 type EncodeError = T::EncodeError;
90
91 fn encode(&mut self, writer: impl io::Write, value: &V) -> Result<(), Self::EncodeError> {
92 T::encode(self, writer, value)
93 }
94}
95
96impl<T, V> FormatDecoder<V> for &mut T
97where
98 T: ?Sized,
99 T: FormatDecoder<V>,
100{
101 type DecodeError = T::DecodeError;
102
103 fn decode(&mut self, reader: impl io::Read) -> Result<V, Self::DecodeError> {
104 T::decode(self, reader)
105 }
106}
107
108impl<T, V> FormatDecoder<V> for Box<T>
109where
110 T: ?Sized,
111 T: FormatDecoder<V>,
112{
113 type DecodeError = T::DecodeError;
114
115 fn decode(&mut self, reader: impl io::Read) -> Result<V, Self::DecodeError> {
116 T::decode(self, reader)
117 }
118}
119
120impl<T, V, E> FormatEncoderDyn<V, E> for T
121where
122 T: FormatEncoder<V> + ?Sized,
123 V: ?Sized,
124 T::EncodeError: Into<E>,
125{
126 fn encode_dyn(&mut self, writer: &mut dyn io::Write, value: &V) -> Result<(), E> {
127 self.encode(writer, value).map_err(Into::into)
128 }
129}
130
131impl<T, V, E> FormatDecoderDyn<V, E> for T
132where
133 T: FormatDecoder<V> + ?Sized,
134 T::DecodeError: Into<E>,
135{
136 fn decode_dyn(&mut self, reader: &mut dyn io::Read) -> Result<V, E> {
137 self.decode(reader).map_err(Into::into)
138 }
139}