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(transparent)]
12 BufferTooShort(#[from] broadcast_core::error::BufferTooShort),
13 /// Output buffer smaller than `serialized_len()`.
14 #[error(transparent)]
15 OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
16 /// An object's 3-byte `apdu_tag` did not match the expected value.
17 #[error("unexpected apdu_tag: got {got:#08X}, expected {expected:#08X} ({what})")]
18 #[non_exhaustive]
19 UnexpectedApduTag {
20 /// The tag read from the wire (24-bit, in the low 3 bytes).
21 got: u32,
22 /// The tag the parser expected.
23 expected: u32,
24 /// The object being parsed.
25 what: &'static str,
26 },
27 /// A 1-byte `spdu_tag` did not match the expected value.
28 #[error("unexpected spdu_tag: got {got:#04X}, expected {expected:#04X} ({what})")]
29 #[non_exhaustive]
30 UnexpectedSpduTag {
31 /// The tag read from the wire.
32 got: u8,
33 /// The tag the parser expected.
34 expected: u8,
35 /// The object being parsed.
36 what: &'static str,
37 },
38 /// A 1-byte `tpdu_tag` did not match the expected value.
39 #[error("unexpected tpdu_tag: got {got:#04X}, expected {expected:#04X} ({what})")]
40 #[non_exhaustive]
41 UnexpectedTpduTag {
42 /// The tag read from the wire.
43 got: u8,
44 /// The tag the parser expected.
45 expected: u8,
46 /// The object being parsed.
47 what: &'static str,
48 },
49 /// The ASN.1-style `length_field` was malformed (truncated, or a
50 /// declared length running past the buffer).
51 #[error("invalid length_field: {0}")]
52 InvalidLength(&'static str),
53 /// A `length_field` encoded a value too large to represent (the spec caps
54 /// any length at 65535, i.e. at most 3 length bytes).
55 #[error("length_field value too large to encode: {0}")]
56 LengthTooLarge(usize),
57 /// A declared `length_field` did not match the actual body length, or the
58 /// body did not fit the fixed shape the object requires.
59 #[error(transparent)]
60 LengthMismatch(#[from] broadcast_core::error::LengthMismatch),
61 /// A fixed-shape object had a body length the spec does not allow.
62 #[error("invalid object body for {what}: {reason}")]
63 #[non_exhaustive]
64 InvalidObject {
65 /// The object being parsed.
66 what: &'static str,
67 /// Why it is invalid.
68 reason: &'static str,
69 },
70 /// A `resource_identifier()` did not map to any CI-extension resource that
71 /// [`crate::ci_ext::CiExtApdu`] can dispatch.
72 #[error("unknown CI-extension resource: {resource_id:#010X}")]
73 #[non_exhaustive]
74 UnknownResource {
75 /// The 32-bit `resource_identifier()`.
76 resource_id: u32,
77 },
78}
79
80impl Error {
81 /// Builds [`Self::UnexpectedTpduTag`].
82 #[must_use]
83 pub fn unexpected_tpdu_tag(got: u8, expected: u8, what: &'static str) -> Self {
84 Self::UnexpectedTpduTag {
85 got,
86 expected,
87 what,
88 }
89 }
90}