1use std::fmt;
4use std::io;
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
11pub enum Error {
12 Io(io::Error),
14 Protocol(ProtocolError),
16 Amf(AmfError),
18 Handshake(HandshakeError),
20 Media(MediaError),
22 Rejected(String),
24 Timeout,
26 ConnectionClosed,
28 Config(String),
30}
31
32impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 Error::Io(e) => write!(f, "I/O error: {}", e),
36 Error::Protocol(e) => write!(f, "Protocol error: {}", e),
37 Error::Amf(e) => write!(f, "AMF error: {}", e),
38 Error::Handshake(e) => write!(f, "Handshake error: {}", e),
39 Error::Media(e) => write!(f, "Media error: {}", e),
40 Error::Rejected(msg) => write!(f, "Connection rejected: {}", msg),
41 Error::Timeout => write!(f, "Operation timed out"),
42 Error::ConnectionClosed => write!(f, "Connection closed"),
43 Error::Config(msg) => write!(f, "Configuration error: {}", msg),
44 }
45 }
46}
47
48impl std::error::Error for Error {
49 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50 match self {
51 Error::Io(e) => Some(e),
52 _ => None,
53 }
54 }
55}
56
57impl From<io::Error> for Error {
58 fn from(err: io::Error) -> Self {
59 Error::Io(err)
60 }
61}
62
63impl From<ProtocolError> for Error {
64 fn from(err: ProtocolError) -> Self {
65 Error::Protocol(err)
66 }
67}
68
69impl From<AmfError> for Error {
70 fn from(err: AmfError) -> Self {
71 Error::Amf(err)
72 }
73}
74
75impl From<HandshakeError> for Error {
76 fn from(err: HandshakeError) -> Self {
77 Error::Handshake(err)
78 }
79}
80
81impl From<MediaError> for Error {
82 fn from(err: MediaError) -> Self {
83 Error::Media(err)
84 }
85}
86
87#[derive(Debug)]
89pub enum ProtocolError {
90 InvalidChunkHeader,
91 UnknownMessageType(u8),
92 MessageTooLarge { size: u32, max: u32 },
93 InvalidChunkStreamId(u32),
94 UnexpectedMessage(String),
95 MissingField(String),
96 InvalidCommand(String),
97 StreamNotFound(u32),
98}
99
100impl fmt::Display for ProtocolError {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 match self {
103 ProtocolError::InvalidChunkHeader => write!(f, "Invalid chunk header"),
104 ProtocolError::UnknownMessageType(t) => write!(f, "Unknown message type: {}", t),
105 ProtocolError::MessageTooLarge { size, max } => {
106 write!(f, "Message too large: {} bytes (max {})", size, max)
107 }
108 ProtocolError::InvalidChunkStreamId(id) => write!(f, "Invalid chunk stream ID: {}", id),
109 ProtocolError::UnexpectedMessage(msg) => write!(f, "Unexpected message: {}", msg),
110 ProtocolError::MissingField(field) => write!(f, "Missing required field: {}", field),
111 ProtocolError::InvalidCommand(cmd) => write!(f, "Invalid command: {}", cmd),
112 ProtocolError::StreamNotFound(id) => write!(f, "Stream not found: {}", id),
113 }
114 }
115}
116
117impl std::error::Error for ProtocolError {}
118
119#[derive(Debug)]
121pub enum AmfError {
122 UnknownMarker(u8),
123 UnexpectedEof,
124 InvalidUtf8,
125 InvalidReference(u16),
126 NestingTooDeep,
127 InvalidObjectEnd,
128}
129
130impl fmt::Display for AmfError {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 match self {
133 AmfError::UnknownMarker(m) => write!(f, "Unknown AMF marker: 0x{:02x}", m),
134 AmfError::UnexpectedEof => write!(f, "Unexpected end of AMF data"),
135 AmfError::InvalidUtf8 => write!(f, "Invalid UTF-8 in AMF string"),
136 AmfError::InvalidReference(idx) => write!(f, "Invalid AMF reference: {}", idx),
137 AmfError::NestingTooDeep => write!(f, "AMF nesting too deep"),
138 AmfError::InvalidObjectEnd => write!(f, "Invalid object end marker"),
139 }
140 }
141}
142
143impl std::error::Error for AmfError {}
144
145#[derive(Debug)]
147pub enum HandshakeError {
148 InvalidVersion(u8),
149 DigestMismatch,
150 InvalidState,
151 ResponseMismatch,
152}
153
154impl fmt::Display for HandshakeError {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 match self {
157 HandshakeError::InvalidVersion(v) => write!(f, "Invalid RTMP version: {}", v),
158 HandshakeError::DigestMismatch => write!(f, "Handshake digest mismatch"),
159 HandshakeError::InvalidState => write!(f, "Invalid handshake state"),
160 HandshakeError::ResponseMismatch => write!(f, "Handshake response mismatch"),
161 }
162 }
163}
164
165impl std::error::Error for HandshakeError {}
166
167#[derive(Debug)]
169pub enum MediaError {
170 InvalidFlvTag,
171 InvalidAvcPacket,
172 InvalidAacPacket,
173 UnsupportedCodec(String),
174 InvalidNalu,
175 MissingSequenceHeader,
176}
177
178impl fmt::Display for MediaError {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 match self {
181 MediaError::InvalidFlvTag => write!(f, "Invalid FLV tag"),
182 MediaError::InvalidAvcPacket => write!(f, "Invalid AVC packet"),
183 MediaError::InvalidAacPacket => write!(f, "Invalid AAC packet"),
184 MediaError::UnsupportedCodec(c) => write!(f, "Unsupported codec: {}", c),
185 MediaError::InvalidNalu => write!(f, "Invalid NAL unit"),
186 MediaError::MissingSequenceHeader => write!(f, "Missing sequence header"),
187 }
188 }
189}
190
191impl std::error::Error for MediaError {}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196 use std::error::Error as StdError;
197 use std::io;
198
199 #[test]
200 fn test_error_display() {
201 let io_err = io::Error::new(io::ErrorKind::ConnectionReset, "connection reset");
203 let err = Error::Io(io_err);
204 assert!(err.to_string().contains("I/O error"));
205
206 let err = Error::Protocol(ProtocolError::InvalidChunkHeader);
208 assert!(err.to_string().contains("Protocol error"));
209 assert!(err.to_string().contains("Invalid chunk header"));
210
211 let err = Error::Amf(AmfError::UnknownMarker(0xFF));
213 assert!(err.to_string().contains("AMF error"));
214 assert!(err.to_string().contains("0xff"));
215
216 let err = Error::Handshake(HandshakeError::InvalidVersion(5));
218 assert!(err.to_string().contains("Handshake error"));
219 assert!(err.to_string().contains("5"));
220
221 let err = Error::Media(MediaError::UnsupportedCodec("VP9".into()));
223 assert!(err.to_string().contains("Media error"));
224 assert!(err.to_string().contains("VP9"));
225
226 let err = Error::Rejected("stream key invalid".into());
228 assert!(err.to_string().contains("Connection rejected"));
229 assert!(err.to_string().contains("stream key invalid"));
230
231 let err = Error::Timeout;
233 assert!(err.to_string().contains("timed out"));
234
235 let err = Error::ConnectionClosed;
237 assert!(err.to_string().contains("closed"));
238
239 let err = Error::Config("invalid port".into());
241 assert!(err.to_string().contains("Configuration error"));
242 }
243
244 #[test]
245 fn test_error_source() {
246 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
248 let err = Error::Io(io_err);
249 assert!(StdError::source(&err).is_some());
250
251 let err = Error::Protocol(ProtocolError::InvalidChunkHeader);
253 assert!(StdError::source(&err).is_none());
254
255 let err = Error::Timeout;
256 assert!(StdError::source(&err).is_none());
257 }
258
259 #[test]
260 fn test_from_conversions() {
261 let io_err = io::Error::new(io::ErrorKind::TimedOut, "timeout");
263 let err: Error = io_err.into();
264 assert!(matches!(err, Error::Io(_)));
265
266 let proto_err = ProtocolError::MessageTooLarge { size: 100, max: 50 };
268 let err: Error = proto_err.into();
269 assert!(matches!(err, Error::Protocol(_)));
270
271 let amf_err = AmfError::UnexpectedEof;
273 let err: Error = amf_err.into();
274 assert!(matches!(err, Error::Amf(_)));
275
276 let hs_err = HandshakeError::DigestMismatch;
278 let err: Error = hs_err.into();
279 assert!(matches!(err, Error::Handshake(_)));
280
281 let media_err = MediaError::InvalidFlvTag;
283 let err: Error = media_err.into();
284 assert!(matches!(err, Error::Media(_)));
285 }
286
287 #[test]
288 fn test_protocol_error_display() {
289 assert!(ProtocolError::InvalidChunkHeader
290 .to_string()
291 .contains("Invalid chunk header"));
292
293 assert!(ProtocolError::UnknownMessageType(99)
294 .to_string()
295 .contains("99"));
296
297 let err = ProtocolError::MessageTooLarge {
298 size: 1000,
299 max: 500,
300 };
301 assert!(err.to_string().contains("1000"));
302 assert!(err.to_string().contains("500"));
303
304 assert!(ProtocolError::InvalidChunkStreamId(123)
305 .to_string()
306 .contains("123"));
307
308 assert!(ProtocolError::UnexpectedMessage("test".into())
309 .to_string()
310 .contains("test"));
311
312 assert!(ProtocolError::MissingField("app".into())
313 .to_string()
314 .contains("app"));
315
316 assert!(ProtocolError::InvalidCommand("bad".into())
317 .to_string()
318 .contains("bad"));
319
320 assert!(ProtocolError::StreamNotFound(5).to_string().contains("5"));
321 }
322
323 #[test]
324 fn test_amf_error_display() {
325 assert!(AmfError::UnknownMarker(0xAB).to_string().contains("0xab"));
326
327 assert!(AmfError::UnexpectedEof.to_string().contains("end of AMF"));
328
329 assert!(AmfError::InvalidUtf8.to_string().contains("UTF-8"));
330
331 assert!(AmfError::InvalidReference(42).to_string().contains("42"));
332
333 assert!(AmfError::NestingTooDeep.to_string().contains("deep"));
334
335 assert!(AmfError::InvalidObjectEnd.to_string().contains("end"));
336 }
337
338 #[test]
339 fn test_handshake_error_display() {
340 assert!(HandshakeError::InvalidVersion(10)
341 .to_string()
342 .contains("10"));
343
344 assert!(HandshakeError::DigestMismatch
345 .to_string()
346 .contains("digest"));
347
348 assert!(HandshakeError::InvalidState.to_string().contains("state"));
349
350 assert!(HandshakeError::ResponseMismatch
351 .to_string()
352 .contains("response"));
353 }
354
355 #[test]
356 fn test_media_error_display() {
357 assert!(MediaError::InvalidFlvTag.to_string().contains("FLV"));
358 assert!(MediaError::InvalidAvcPacket.to_string().contains("AVC"));
359 assert!(MediaError::InvalidAacPacket.to_string().contains("AAC"));
360 assert!(MediaError::UnsupportedCodec("HEVC".into())
361 .to_string()
362 .contains("HEVC"));
363 assert!(MediaError::InvalidNalu.to_string().contains("NAL"));
364 assert!(MediaError::MissingSequenceHeader
365 .to_string()
366 .contains("sequence"));
367 }
368}