use core::{
fmt::{Display, Formatter, Result as FmtResult},
result::Result as StdResult,
};
#[cfg(feature = "std")]
use {
hex::FromHexError as HexError,
std::{error::Error as StdError, io::Error as IoError},
url::{ParseError as UrlError, Url},
};
pub(crate) type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "std")]
InvalidScheme(Url),
#[cfg(feature = "std")]
MissingObjectType(Url),
#[cfg(feature = "std")]
MissingHashAlgorithm(Url),
#[cfg(feature = "std")]
MissingHash(Url),
UnknownObjectType,
MismatchedObjectType { expected: &'static str },
MismatchedHashAlgorithm { expected: &'static str },
UnexpectedHashLength { expected: usize, observed: usize },
UnexpectedReadLength { expected: usize, observed: usize },
#[cfg(feature = "std")]
InvalidHex(HexError),
#[cfg(feature = "std")]
Url(UrlError),
#[cfg(feature = "std")]
Io(IoError),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
#[cfg(feature = "std")]
Error::InvalidScheme(url) => write!(f, "invalid scheme in URL '{}'", url.scheme()),
#[cfg(feature = "std")]
Error::MissingObjectType(url) => write!(f, "missing object type in URL '{}'", url),
#[cfg(feature = "std")]
Error::MissingHashAlgorithm(url) => {
write!(f, "missing hash algorithm in URL '{}'", url)
}
#[cfg(feature = "std")]
Error::MissingHash(url) => write!(f, "missing hash in URL '{}'", url),
Error::UnknownObjectType => write!(f, "unknown object type"),
Error::MismatchedObjectType { expected } => {
write!(f, "mismatched object type; expected '{}'", expected,)
}
Error::MismatchedHashAlgorithm { expected } => {
write!(f, "mismatched hash algorithm; expected '{}'", expected)
}
Error::UnexpectedHashLength { expected, observed } => {
write!(
f,
"unexpected hash length; expected '{}', got '{}'",
expected, observed
)
}
Error::UnexpectedReadLength { expected, observed } => {
write!(
f,
"unexpected read length; expected '{}', got '{}'",
expected, observed
)
}
#[cfg(feature = "std")]
Error::InvalidHex(_) => write!(f, "invalid hex string"),
#[cfg(feature = "std")]
Error::Url(e) => write!(f, "{}", e),
#[cfg(feature = "std")]
Error::Io(e) => write!(f, "{}", e),
}
}
}
#[cfg(feature = "std")]
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
#[cfg(feature = "std")]
Error::InvalidScheme(_) => None,
#[cfg(feature = "std")]
Error::MissingObjectType(_) => None,
#[cfg(feature = "std")]
Error::MissingHashAlgorithm(_) => None,
#[cfg(feature = "std")]
Error::MissingHash(_) => None,
Error::UnknownObjectType
| Error::MismatchedObjectType { .. }
| Error::MismatchedHashAlgorithm { .. }
| Error::UnexpectedHashLength { .. }
| Error::UnexpectedReadLength { .. } => None,
#[cfg(feature = "std")]
Error::InvalidHex(e) => Some(e),
#[cfg(feature = "std")]
Error::Url(e) => Some(e),
#[cfg(feature = "std")]
Error::Io(e) => Some(e),
}
}
}
#[cfg(feature = "std")]
impl From<HexError> for Error {
fn from(e: HexError) -> Error {
Error::InvalidHex(e)
}
}
#[cfg(feature = "std")]
impl From<UrlError> for Error {
fn from(e: UrlError) -> Error {
Error::Url(e)
}
}
#[cfg(feature = "std")]
impl From<IoError> for Error {
fn from(e: IoError) -> Error {
Error::Io(e)
}
}