1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! A set of compositional operations on [`Decoder`].
//!
//! The operations take simpler decoders as inputs with customization functions and produce more powerful ones as output.

pub mod adaptors;

use self::adaptors::{
    DecoderAndThen, DecoderBoxed, DecoderMap, DecoderMapErr, DecoderMapInto, DecoderThen,
    DecoderTryMap, DecoderTryMapInto,
};

use tokio_util::codec::Decoder;

/// Extension of [`Decoder`] with compositional operations.
pub trait DecoderExt<A, E>: Decoder<Item = A, Error = E> {
    /// Applies a function `f` of type `A -> B` over the decoded value when that is `Ok(Some(a))`.
    ///
    /// The function `f` cannot fail. If you need a fallible mapping, then consider [`DecoderExt::try_map`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// struct Device(u8);
    ///
    /// let device = uint8().map(Device).decode(&mut BytesMut::from("\x2A")).unwrap();
    /// assert_eq!(device, Some(Device(42)));
    /// ```
    fn map<F, B>(self, f: F) -> DecoderMap<Self, F>
    where
        F: Fn(A) -> B,
        Self: Sized,
    {
        DecoderMap::new(self, f)
    }

    /// Applies an `B::from` `A` conversion over the decoded value when that is `Ok(Some(a))`.
    ///
    /// The conversion cannot fail. If you need a fallible conversion, then consider [`DecoderExt::try_map_into`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// struct Device(u8);
    ///
    /// impl From<u8> for Device {
    ///     fn from(value: u8) -> Self {
    ///         Self(value)
    ///     }
    /// }
    ///
    /// let device = uint8().map_into::<Device>().decode(&mut BytesMut::from("\x2A")).unwrap();
    /// assert_eq!(device, Some(Device(42)));
    /// ```
    fn map_into<B>(self) -> DecoderMapInto<Self, B>
    where
        B: From<A>,
        Self: Sized,
    {
        DecoderMapInto::new(self)
    }

    /// Applies a fallible function `f` of type `A -> Result<B, EE>` over the decoded value when that is `Ok(Some(a))`.
    ///
    /// The function `f` can fail and that's handy when we interleave decoding with validation,
    /// for instance, when mapping from a larger domain (e.g. `u8`) into a smaller co-domain (e.g. `Version::v1`).
    /// If you don't need a fallible mapping, then consider [`DecoderExt::map`].
    ///
    /// The mapping can return an error type `EE` other than `E` as long as there is an implicit conversion [`From<E>`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// enum Version {
    ///     V1
    /// }
    ///
    /// impl TryFrom<u8> for Version {
    ///     type Error = std::io::Error;
    ///
    ///     fn try_from(value: u8) -> Result<Self, Self::Error> {
    ///             match value {
    ///                 1 => Ok(Version::V1),
    ///                 _ => Err(std::io::Error::from(std::io::ErrorKind::InvalidData))
    ///             }
    ///     }
    /// }
    ///
    /// let mut decoder = uint8().try_map(Version::try_from);
    ///
    /// let version_ok = decoder.decode(&mut BytesMut::from("\x01")).unwrap();
    /// assert_eq!(version_ok, Some(Version::V1));
    ///
    /// let version_err = decoder.decode(&mut BytesMut::from("\x02")).unwrap_err();
    /// assert_eq!(version_err.kind(), std::io::ErrorKind::InvalidData);
    /// ```
    fn try_map<F, B, EE>(self, f: F) -> DecoderTryMap<Self, F, EE>
    where
        F: Fn(A) -> Result<B, EE>,
        Self: Sized,
    {
        DecoderTryMap::new(self, f)
    }

    /// Applies an `B::try_from` `A` conversion over the decoded value when that is `Ok(Some(a))`.
    ///
    /// The conversion can fail and that's handy when we interleave decoding with validation,
    /// for instance, when mapping from a larger domain (e.g. `u8`) into a smaller co-domain (e.g. `Version::v1`).
    /// If you don't need a fallible conversion, then consider [`DecoderExt::map`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// enum Version {
    ///     V1
    /// }
    ///
    /// impl TryFrom<u8> for Version {
    ///     type Error = std::io::Error;
    ///
    ///     fn try_from(value: u8) -> Result<Self, Self::Error> {
    ///             match value {
    ///                 1 => Ok(Version::V1),
    ///                 _ => Err(std::io::Error::from(std::io::ErrorKind::InvalidData))
    ///             }
    ///     }
    /// }
    ///
    /// let mut decoder = uint8().try_map_into::<Version>();
    ///
    /// let version_ok = decoder.decode(&mut BytesMut::from("\x01")).unwrap();
    /// assert_eq!(version_ok, Some(Version::V1));
    ///
    /// let version_err = decoder.decode(&mut BytesMut::from("\x02")).unwrap_err();
    /// assert_eq!(version_err.kind(), std::io::ErrorKind::InvalidData);
    /// ```
    fn try_map_into<B>(self) -> DecoderTryMapInto<Self, B, B::Error>
    where
        B: TryFrom<A>,
        Self: Sized,
    {
        DecoderTryMapInto::new(self)
    }

