bitcoin-consensus-encoding 0.2.0

Consensus encoding and decoding
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SPDX-License-Identifier: CC0-1.0

//! Error types for the whole crate.
//!
//! All error types are publicly available at the crate root.
// We separate them into a module so the HTML docs are less cluttered.

use core::convert::Infallible;
use core::fmt;

use internals::write_err;

#[cfg(doc)]
use crate::{ArrayDecoder, Decoder2, Decoder3, Decoder4, Decoder6};
#[cfg(feature = "alloc")]
#[cfg(doc)]
use crate::{ByteVecDecoder, VecDecoder};

/// An error that can occur when reading and decoding from a buffered reader.
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum ReadError<D> {
    /// An I/O error occurred while reading from the reader.
    Io(std::io::Error),
    /// The decoder encountered an error while parsing the data.
    Decode(D),
}

#[cfg(feature = "std")]
impl<D: core::fmt::Display> core::fmt::Display for ReadError<D> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "I/O error: {}", e),
            Self::Decode(e) => write!(f, "decode error: {}", e),
        }
    }
}

#[cfg(feature = "std")]
impl<D> std::error::Error for ReadError<D>
where
    D: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(e) => Some(e),
            Self::Decode(e) => Some(e),
        }
    }
}

#[cfg(feature = "std")]
impl<D> From<std::io::Error> for ReadError<D> {
    fn from(e: std::io::Error) -> Self { Self::Io(e) }
}

/// An error that can occur when decoding from a byte slice.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum DecodeError<Err> {
    /// Provided slice failed to correctly decode as a type.
    Parse(Err),
    /// Bytes remained unconsumed after completing decoding.
    Unconsumed(UnconsumedError),
}

impl<Err> From<Infallible> for DecodeError<Err> {
    fn from(never: Infallible) -> Self { match never {} }
}

impl<Err> fmt::Display for DecodeError<Err>
where
    Err: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Parse(ref e) => write_err!(f, "error parsing encoded object"; e),
            Self::Unconsumed(ref e) => write_err!(f, "unconsumed"; e),
        }
    }
}

#[cfg(feature = "std")]
impl<Err> std::error::Error for DecodeError<Err>
where
    Err: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Parse(ref e) => Some(e),
            Self::Unconsumed(ref e) => Some(e),
        }
    }
}

/// Bytes remained unconsumed after completing decoding.
// This is just to give us the ability to add details in a
// non-breaking way if we want to at some stage.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UnconsumedError();

impl From<Infallible> for UnconsumedError {
    fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for UnconsumedError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "data not consumed entirely when decoding")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UnconsumedError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

/// An error consensus decoding a compact size encoded integer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompactSizeDecoderError(pub(crate) CompactSizeDecoderErrorInner);

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum CompactSizeDecoderErrorInner {
    /// Returned when the decoder reaches end of stream (EOF).
    UnexpectedEof {
        /// How many bytes were required.
        required: usize,
        /// How many bytes were received.
        received: usize,
    },
    /// Returned when the encoding is not minimal
    NonMinimal {
        /// The encoded value.
        value: u64,
    },
    /// Returned when the encoded value exceeds the decoder's limit.
    ValueExceedsLimit(LengthPrefixExceedsMaxError),
}

impl From<Infallible> for CompactSizeDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

impl core::fmt::Display for CompactSizeDecoderError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        use internals::write_err;
        use CompactSizeDecoderErrorInner as E;

        match self.0 {
            E::UnexpectedEof { required: 1, received: 0 } => {
                write!(f, "required at least one byte but the input is empty")
            }
            E::UnexpectedEof { required, received: 0 } => {
                write!(f, "required at least {} bytes but the input is empty", required)
            }
            E::UnexpectedEof { required, received } => write!(
                f,
                "required at least {} bytes but only {} bytes were received",
                required, received
            ),
            E::NonMinimal { value } => write!(f, "the value {} was not encoded minimally", value),
            E::ValueExceedsLimit(ref e) => write_err!(f, "value exceeds limit"; e),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CompactSizeDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use CompactSizeDecoderErrorInner as E;

        match self {
            Self(E::ValueExceedsLimit(ref e)) => Some(e),
            _ => None,
        }
    }
}

/// The error returned when a compact size value exceeds a configured limit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LengthPrefixExceedsMaxError {
    /// The limit that was exceeded.
    pub(crate) limit: usize,
    /// The value that exceeded the limit.
    pub(crate) value: u64,
}

impl From<Infallible> for LengthPrefixExceedsMaxError {
    fn from(never: Infallible) -> Self { match never {} }
}

impl core::fmt::Display for LengthPrefixExceedsMaxError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "decoded length {} exceeds maximum allowed {}", self.value, self.limit)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for LengthPrefixExceedsMaxError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

/// The error returned by the [`ByteVecDecoder`].
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ByteVecDecoderError(pub(crate) ByteVecDecoderErrorInner);

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ByteVecDecoderErrorInner {
    /// Error decoding the byte vector length prefix.
    LengthPrefixDecode(CompactSizeDecoderError),
    /// Not enough bytes given to decoder.
    UnexpectedEof(UnexpectedEofError),
}

