Skip to main content

net_wire/
error.rs

1//! Shared parsing failures for validated wire-format views.
2
3use core::fmt;
4
5/// Failure to validate bytes required by a wire-format view.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum ParseError {
8    /// The supplied bytes do not contain the required prefix or declared packet length.
9    Truncated {
10        /// Minimum number of bytes required.
11        minimum: usize,
12        /// Number of bytes supplied.
13        available: usize,
14    },
15    /// A packet contains a version other than the one required by its format.
16    InvalidVersion {
17        /// Required version.
18        expected: u8,
19        /// Encoded version.
20        actual: u8,
21    },
22    /// A header length field is smaller than the format minimum.
23    InvalidHeaderLength {
24        /// Minimum valid header length.
25        minimum: usize,
26        /// Encoded header length.
27        actual: usize,
28    },
29    /// A declared packet length is smaller than its validated header.
30    InvalidTotalLength {
31        /// Validated header length.
32        header_length: usize,
33        /// Declared packet length.
34        total_length: usize,
35    },
36}
37
38impl fmt::Display for ParseError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Truncated { minimum, available } => {
42                write!(
43                    f,
44                    "packet is truncated: need {minimum} bytes, have {available}"
45                )
46            }
47            Self::InvalidVersion { expected, actual } => {
48                write!(
49                    f,
50                    "invalid packet version: expected {expected}, got {actual}"
51                )
52            }
53            Self::InvalidHeaderLength { minimum, actual } => {
54                write!(f, "invalid header length: minimum {minimum}, got {actual}")
55            }
56            Self::InvalidTotalLength {
57                header_length,
58                total_length,
59            } => {
60                write!(
61                    f,
62                    "invalid total length: header {header_length}, total {total_length}"
63                )
64            }
65        }
66    }
67}