use crate::check::Check;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
check: Check,
detail: String,
}
impl Error {
pub fn new(check: Check, detail: impl Into<String>) -> Self {
Error {
check,
detail: detail.into(),
}
}
pub fn check(&self) -> Check {
self.check
}
pub fn check_name(&self) -> &'static str {
self.check.name()
}
pub fn detail(&self) -> &str {
&self.detail
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {}", self.check.name(), self.detail)
}
}
impl std::error::Error for Error {}
impl From<core::str::Utf8Error> for Error {
fn from(err: core::str::Utf8Error) -> Self {
Error::new(Check::MetaJson, err.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::new(Check::MetaJson, err.to_string())
}
}