use std::{fmt, io, str::Utf8Error};
#[derive(Debug)]
pub enum Error {
IO(io::Error),
Utf8(Utf8Error),
MissingHeader(&'static str),
InvalidHeader(&'static str),
ParseSearchTargetError(ParseSearchTargetError),
InvalidHTTP(&'static str),
HTTPError(u32),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::IO(err) => write!(f, "io error: {err}"),
Error::Utf8(err) => write!(f, "utf8 decoding error: {err}"),
Error::MissingHeader(err) => write!(f, "missing header: {err}"),
Error::InvalidHeader(err) => write!(f, "invalid header: {err}"),
Error::ParseSearchTargetError(err) => write!(f, "{err}"),
Error::InvalidHTTP(err) => write!(f, "failed to parse http response: {err}"),
Error::HTTPError(err) => write!(
f,
"control point responded with non-zero exit code: {err}"
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::IO(err) => Some(err),
Error::Utf8(err) => Some(err),
Error::ParseSearchTargetError(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::IO(err)
}
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Error::Utf8(err)
}
}
impl From<ParseSearchTargetError> for Error {
fn from(err: ParseSearchTargetError) -> Self {
Error::ParseSearchTargetError(err)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct ParseURNError;
impl std::error::Error for ParseURNError {}
impl fmt::Display for ParseURNError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to parse URN")
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum ParseSearchTargetError {
#[allow(clippy::upper_case_acronyms)]
URN(ParseURNError),
ST,
}
impl fmt::Display for ParseSearchTargetError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseSearchTargetError::URN(_) => write!(f, "invalid urn supplied"),
ParseSearchTargetError::ST => write!(f, "invalid search target format"),
}
}
}
impl std::error::Error for ParseSearchTargetError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let ParseSearchTargetError::URN(err) = self {
Some(err)
} else {
None
}
}
}