#[cfg(feature = "alloc")]
impl From<Infallible> for ByteVecDecoderError {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "alloc")]
impl fmt::Display for ByteVecDecoderError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ByteVecDecoderErrorInner as E;

        match self.0 {
            E::LengthPrefixDecode(ref e) => write_err!(f, "byte vec decoder error"; e),
            E::UnexpectedEof(ref e) => write_err!(f, "byte vec decoder error"; e),
        }
    }
}

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
impl std::error::Error for ByteVecDecoderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use ByteVecDecoderErrorInner as E;

        match self.0 {
            E::LengthPrefixDecode(ref e) => Some(e),
            E::UnexpectedEof(ref e) => Some(e),
        }
    }
}

/// The error returned by the [`VecDecoder`].
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VecDecoderError<Err>(pub(crate) VecDecoderErrorInner<Err>);

#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum VecDecoderErrorInner<Err> {
    /// Error decoding the vector length prefix.
    LengthPrefixDecode(CompactSizeDecoderError),
    /// Error while decoding an item.
    Item(Err),
    /// Not enough bytes given to decoder.
    UnexpectedEof(UnexpectedEofError),
}

#[cfg(feature = "alloc")]
impl<Err> From<Infallible> for VecDecoderError<Err> {
    fn from(never: Infallible) -> Self { match never {} }
}

#[cfg(feature = "alloc")]
impl<Err> fmt::Display for VecDecoderError<Err>
where
    Err: fmt::Display + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use VecDecoderErrorInner as E;

        match self.0 {
            E::LengthPrefixDecode(ref e) => write_err!(f, "vec decoder error"; e),
            E::Item(ref e) => write_err!(f, "vec decoder error"; e),
            E::UnexpectedEof(ref e) => write_err!(f, "vec decoder error"; e),
        }
    }
}

#[cfg(feature = "std")]
impl<Err> std::error::Error for VecDecoderError<Err>
where
    Err: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use VecDecoderErrorInner as E;

        match self.0 {
            E::LengthPrefixDecode(ref e) => Some(e),
            E::Item(ref e) => Some(e),
            E::UnexpectedEof(ref e) => Some(e),
        }
    }
}

/// Not enough bytes given to decoder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnexpectedEofError {
    /// Number of bytes missing to complete decoder.
    pub(crate) missing: usize,
}

impl From<Infallible> for UnexpectedEofError {
    fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for UnexpectedEofError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "not enough bytes for decoder, {} more bytes required", self.missing)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UnexpectedEofError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

/// Helper macro to define an error type for a `DecoderN`.
macro_rules! define_decoder_n_error {
    (
        $(#[$attr:meta])*
        $name:ident;
        $(
            $(#[$err_attr:meta])*
            ($err_wrap:ident, $err_type:ident, $err_msg:literal),
        )*
    ) => {
        $(#[$attr])*
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub enum $name<$($err_type,)*> {
            $(
                $(#[$err_attr])*
                $err_wrap($err_type),
            )*
        }

        impl<$($err_type,)*> fmt::Display for $name<$($err_type,)*>
        where
            $($err_type: fmt::Display,)*
        {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                match self {
                    $(Self::$err_wrap(ref e) => write_err!(f, $err_msg; e),)*
                }
            }
        }

        #[cfg(feature = "std")]
        impl<$($err_type,)*> std::error::Error for $name<$($err_type,)*>
        where
            $($err_type: std::error::Error + 'static,)*
        {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                match self {
                    $(Self::$err_wrap(ref e) => Some(e),)*
                }
            }
        }
    };
}

define_decoder_n_error! {
    /// Error type for [`Decoder2`].
    Decoder2Error;
    /// Error from the first decoder.
    (First, A, "first decoder error."),
    /// Error from the second decoder.
    (Second, B, "second decoder error."),
}

define_decoder_n_error! {
    /// Error type for [`Decoder3`].
    Decoder3Error;
    /// Error from the first decoder.
    (First, A, "first decoder error."),
    /// Error from the second decoder.
    (Second, B, "second decoder error."),
    /// Error from the third decoder.
    (Third, C, "third decoder error."),
}

define_decoder_n_error! {
    /// Error type for [`Decoder4`].
    Decoder4Error;
    /// Error from the first decoder.
    (First, A, "first decoder error."),
    /// Error from the second decoder.
    (Second, B, "second decoder error."),
    /// Error from the third decoder.
    (Third, C, "third decoder error."),
    /// Error from the fourth decoder.
    (Fourth, D, "fourth decoder error."),
}

define_decoder_n_error! {
    /// Error type for [`Decoder6`].
    Decoder6Error;
    /// Error from the first decoder.
    (First, A, "first decoder error."),
    /// Error from the second decoder.
    (Second, B, "second decoder error."),
    /// Error from the third decoder.
    (Third, C, "third decoder error."),
    /// Error from the fourth decoder.
    (Fourth, D, "fourth decoder error."),
    /// Error from the fifth decoder.
    (Fifth, E, "fifth decoder error."),
    /// Error from the sixth decoder.
    (Sixth, F, "sixth decoder error."),
}