pack-io 2.0.0

Compact binary wire format with schema evolution and zero-copy deserialization for Rust. The serialization substrate under network-protocol and Hive DB.
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Error type for the codec.
//!
//! `pack-io` uses a single, `#[non_exhaustive]` error enum that covers every
//! failure mode of both encode and decode. The built-in encode paths are
//! infallible on the in-memory [`crate::Encoder`]; encoding stays fallible at
//! the type level so streaming encoders can surface `Write` failures and user
//! impls can reject values they cannot represent.
//!
//! The variants are kept small and concrete: each one names a single failure
//! mode and carries the smallest amount of context needed to act on it. None
//! of them include the malformed bytes themselves — error messages from the
//! codec never echo untrusted input back into a log line.

use alloc::borrow::Cow;
use core::fmt;

/// Every error returned by the codec.
///
/// `#[non_exhaustive]` so additional variants can be added in a MINOR release
/// without breaking downstream `match` arms. Callers MUST include a wildcard
/// arm.
///
/// # Examples
///
/// ```
/// use pack_io::{decode, SerialError};
///
/// // A length prefix that runs off the end of the buffer is rejected, not
/// // accepted-and-corrected.
/// let bad: &[u8] = &[0xff, 0xff, 0xff, 0xff, 0x0f]; // varint = u32::MAX
/// match decode::<String>(bad) {
///     Ok(_) => unreachable!("hostile length should not decode"),
///     Err(SerialError::InvalidLength { .. })
///     | Err(SerialError::UnexpectedEof { .. }) => {} // expected
///     Err(other) => panic!("unexpected error variant: {other}"),
/// }
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SerialError {
    /// The decoder needed more bytes than the input contained.
    ///
    /// `needed` is the number of additional bytes the codec required to make
    /// progress; `remaining` is what was actually left in the buffer.
    UnexpectedEof {
        /// Number of bytes the codec required to make progress.
        needed: usize,
        /// Number of bytes still available in the input when the read failed.
        remaining: usize,
    },

    /// A length prefix declared a value larger than the buffer can hold.
    ///
    /// This is the primary defence against a hostile length-prefix attack:
    /// the decoder refuses to allocate or read past the available input.
    /// It is also returned when collection elements that occupy no input
    /// bytes (`Box<()>`, structs whose fields are all `skip`, …) would need
    /// more memory than [`crate::Config::max_alloc`] allows across the whole
    /// decode; `declared` is then that memory in bytes.
    InvalidLength {
        /// The length declared by the prefix, in bytes.
        declared: u64,
        /// Bytes remaining in the input when the prefix was read.
        remaining: usize,
    },

    /// A LEB128 varint exceeded the maximum legal byte count for its target
    /// width (10 bytes for `u64`, 5 for `u32`, 19 for `u128`, etc.).
    VarintOverflow,

    /// A decoded varint did not fit in the requested integer width
    /// (e.g. `u64` decoded successfully but the target was `u32`).
    IntegerOutOfRange,

    /// A boolean byte was neither `0x00` nor `0x01`.
    InvalidBool {
        /// The offending byte. Kept so the caller can log a sanitised summary
        /// (`{:02x}`) without echoing the surrounding payload.
        byte: u8,
    },

    /// A length-prefixed byte run was not valid UTF-8 when decoding a
    /// `String`.
    InvalidUtf8,

    /// A tag byte for `Option` (`0x00` / `0x01`) or `Result` (`0x00` / `0x01`)
    /// was outside the legal range.
    InvalidTag {
        /// Name of the type that owns this tag (`"Option"`, `"Result"`).
        kind: &'static str,
        /// The offending tag byte.
        tag: u8,
    },

    /// A decoded enum variant index did not correspond to any known variant
    /// of the target type. Produced by the `#[derive(Deserialize)]` /
    /// `#[derive(DeserializeView)]` enum deserialisers in `pack-io-derive`.
    ///
    /// `kind` is the enum's type name; `index` is the offending varint
    /// value (read as `u64` so any overflow case is representable).
    UnknownVariant {
        /// Name of the enum that was being decoded.
        kind: &'static str,
        /// The offending varint variant index.
        index: u64,
    },

    /// The input buffer contained trailing bytes after a strict decode
    /// completed. Returned by the whole-buffer entry points
    /// ([`crate::decode`], [`crate::decode_view`], `decode_from` (feature `std`) and
    /// their `_with_config` siblings), which require the payload to be fully
    /// consumed.
    TrailingBytes {
        /// Number of bytes left over after the value was decoded.
        remaining: usize,
    },

    /// An underlying `std::io::Write` / `std::io::Read` operation failed
    /// while a streaming codec was in flight. Returned only by the
    /// `std`-gated I/O integration (`IoEncoder`, `IoDecoder`,
    /// `encode_into`, `decode_from`).
    ///
    /// The error kind and a stringified message are captured so the variant
    /// remains `Clone + Eq`. The original `std::io::Error` is not preserved
    /// — log the captured `message` field for diagnostics.
    #[cfg(feature = "std")]
    Io {
        /// Classification of the underlying I/O failure.
        kind: std::io::ErrorKind,
        /// Human-readable rendering of the original `std::io::Error`.
        message: alloc::string::String,
    },

    /// Decoding nested deeper than [`crate::Config::max_depth`] levels.
    ///
    /// Every derived `Deserialize` / `DeserializeView` impl counts as one
    /// level, so a hostile payload for a recursive type (for example
    /// `struct Node { children: Vec<Node> }`) cannot drive the decoder into
    /// a stack overflow. Raise the limit with
    /// [`crate::Config::with_max_depth`] if legitimate data is deeper than
    /// the default; otherwise treat the input as hostile.
    DepthLimitExceeded {
        /// The configured limit that was hit.
        max_depth: usize,
    },

    /// The input used a valid but non-canonical encoding while the decoder
    /// was in canonical mode ([`crate::Config::with_canonical`]).
    ///
    /// Canonical mode rejects every input that is not the exact byte string
    /// the encoder would produce for the decoded value, so that a hash or
    /// signature over the bytes identifies the value. `reason` names the
    /// rule that was broken: `"varint"` (a varint with redundant zero high
    /// bytes), `"map key order"` / `"set element order"` (entries not in
    /// strictly increasing encoded-key order, which also covers
    /// duplicates), `"versioned struct version"` (a versioned struct
    /// written at a version other than the decoding type's own),
    /// `"versioned body"` (unread bytes inside a versioned struct body), or
    /// `"retired field"` (a retired field's slot holding anything but the
    /// encoding of its default).
    NonCanonical {
        /// The canonical-form rule the input violated.
        reason: &'static str,
    },

    /// A versioned struct carried a schema version the format reserves.
    ///
    /// Version `0` is reserved by the wire format so that an accidental
    /// all-zero buffer is never mistaken for a versioned payload
    /// (`docs/WIRE_FORMAT.md` §3.8). Treat the input as corrupt.
    InvalidVersion {
        /// The offending version number.
        version: u64,
    },

    /// A decoded value was well-formed on the wire but violates the target
    /// type's own invariant — a `char` outside the Unicode scalar range, a
    /// zero for a `NonZero*` integer, or a `Duration` whose nanosecond part
    /// is one second or more.
    InvalidValue {
        /// Name of the target type (`"char"`, `"NonZeroU32"`, `"Duration"`, …).
        kind: &'static str,
    },

    /// A [`crate::Config`] knob was set to a value the codec cannot honour
    /// (for example `max_alloc == 0` or `max_depth == 0`), or canonical mode
    /// on a decoder that cannot check it (a streaming decoder). Returned at
    /// construction time, before any byte is read.
    InvalidConfig {
        /// Which knob was rejected and why.
        reason: &'static str,
    },

    /// An error raised by user code: a hand-written `Serialize` /
    /// `Deserialize` impl, a `#[pack_io(with = "...")]` helper, or a
    /// `serde` type routed through the `serde` adapter.
    ///
    /// Build it with [`SerialError::custom`]. The message is the caller's
    /// responsibility; as with every other variant, never put untrusted
    /// payload bytes or secrets into it.
    Custom {
        /// Human-readable description of the failure.
        message: Cow<'static, str>,
    },
}

