use std::fmt;
use crate::verify::Category;
use crate::verify::render::Comparison;
#[derive(Debug)]
#[non_exhaustive]
pub enum Refusal {
Unparseable(lopdf::Error),
PasswordRequired,
UndecryptedCryptFilters,
DamagedPageTree,
DamagedResources,
VerificationRegressed(Vec<(Category, usize, usize)>),
BelowSimilarityFloor(Comparison),
Internal(anyhow::Error),
}
impl fmt::Display for Refusal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Refusal::Unparseable(e) => write!(f, "parsing the input: {e}"),
Refusal::PasswordRequired => {
f.write_str("input is encrypted with a password that is needed to open it")
}
Refusal::UndecryptedCryptFilters => f.write_str(
"input is encrypted with crypt filters the parser could not read, so it was not decrypted",
),
Refusal::DamagedPageTree => {
f.write_str("page tree refers to objects the parser could not load")
}
Refusal::DamagedResources => {
f.write_str("page content or resources refer to objects the parser could not load")
}
Refusal::VerificationRegressed(regressions) => write!(
f,
"output failed verification with new problems {regressions:?}; nothing written (this is a bug, please report it)"
),
Refusal::BelowSimilarityFloor(_) => {
f.write_str("pages below the similarity floor; nothing written (--strict)")
}
Refusal::Internal(e) => write!(f, "{e:#}"),
}
}
}
impl std::error::Error for Refusal {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Refusal::Unparseable(e) => Some(e),
Refusal::Internal(e) => Some(e.as_ref()),
_ => None,
}
}
}
impl From<anyhow::Error> for Refusal {
fn from(e: anyhow::Error) -> Self {
Refusal::Internal(e)
}
}