Skip to main content

net_wire/tls/
error.rs

1//! TLS parsing and construction errors.
2
3use core::fmt;
4
5/// Failure to validate a TLS wire layout.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum TlsParseError {
8    /// Fewer bytes than a fixed prefix or declared vector were supplied.
9    Incomplete {
10        /// Required bytes.
11        required: usize,
12        /// Available bytes.
13        available: usize,
14    },
15    /// A declared vector or value violates its format constraints.
16    InvalidValue,
17    /// A structured payload has trailing bytes after its required fields.
18    TrailingBytes,
19}
20
21impl fmt::Display for TlsParseError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::Incomplete {
25                required,
26                available,
27            } => write!(
28                f,
29                "TLS input is incomplete: need {required} bytes, have {available}"
30            ),
31            Self::InvalidValue => f.write_str("invalid TLS vector or value"),
32            Self::TrailingBytes => f.write_str("TLS payload has trailing bytes"),
33        }
34    }
35}
36
37/// Failure to construct TLS bytes in caller-provided storage.
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub enum TlsBuildError {
40    /// An input vector or scalar violates the TLS encoding rules.
41    InvalidValue,
42    /// A requested length cannot be represented by the TLS field.
43    LengthTooLarge,
44    /// The caller buffer cannot contain the finished encoding.
45    BufferTooShort {
46        /// Required bytes.
47        required: usize,
48        /// Available bytes.
49        available: usize,
50    },
51}
52
53impl fmt::Display for TlsBuildError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            Self::InvalidValue => f.write_str("invalid TLS builder value"),
57            Self::LengthTooLarge => f.write_str("TLS length is not representable"),
58            Self::BufferTooShort {
59                required,
60                available,
61            } => write!(
62                f,
63                "TLS buffer is too short: need {required} bytes, have {available}"
64            ),
65        }
66    }
67}