impl fmt::Display for SerialError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedEof { needed, remaining } => write!(
                f,
                "unexpected end of input: needed {needed} more byte(s), {remaining} remaining"
            ),
            Self::InvalidLength {
                declared,
                remaining,
            } => write!(
                f,
                "length prefix exceeds remaining buffer: declared {declared}, remaining {remaining}"
            ),
            Self::VarintOverflow => {
                f.write_str("varint exceeds the maximum byte count for its target width")
            }
            Self::IntegerOutOfRange => {
                f.write_str("decoded integer does not fit in the requested width")
            }
            Self::InvalidBool { byte } => write!(f, "invalid boolean byte: 0x{byte:02x}"),
            Self::InvalidUtf8 => f.write_str("length-prefixed bytes were not valid UTF-8"),
            Self::InvalidTag { kind, tag } => write!(f, "invalid {kind} tag: 0x{tag:02x}"),
            Self::UnknownVariant { kind, index } => {
                write!(f, "unknown {kind} variant index: {index}")
            }
            Self::TrailingBytes { remaining } => {
                write!(
                    f,
                    "trailing input after strict decode: {remaining} byte(s) unread"
                )
            }
            Self::DepthLimitExceeded { max_depth } => {
                write!(
                    f,
                    "nesting depth exceeds the configured limit of {max_depth}"
                )
            }
            Self::NonCanonical { reason } => {
                write!(
                    f,
                    "non-canonical encoding rejected in canonical mode: {reason}"
                )
            }
            Self::InvalidVersion { version } => {
                write!(
                    f,
                    "invalid schema version {version} (version 0 is reserved)"
                )
            }
            Self::InvalidValue { kind } => {
                write!(f, "decoded value is not a valid {kind}")
            }
            Self::InvalidConfig { reason } => write!(f, "invalid decoder configuration: {reason}"),
            Self::Custom { message } => f.write_str(message),
            #[cfg(feature = "std")]
            Self::Io { kind, message } => write!(f, "I/O error ({kind:?}): {message}"),
        }
    }
}

