use core::fmt;
use crate::exi::ExiError;
use crate::v2gtp::V2gtpError;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Exi(ExiError),
V2gtp(V2gtpError),
#[cfg(feature = "sdp")]
#[cfg_attr(docsrs, doc(cfg(feature = "sdp")))]
Sdp(crate::sdp::SdpError),
InvalidValue(&'static str),
TooManyItems {
field: &'static str,
count: usize,
max: usize,
},
TooFewItems {
field: &'static str,
count: usize,
min: usize,
},
}
impl From<ExiError> for Error {
fn from(e: ExiError) -> Self {
Self::Exi(e)
}
}
impl From<V2gtpError> for Error {
fn from(e: V2gtpError) -> Self {
Self::V2gtp(e)
}
}
#[cfg(feature = "sdp")]
impl From<crate::sdp::SdpError> for Error {
fn from(e: crate::sdp::SdpError) -> Self {
Self::Sdp(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Exi(e) => write!(f, "EXI: {e}"),
Self::V2gtp(e) => write!(f, "V2GTP: {e}"),
#[cfg(feature = "sdp")]
Self::Sdp(e) => write!(f, "SDP: {e}"),
Self::InvalidValue(field) => write!(f, "invalid value for {field}"),
Self::TooManyItems { field, count, max } => {
write!(f, "{field} has {count} entries, the schema allows at most {max}")
}
Self::TooFewItems { field, count, min } => {
write!(f, "{field} has {count} entries, the schema requires at least {min}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Exi(e) => Some(e),
Self::V2gtp(e) => Some(e),
#[cfg(feature = "sdp")]
Self::Sdp(e) => Some(e),
_ => None,
}
}
}