rofd-dom 0.1.7

OFD (GB/T 33190) document model — page body + annotation data structures for the rofd editor.
Documentation
//! Degraded-load warnings. Pure data model types (no ZIP/XML deps).
//!
//! `OfdWarning` represents non-fatal issues encountered while parsing an OFD
//! package (e.g. unmodelled features, missing resources). The io layer emits
//! these into [`LoadReport::warnings`](crate::LoadReport); the component layer
//! fires them to the host via the `on_warning` callback.
//!
//! Defined in dom (not io) so that both io (parse) and component (callback)
//! can reference the type without component depending on io (AGENTS.md §4.1).

use crate::PageId;

/// Kind of resource referenced by the document.
#[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"),
        }
    }
}

/// A non-fatal issue encountered while parsing an OFD package. Parse continues;
/// the warning is surfaced to the host via the `on_warning` callback
/// (AGENTS.md §4.6 - degraded input path, distinct from `OfdError`).
#[derive(Debug, Clone, PartialEq)]
pub enum OfdWarning {
    /// An OFD feature not modelled in v1 (e.g. templates, JBIG2). Parse
    /// continues with the feature skipped.
    MissingFeature { feature: String, entry: String },
    /// An unknown/unmodelled object was skipped on a page (e.g. an unrecognized
    /// XML element). Parse continues without it.
    SkippedObject { page: PageId, reason: String },
    /// An unrecognized element was preserved verbatim as a raw passthrough
    /// fragment (spec 2026-11-11 D3). Parse continues; the fragment
    /// round-trips byte-for-byte on save.
    PreservedRaw { entry: String, element: String },
    /// A requested font was not found in the package; a default font is used.
    FontSubstituted { requested: String, used: String },
    /// A resource (image/font) referenced by the document is missing from the
    /// package. Parse continues; the resource is simply unavailable.
    ResourceNotFound { kind: ResourceKind, id: String },
}

/// One human-readable line per variant. Shared formatter for every
/// warning consumer (the wasm event bridge, the convert CLI); wording
/// mirrors the original web-view private formatter verbatim so no
/// consumer sees a message change.
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() {
        // Exact strings pinned: these are the messages hosts show users
        // (web-view on_warning, the convert CLI stderr loop).
        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");
    }
}