impl SerialError {
    /// Build a [`SerialError::Custom`] from any message.
    ///
    /// Accepts a `&'static str` (no allocation) or an owned `String`. Use it
    /// from hand-written `Serialize` / `Deserialize` impls to reject values
    /// the built-in variants do not describe.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Decode, Deserialize, Result, SerialError};
    ///
    /// /// A percentage stored as one byte; anything above 100 is corrupt.
    /// struct Percent(u8);
    ///
    /// impl Deserialize for Percent {
    ///     fn deserialize<D: Decode + ?Sized>(dec: &mut D) -> Result<Self> {
    ///         let raw = u8::deserialize(dec)?;
    ///         if raw > 100 {
    ///             return Err(SerialError::custom("percentage above 100"));
    ///         }
    ///         Ok(Percent(raw))
    ///     }
    /// }
    ///
    /// let err = pack_io::decode::<Percent>(&[101]).err().unwrap();
    /// assert_eq!(err.to_string(), "percentage above 100");
    /// ```
    #[must_use]
    pub fn custom(message: impl Into<Cow<'static, str>>) -> Self {
        Self::Custom {
            message: message.into(),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for SerialError {}

// The `serde` adapter needs `SerialError` to satisfy serde's error traits.
// With pack-io's `std` feature on, the `std::error::Error` impl above covers
// serde's `StdError` bound (pack-io's `std` feature turns on `serde/std`).
// Without it, serde's own `StdError` trait is implemented instead.
#[cfg(all(feature = "serde", not(feature = "std")))]
impl serde::ser::StdError for SerialError {}

#[cfg(feature = "serde")]
impl serde::ser::Error for SerialError {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        use alloc::string::ToString;
        Self::custom(msg.to_string())
    }
}

#[cfg(feature = "serde")]
impl serde::de::Error for SerialError {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        use alloc::string::ToString;
        Self::custom(msg.to_string())
    }
}

/// Convenience alias for `Result<T, SerialError>`.
///
/// Used throughout the codec so the trait surface stays terse. Crates that
/// implement `Serialize` / `Deserialize` for their own types are encouraged to
/// use it as well; nothing in the public API requires it.
///
/// # Examples
///
/// ```
/// use pack_io::Result;
///
/// fn parse_header(_bytes: &[u8]) -> Result<u32> {
///     Ok(0)
/// }
/// ```
pub type Result<T> = core::result::Result<T, SerialError>;

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::format;
    use alloc::string::ToString;

