moq_mux/container/ts/
error.rs1#[derive(Clone, Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9 #[error("transport stream carries {} programs ({})", .programs.len(), list(.programs))]
15 MultiplePrograms {
16 programs: Vec<u16>,
18 },
19
20 #[error("transport stream has no program {program}; its PAT lists {}", list(.programs))]
22 MissingProgram {
23 program: u16,
25 programs: Vec<u16>,
27 },
28
29 #[error("the source {0}; what follows is a new broadcast")]
33 Restarted(super::Step),
34
35 #[error("input ended before {0}")]
37 Empty(&'static str),
38
39 #[error("{0}")]
42 Malformed(String),
43
44 #[error("{0}")]
47 Unsupported(String),
48
49 #[error(transparent)]
51 Mux(Box<crate::Error>),
52}
53
54pub(super) type Result<T, E = Error> = std::result::Result<T, E>;
56
57fn list(programs: &[u16]) -> String {
58 programs.iter().map(u16::to_string).collect::<Vec<_>>().join(", ")
59}
60
61impl From<crate::Error> for Error {
62 fn from(err: crate::Error) -> Self {
64 match err {
65 crate::Error::Ts(err) => err,
66 err => Self::Mux(Box::new(err)),
67 }
68 }
69}
70
71impl From<super::schedule::Error> for Error {
72 fn from(err: super::schedule::Error) -> Self {
73 match err {
74 super::schedule::Error::Unset => Self::Malformed(err.to_string()),
75 err => Self::Unsupported(err.to_string()),
76 }
77 }
78}
79
80impl From<mpeg2ts::Error> for Error {
83 fn from(err: mpeg2ts::Error) -> Self {
84 Self::Malformed(crate::error::message(err))
85 }
86}
87
88macro_rules! mux_errors {
90 ($($ty:ty),* $(,)?) => {
91 $(
92 impl From<$ty> for Error {
93 fn from(err: $ty) -> Self {
94 crate::Error::from(err).into()
95 }
96 }
97 )*
98 };
99}
100
101mux_errors!(
102 moq_net::Error,
103 moq_net::TimeOverflow,
104 hang::Error,
105 crate::codec::aac::Error,
106 crate::codec::aac::adts::Error,
107 crate::codec::annexb::Error,
108 crate::codec::h264::Error,
109 crate::codec::h265::Error,
110 crate::codec::legacy::Error,
111 crate::codec::opus::Error,
112 crate::container::MissingKeyframe,
113 crate::container::TimestampRewind,
114 crate::jitter::Error,
115);
116
117macro_rules! malformed {
119 ($($arg:tt)*) => {
120 $crate::container::ts::Error::Malformed(format!($($arg)*))
121 };
122}
123
124macro_rules! unsupported {
126 ($($arg:tt)*) => {
127 $crate::container::ts::Error::Unsupported(format!($($arg)*))
128 };
129}
130
131macro_rules! bail {
133 ($($arg:tt)*) => {
134 return Err($crate::container::ts::error::malformed!($($arg)*).into())
135 };
136}
137
138macro_rules! ensure {
140 ($cond:expr, $($arg:tt)*) => {
141 if !$cond {
142 $crate::container::ts::error::bail!($($arg)*);
143 }
144 };
145}
146
147macro_rules! bail_unsupported {
149 ($($arg:tt)*) => {
150 return Err($crate::container::ts::error::unsupported!($($arg)*).into())
151 };
152}
153
154macro_rules! ensure_supported {
156 ($cond:expr, $($arg:tt)*) => {
157 if !$cond {
158 $crate::container::ts::error::bail_unsupported!($($arg)*);
159 }
160 };
161}
162
163pub(super) use {bail, bail_unsupported, ensure, ensure_supported, malformed, unsupported};
164
165pub(super) trait Context<T> {
167 fn context(self, msg: &str) -> Result<T>;
169}
170
171impl<T> Context<T> for Option<T> {
172 fn context(self, msg: &str) -> Result<T> {
173 self.ok_or_else(|| Error::Malformed(msg.to_string()))
174 }
175}