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