    #[test]
    fn display_unexpected_eof_reports_counts() {
        let err = SerialError::UnexpectedEof {
            needed: 4,
            remaining: 1,
        };
        let msg = err.to_string();
        assert!(msg.contains("needed 4"));
        assert!(msg.contains("1 remaining"));
    }

    #[test]
    fn display_invalid_length_reports_declared_and_remaining() {
        let err = SerialError::InvalidLength {
            declared: 1 << 20,
            remaining: 16,
        };
        let msg = err.to_string();
        assert!(msg.contains("1048576"));
        assert!(msg.contains("16"));
    }

    #[test]
    fn display_invalid_bool_is_hex_with_zero_pad() {
        let err = SerialError::InvalidBool { byte: 0x2a };
        assert_eq!(err.to_string(), "invalid boolean byte: 0x2a");
    }

    #[test]
    fn display_invalid_tag_carries_kind_and_byte() {
        let err = SerialError::InvalidTag {
            kind: "Option",
            tag: 0x7f,
        };
        assert!(err.to_string().contains("Option"));
        assert!(err.to_string().contains("0x7f"));
    }

    #[test]
    fn equality_distinguishes_variants() {
        let a = SerialError::VarintOverflow;
        let b = SerialError::VarintOverflow;
        let c = SerialError::IntegerOutOfRange;
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn clone_preserves_variant() {
        let err = SerialError::TrailingBytes { remaining: 8 };
        let cloned = err.clone();
        assert_eq!(err, cloned);
    }

    #[test]
    fn test_display_of_variants_added_in_1_1() {
        let cases = [
            (
                SerialError::DepthLimitExceeded { max_depth: 7 },
                "nesting depth exceeds the configured limit of 7",
            ),
            (
                SerialError::NonCanonical { reason: "varint" },
                "non-canonical encoding rejected in canonical mode: varint",
            ),
            (
                SerialError::InvalidVersion { version: 0 },
                "invalid schema version 0 (version 0 is reserved)",
            ),
            (
                SerialError::InvalidValue { kind: "char" },
                "decoded value is not a valid char",
            ),
            (
                SerialError::InvalidConfig { reason: "x" },
                "invalid decoder configuration: x",
            ),
            (SerialError::custom("boom"), "boom"),
            (
                SerialError::UnknownVariant {
                    kind: "E",
                    index: 9,
                },
                "unknown E variant index: 9",
            ),
        ];
        for (err, expected) in cases {
            assert_eq!(err.to_string(), expected);
        }
    }

    #[test]
    fn test_custom_accepts_static_and_owned_messages() {
        let a = SerialError::custom("static");
        let b = SerialError::custom(alloc::string::String::from("static"));
        assert_eq!(a, b);
        assert!(matches!(
            a,
            SerialError::Custom {
                message: Cow::Borrowed("static")
            }
        ));
    }

    #[test]
    fn debug_format_does_not_panic() {
        // We never put untrusted bytes into Debug, so it's safe to print.
        let _ = format!("{:?}", SerialError::InvalidUtf8);
    }
}