use alloc::string::String;
use core::fmt;
use crate::errors::{
BunsenError,
BunsenErrorKind,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DigestOrigin {
Download {
url: String,
},
AtRest,
Bundled,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DigestMismatch {
pub subject: String,
pub origin: DigestOrigin,
pub expected: String,
pub found: String,
}
impl DigestMismatch {
pub fn new(
subject: impl Into<String>,
origin: DigestOrigin,
expected: impl Into<String>,
found: impl Into<String>,
) -> Self {
Self {
subject: subject.into(),
origin,
expected: expected.into(),
found: found.into(),
}
}
}
impl fmt::Display for DigestMismatch {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(
f,
"{}: sha256 is {}, not the pinned {}",
self.subject, self.found, self.expected
)?;
match &self.origin {
DigestOrigin::Download { url } => write!(
f,
" (downloaded from {url}: the transfer was corrupt, or the source changed)"
),
DigestOrigin::AtRest => f.write_str(" (the file changed after it was verified)"),
DigestOrigin::Bundled => f.write_str(" (bundled)"),
}
}
}
impl core::error::Error for DigestMismatch {}
impl From<DigestMismatch> for BunsenError {
#[track_caller]
fn from(error: DigestMismatch) -> Self {
BunsenError::from_cause(BunsenErrorKind::InvalidResource, error)
}
}