1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("codec error: {0}")]
13 Codec(String),
14
15 #[error("RTP error: {0}")]
17 Rtp(String),
18
19 #[error("RTCP error: {0}")]
21 Rtcp(String),
22
23 #[error("SRTP error: {0}")]
25 Srtp(String),
26
27 #[error("device error: {0}")]
29 Device(String),
30
31 #[error("network error: {0}")]
33 Network(#[from] std::io::Error),
34
35 #[error("invalid parameter: {0}")]
37 InvalidParameter(String),
38}
39
40impl Error {
41 pub fn codec(msg: impl Into<String>) -> Self {
43 Self::Codec(msg.into())
44 }
45
46 pub fn rtp(msg: impl Into<String>) -> Self {
48 Self::Rtp(msg.into())
49 }
50
51 pub fn rtcp(msg: impl Into<String>) -> Self {
53 Self::Rtcp(msg.into())
54 }
55
56 pub fn srtp(msg: impl Into<String>) -> Self {
58 Self::Srtp(msg.into())
59 }
60
61 pub fn device(msg: impl Into<String>) -> Self {
63 Self::Device(msg.into())
64 }
65
66 pub fn invalid_parameter(msg: impl Into<String>) -> Self {
68 Self::InvalidParameter(msg.into())
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_error_constructors() {
78 let e = Error::codec("test codec error");
79 assert!(matches!(e, Error::Codec(_)));
80 assert_eq!(format!("{}", e), "codec error: test codec error");
81
82 let e = Error::rtp("test rtp error");
83 assert!(matches!(e, Error::Rtp(_)));
84 assert_eq!(format!("{}", e), "RTP error: test rtp error");
85
86 let e = Error::rtcp("test rtcp error");
87 assert!(matches!(e, Error::Rtcp(_)));
88 assert_eq!(format!("{}", e), "RTCP error: test rtcp error");
89
90 let e = Error::srtp("test srtp error");
91 assert!(matches!(e, Error::Srtp(_)));
92 assert_eq!(format!("{}", e), "SRTP error: test srtp error");
93
94 let e = Error::device("test device error");
95 assert!(matches!(e, Error::Device(_)));
96 assert_eq!(format!("{}", e), "device error: test device error");
97
98 let e = Error::invalid_parameter("bad param");
99 assert!(matches!(e, Error::InvalidParameter(_)));
100 assert_eq!(format!("{}", e), "invalid parameter: bad param");
101 }
102
103 #[test]
104 fn test_error_from_io() {
105 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
106 let e: Error = io_err.into();
107 assert!(matches!(e, Error::Network(_)));
108 assert!(format!("{}", e).contains("file not found"));
109 }
110
111 #[test]
112 fn test_error_debug() {
113 let e = Error::codec("debug test");
114 let debug_str = format!("{:?}", e);
115 assert!(debug_str.contains("Codec"));
116 assert!(debug_str.contains("debug test"));
117 }
118}