    /// Applies a function `f` of type `E -> EE` over the decoding error when that is `Err(e)`.
    ///
    /// That's handy when we need to adapt errors across boundaries.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// fn decoder_operation() -> impl Decoder<Item = Operation, Error = std::io::Error> {
    /// #   uint8().try_map(|_| Err(std::io::Error::from(std::io::ErrorKind::Other)))
    /// }
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// enum Operation {
    ///     TurnOff, Turning
    /// }
    ///
    /// # #[derive(Debug, PartialEq, Eq)]
    /// struct OperationError;
    ///
    /// impl From<std::io::Error> for OperationError {
    ///     fn from(value: std::io::Error) -> Self {
    ///         Self
    ///     }
    /// }
    ///
    /// let err = decoder_operation().map_err(|_| OperationError).decode(&mut BytesMut::from("\x00")); // invalid operation number
    /// assert_eq!(err, Err(OperationError));
    /// ```
    fn map_err<F, EE>(self, f: F) -> DecoderMapErr<Self, F>
    where
        F: Fn(E) -> EE,
        Self: Sized,
    {
        DecoderMapErr::new(self, f)
    }

    /// Chains a decoder of `B` on the *remaining* bytes after applying this decoder, then returns a pair of the individual values `(a, b)`.
    ///
    /// This enables the application of decoders in sequence where a step does not depend on its predecessor (when such a dependency exists, consider [`DecoderExt::and_then`].
    ///
    /// The next decoder can return an error type `EE` other than `E` as long as there is an implicit conversion [`From<E>`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::DecoderExt, primitives::uint8};
    ///
    /// let pair = uint8().then(uint8()).decode(&mut BytesMut::from("\x2A\x3B")).unwrap();
    ///
    /// assert_eq!(pair, Some((0x2A, 0x3B)));
    /// ```
    // TODO: Flatten resulting tuple.
    fn then<DNext, B, EE>(self, next: DNext) -> DecoderThen<Self, DNext, A, EE>
    where
        DNext: Decoder<Item = B, Error = EE>,
        EE: From<E>,
        Self: Sized,
    {
        DecoderThen::new(self, next)
    }

    /// Chains a function `f` of type `&A -> Box<Decoder<Item = B, Error = E>>` over the decoded value when that is `Ok(Some(a))`.
    ///
    /// Contrary to [`DecoderExt::map`], the function `f` can decide (dynamically) which decoder to return next according to `a`, which enables complex behaviors
    /// out of simple building blocks by defining dependency relationships between decoders.
    /// e.g. first we decode the header of a message and use that information, say protocol version, to then select the appropriate
    /// decoder among multiple candidates, say one per protocol version, for the body.
    ///
    /// The next decoder can return an error type `EE` other than `E` as long as there is an implicit conversion [`From<E>`].
    ///
    /// The function `f` cannot fail.
    ///
    /// Notice that `f` can't take ownership of the first value `a`, hence the shared borrow, because otherwise it would not be possible to decode incomplete frames
    /// without cloning or maybe saving incoming bytes and re-running this decoder. If you need access to the first value, use [`DecoderAndThen::first_value`]
    /// or [`DecoderAndThen::first_value_as_mut`].
    ///
    /// # Stateful decoders and multi-frames
    ///
    /// Due to the stateful behaviour of this combinator, if you need to decode multiple frames, you'd need to [`DecoderAndThen::reset`] between frames to clean up
    /// the previous value `a` and therefore its influence on `b`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio_util::codec::Decoder;
    /// # use bytes::BytesMut;
    /// use tokio_util_codec_compose::{decode::{adaptors::DecoderBoxed, DecoderExt}, primitives::{uint8, uint16_be, uint16_le}};
    ///
    /// fn payload_for_version(version: &u8) -> DecoderBoxed<u16, std::io::Error> {
    ///     if *version == 0x01 { uint16_be().boxed() } else { uint16_le().boxed() }
    /// }
    ///
    /// let mut decoder = uint8().and_then(payload_for_version);
    ///
    /// let device_big_endian = decoder.decode(&mut BytesMut::from("\x01\x2A\x3B")).unwrap();
    /// assert_eq!(device_big_endian, Some(0x2A3B));
    ///
    /// decoder.reset();
    ///
    /// let device_little_endian = decoder.decode(&mut BytesMut::from("\x00\x2A\x3B")).unwrap();
    /// assert_eq!(device_little_endian, Some(0x3B2A));
    /// ```
    fn and_then<F, DNext, B, EE>(self, f: F) -> DecoderAndThen<Self, F, DNext, A, EE>
    where
        F: Fn(&A) -> DNext,
        DNext: Decoder<Item = B, Error = EE>,
        EE: From<E>,
        Self: Sized,
    {
        DecoderAndThen::new(self, f)
    }

    /// Shorthand for boxing this decoder while also widening its type to ease inference and spelling.
    ///
    /// That's probably useful when combined with [`DecoderExt::and_then`] where the continuation
    /// yields decoders with different types.
    fn boxed(self) -> DecoderBoxed<A, E>
    where
        Self: Sized,
        Self: 'static,
    {
        DecoderBoxed::new(self)
    }
}

impl<D, A, E> DecoderExt<A, E> for D where D: Decoder<Item = A, Error = E> {}