Skip to main content

dvb_ci/
error.rs

1//! Error type for EN 50221 Common Interface parsing/serialization.
2
3/// Result alias for `dvb-ci`.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// A Common Interface parse or serialize error.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// Input shorter than required.
11    #[error("buffer too short: need {need}, have {have} ({what})")]
12    BufferTooShort {
13        /// Bytes required.
14        need: usize,
15        /// Bytes available.
16        have: usize,
17        /// What was being parsed.
18        what: &'static str,
19    },
20    /// Output buffer smaller than `serialized_len()`.
21    #[error("output buffer too small: need {need}, have {have}")]
22    OutputBufferTooSmall {
23        /// Bytes required.
24        need: usize,
25        /// Bytes available.
26        have: usize,
27    },
28    /// An object's 3-byte `apdu_tag` did not match the expected value.
29    #[error("unexpected apdu_tag: got {got:#08X}, expected {expected:#08X} ({what})")]
30    UnexpectedApduTag {
31        /// The tag read from the wire (24-bit, in the low 3 bytes).
32        got: u32,
33        /// The tag the parser expected.
34        expected: u32,
35        /// The object being parsed.
36        what: &'static str,
37    },
38    /// A 1-byte `spdu_tag` did not match the expected value.
39    #[error("unexpected spdu_tag: got {got:#04X}, expected {expected:#04X} ({what})")]
40    UnexpectedSpduTag {
41        /// The tag read from the wire.
42        got: u8,
43        /// The tag the parser expected.
44        expected: u8,
45        /// The object being parsed.
46        what: &'static str,
47    },
48    /// A 1-byte `tpdu_tag` did not match the expected value.
49    #[error("unexpected tpdu_tag: got {got:#04X}, expected {expected:#04X} ({what})")]
50    UnexpectedTpduTag {
51        /// The tag read from the wire.
52        got: u8,
53        /// The tag the parser expected.
54        expected: u8,
55        /// The object being parsed.
56        what: &'static str,
57    },
58    /// The ASN.1-style `length_field` was malformed (truncated, or a
59    /// declared length running past the buffer).
60    #[error("invalid length_field: {0}")]
61    InvalidLength(&'static str),
62    /// A `length_field` encoded a value too large to represent (the spec caps
63    /// any length at 65535, i.e. at most 3 length bytes).
64    #[error("length_field value too large to encode: {0}")]
65    LengthTooLarge(usize),
66    /// A declared `length_field` did not match the actual body length, or the
67    /// body did not fit the fixed shape the object requires.
68    #[error("length mismatch for {what}: declared {declared}, body has {actual}")]
69    LengthMismatch {
70        /// What was being parsed.
71        what: &'static str,
72        /// The `length_field` value.
73        declared: usize,
74        /// The body length actually available.
75        actual: usize,
76    },
77    /// A fixed-shape object had a body length the spec does not allow.
78    #[error("invalid object body for {what}: {reason}")]
79    InvalidObject {
80        /// The object being parsed.
81        what: &'static str,
82        /// Why it is invalid.
83        reason: &'static str,
84    },
85    /// A `resource_identifier()` did not map to any CI-extension resource that
86    /// [`crate::ci_ext::CiExtApdu`] can dispatch.
87    #[error("unknown CI-extension resource: {resource_id:#010X}")]
88    UnknownResource {
89        /// The 32-bit `resource_identifier()`.
90        resource_id: u32,
91    },
92}