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}
140
141#[cfg(feature = "tokio")]
142pub trait FormatEncoderAsync<V: ?Sized> {
143 type EncodeError;
145
146 fn encode_async(
147 &mut self,
148 writer: impl tokio::io::AsyncWrite + Send,
149 value: &V,
150 ) -> impl Future<Output = Result<(), Self::EncodeError>> + Send;
151}
152
153#[cfg(feature = "tokio")]
154pub trait FormatDecoderAsync<V> {
155 type DecodeError;
157
158 fn decode_async(
159 &mut self,
160 reader: impl tokio::io::AsyncRead + Send,
161 ) -> impl Future<Output = Result<V, Self::DecodeError>> + Send;
162}
163
164#[cfg(feature = "tokio")]
165impl<T, V> FormatEncoderAsync<V> for &mut T
166where
167 T: ?Sized,
168 V: ?Sized,
169 T: FormatEncoderAsync<V>,
170{
171 type EncodeError = T::EncodeError;
172
173 fn encode_async(
174 &mut self,
175 writer: impl tokio::io::AsyncWrite + Send,
176 value: &V,
177 ) -> impl Future<Output = Result<(), Self::EncodeError>> + Send {
178 T::encode_async(self, writer, value)
179 }
180}
181
182#[cfg(feature = "tokio")]
183impl<T, V> FormatEncoderAsync<V> for Box<T>
184where
185 T: ?Sized,
186 V: ?Sized,
187 T: FormatEncoderAsync<V>,
188{
189 type EncodeError = T::EncodeError;
190
191 fn encode_async(
192 &mut self,
193 writer: impl tokio::io::AsyncWrite + Send,
194 value: &V,
195 ) -> impl Future<Output = Result<(), Self::EncodeError>> + Send {
196 T::encode_async(self, writer, value)
197 }
198}
199
200#[cfg(feature = "tokio")]
201impl<T, V> FormatDecoderAsync<V> for &mut T
202where
203 T: ?Sized,
204 T: FormatDecoderAsync<V>,
205{
206 type DecodeError = T::DecodeError;
207
208 fn decode_async(
209 &mut self,
210 reader: impl tokio::io::AsyncRead + Send,
211 ) -> impl Future<Output = Result<V, Self::DecodeError>> + Send {
212 T::decode_async(self, reader)
213 }
214}
215
216#[cfg(feature = "tokio")]
217impl<T, V> FormatDecoderAsync<V> for Box<T>
218where
219 T: ?Sized,
220 T: FormatDecoderAsync<V>,
221{
222 type DecodeError = T::DecodeError;
223
224 fn decode_async(
225 &mut self,
226 reader: impl tokio::io::AsyncRead + Send,
227 ) -> impl Future<Output = Result<V, Self::DecodeError>> + Send {
228 T::decode_async(self, reader)
229 }
230}