use thiserror::Error;
use std::io;
#[derive(Debug, Error, Clone)]
pub enum Error {
#[error("Failed to encode RTP packet: {0}")]
EncodeError(String),
#[error("Failed to decode RTP packet: {0}")]
DecodeError(String),
#[error("Invalid RTP packet format: {0}")]
InvalidPacket(String),
#[error("Buffer too small for RTP packet: need {required} but have {available}")]
BufferTooSmall {
required: usize,
available: usize,
},
#[error("Invalid parameter: {0}")]
InvalidParameter(String),
#[error("IO error: {0}")]
IoError(String),
#[error("RTCP error: {0}")]
RtcpError(String),
#[error("RTP session error: {0}")]
SessionError(String),
#[error("Transport error: {0}")]
Transport(String),
#[error("Parse error: {0}")]
ParseError(String),
#[error("SRTP error: {0}")]
SrtpError(String),
#[error("Statistics error: {0}")]
StatsError(String),
#[error("Timing error: {0}")]
TimingError(String),
#[error("Invalid protocol version: {0}")]
InvalidProtocolVersion(String),
#[error("Unsupported feature: {0}")]
UnsupportedFeature(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
#[error("Invalid state: {0}")]
InvalidState(String),
#[error("DTLS handshake error: {0}")]
DtlsHandshakeError(String),
#[error("DTLS alert received: {0}")]
DtlsAlertReceived(String),
#[error("Certificate validation error: {0}")]
CertificateValidationError(String),
#[error("Cryptographic error: {0}")]
CryptoError(String),
#[error("Invalid message: {0}")]
InvalidMessage(String),
#[error("Authentication failed: {0}")]
AuthenticationFailed(String),
#[error("Negotiation failed: {0}")]
NegotiationFailed(String),
#[error("Packet too short")]
PacketTooShort,
#[error("Operation timed out: {0}")]
Timeout(String),
#[error("Serialization error: {0}")]
SerializationError(String),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::IoError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let encode_err = Error::EncodeError("test error".to_string());
assert_eq!(encode_err.to_string(), "Failed to encode RTP packet: test error");
let buffer_err = Error::BufferTooSmall { required: 100, available: 50 };
assert_eq!(buffer_err.to_string(), "Buffer too small for RTP packet: need 100 but have 50");
let io_err = Error::from(io::Error::new(io::ErrorKind::NotFound, "file not found"));
assert!(io_err.to_string().contains("IO error"));
}
}