use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
message: String,
}
impl Error {
pub(crate) fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind, self.message)
}
}
impl std::error::Error for Error {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
Xml,
MissingAttribute {
element: &'static str,
attribute: &'static str,
},
MissingElement {
parent: &'static str,
element: &'static str,
},
InvalidAttributeValue { attribute: &'static str },
InvalidDuration,
UnexpectedStructure,
InvalidXPath,
PatchFailed,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Xml => write!(f, "XML parse error"),
Self::MissingAttribute { element, attribute } => {
write!(f, "missing required attribute '{attribute}' on <{element}>")
}
Self::MissingElement { parent, element } => {
write!(f, "missing required element <{element}> in <{parent}>")
}
Self::InvalidAttributeValue { attribute } => {
write!(f, "invalid value for attribute '{attribute}'")
}
Self::InvalidDuration => write!(f, "invalid ISO 8601 duration"),
Self::UnexpectedStructure => write!(f, "unexpected MPD structure"),
Self::InvalidXPath => write!(f, "invalid XPath selector"),
Self::PatchFailed => write!(f, "patch operation failed"),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;