use crate::PageId;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResourceKind {
Font,
Image,
DrawParam,
}
impl std::fmt::Display for ResourceKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ResourceKind::Font => write!(f, "font"),
ResourceKind::Image => write!(f, "image"),
ResourceKind::DrawParam => write!(f, "draw param"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum OfdWarning {
MissingFeature { feature: String, entry: String },
SkippedObject { page: PageId, reason: String },
PreservedRaw { entry: String, element: String },
FontSubstituted { requested: String, used: String },
ResourceNotFound { kind: ResourceKind, id: String },
}
impl std::fmt::Display for OfdWarning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OfdWarning::MissingFeature { feature, entry } => {
write!(f, "missing feature '{feature}' in {entry} (skipped)")
}
OfdWarning::SkippedObject { page, reason } => {
write!(f, "skipped object on page {}: {reason}", page.0)
}
OfdWarning::PreservedRaw { entry, element } => {
write!(
f,
"preserved unknown element '{element}' in {entry} verbatim"
)
}
OfdWarning::FontSubstituted { requested, used } => {
write!(f, "font '{requested}' not found; substituted with '{used}'")
}
OfdWarning::ResourceNotFound { kind, id } => {
write!(f, "missing {kind} resource: {id}")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn missing_feature_warning_displays_feature() {
let w = OfdWarning::MissingFeature {
feature: "JBIG2".into(),
entry: "Doc_0/Res/Img_0.xml".into(),
};
assert!(format!("{w:?}").contains("JBIG2"));
}
#[test]
fn warning_display_is_one_line_per_variant() {
let missing = OfdWarning::MissingFeature {
feature: "JBIG2".into(),
entry: "Doc_0/Res/Img_0.xml".into(),
};
assert_eq!(
missing.to_string(),
"missing feature 'JBIG2' in Doc_0/Res/Img_0.xml (skipped)"
);
let skipped = OfdWarning::SkippedObject {
page: PageId::new("3"),
reason: "unrecognized element".into(),
};
assert_eq!(
skipped.to_string(),
"skipped object on page 3: unrecognized element"
);
let substituted = OfdWarning::FontSubstituted {
requested: "SimSun".into(),
used: "default".into(),
};
assert_eq!(
substituted.to_string(),
"font 'SimSun' not found; substituted with 'default'"
);
let resource = OfdWarning::ResourceNotFound {
kind: ResourceKind::Font,
id: "Font_4".into(),
};
assert_eq!(resource.to_string(), "missing font resource: Font_4");
}
#[test]
fn resource_kind_display() {
assert_eq!(ResourceKind::Font.to_string(), "font");
assert_eq!(ResourceKind::Image.to_string(), "image");
assert_eq!(ResourceKind::DrawParam.to_string(), "draw param");
}
}