1use thiserror::Error;
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8#[derive(Debug, Error, PartialEq, Eq, Clone)]
13#[non_exhaustive]
14pub enum Error {
15 #[error(transparent)]
17 BufferTooShort(#[from] broadcast_core::error::BufferTooShort),
18
19 #[error(transparent)]
21 CrcMismatch(#[from] broadcast_core::error::CrcMismatch),
22
23 #[error("unexpected table_id {table_id:#04x} for {what} (expected one of {expected:?})")]
25 #[non_exhaustive]
26 UnexpectedTableId {
27 table_id: u8,
29 what: &'static str,
31 expected: &'static [u8],
33 },
34
35 #[error("invalid descriptor (tag {tag:#04x}): {reason}")]
37 #[non_exhaustive]
38 InvalidDescriptor {
39 tag: u8,
41 reason: &'static str,
43 },
44
45 #[error("value out of range for {field}: {reason}")]
49 #[non_exhaustive]
50 ValueOutOfRange {
51 field: &'static str,
53 reason: &'static str,
55 },
56
57 #[error("{charset} text needs the `legacy-charsets` feature")]
60 #[non_exhaustive]
61 CharsetNotEnabled {
62 charset: crate::text::LegacyCharset,
64 },
65
66 #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
68 #[non_exhaustive]
69 SectionLengthOverflow {
70 declared: usize,
72 available: usize,
74 },
75
76 #[error("{limit_name} exceeds the configured limit of {limit_bytes} bytes")]
78 #[non_exhaustive]
79 TooLarge {
80 limit_name: &'static str,
82 limit_bytes: u64,
84 },
85
86 #[error(transparent)]
90 ReservedBits(#[from] broadcast_core::error::ReservedBits),
91
92 #[error(transparent)]
94 InvalidValue(#[from] broadcast_core::error::InvalidValue),
95
96 #[error(transparent)]
98 InvalidField(#[from] broadcast_core::error::InvalidField),
99
100 #[error(transparent)]
102 OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
103
104 #[error(transparent)]
106 InvalidSync(#[from] broadcast_core::error::InvalidSync),
107
108 #[error(transparent)]
112 FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
113}
114
115impl Error {
116 #[must_use]
118 pub fn too_large(limit_name: &'static str, limit_bytes: u64) -> Self {
119 Self::TooLarge {
120 limit_name,
121 limit_bytes,
122 }
123 }
124 #[must_use]
126 pub fn invalid_descriptor(tag: u8, reason: &'static str) -> Self {
127 Self::InvalidDescriptor { tag, reason }
128 }
129
130 #[must_use]
132 pub fn section_length_overflow(declared: usize, available: usize) -> Self {
133 Self::SectionLengthOverflow {
134 declared,
135 available,
136 }
137 }
138
139 #[must_use]
141 pub fn value_out_of_range(field: &'static str, reason: &'static str) -> Self {
142 Self::ValueOutOfRange { field, reason }
143 }
144}
145
146impl From<mpeg_ts::Error> for Error {
147 fn from(e: mpeg_ts::Error) -> Self {
148 match e {
149 mpeg_ts::Error::BufferTooShort(e) => Error::BufferTooShort(e),
150 mpeg_ts::Error::CrcMismatch(e) => Error::CrcMismatch(e),
151 mpeg_ts::Error::InvalidSync(e) => Error::InvalidSync(e),
152 mpeg_ts::Error::OutputBufferTooSmall(e) => Error::OutputBufferTooSmall(e),
153 mpeg_ts::Error::SectionLengthOverflow {
154 declared,
155 available,
156 ..
157 } => Error::SectionLengthOverflow {
158 declared,
159 available,
160 },
161 _ => Error::BufferTooShort(broadcast_core::error::BufferTooShort::new(
163 0,
164 0,
165 "mpeg_ts (unknown error)",
166 )),
167 }
168 }
169}