crossfoot-inline 0.3.0

Inline XBRL 1.1 document sets and the transformation registry (Crossfoot)
Documentation
//! crossfoot-inline: Inline XBRL 1.0/1.1 document sets → XBRL instances, the Inline XBRL
//! validation rules and the transformation registry (ixt v1–v5, ixt-sec).
//!
//! Contract (frozen): `load_document_set`, `InlineSet`, `validate`, `transform::apply`,
//! `is_inline`.
//!
//! Output-instance semantics follow the specification's mapping to target documents:
//! `Fact::lang` is the `xml:lang` the fact has in the generated target document (copied
//! from the ix element, an enclosing output tuple or the target root via `ix:references`),
//! not the language in scope in the XHTML. The XHTML language is still reachable through
//! `InlineInfo::element`.

pub mod codes;
mod extract;
mod registry_data;
mod schema;
pub mod sec_tr;
mod sec_tr_data;
mod text;
pub mod transform;
mod xhtml_attrs;

use std::sync::Arc;

use crossfoot_core::{ns, Diagnostic, Document, Dts, Instance};

pub struct InlineSet {
    /// One instance per ix target (the default target first, `None`).
    pub instances: Vec<Instance>,
    pub diags: Vec<Diagnostic>,
}

/// Is this document meant as an Inline XBRL document: an XHTML root using an ix namespace,
/// or any root that declares an ix namespace (so that non-XHTML inputs to an inline
/// document set are reported rather than read as XBRL instances)?
pub fn is_inline(doc: &Document) -> bool {
    let Some(r) = doc.document_element() else {
        return false;
    };
    if r.ns_decls()
        .iter()
        .any(|(_, u)| &**u == ns::IX11 || &**u == ns::IX10)
        && !r.is(ns::XBRLI, "xbrl")
        && !r.is(ns::LINK, "linkbase")
        && r.local() != "schema"
    {
        return true;
    }
    if r.local() != "html" {
        return false;
    }
    r.descendant_elements()
        .any(|n| n.ns() == ns::IX11 || n.ns() == ns::IX10)
}

/// Load an inline document set. Documents are the XHTML files of one report.
pub fn load_document_set(docs: Vec<Arc<Document>>) -> InlineSet {
    extract::load(docs)
}

/// Inline XBRL validation rules that need the DTS (e.g. nonFraction on numeric concepts).
pub fn validate(dts: &Dts, set: &InlineSet) -> Vec<Diagnostic> {
    let mut out = Vec::new();
    for inst in &set.instances {
        for f in &inst.facts {
            let Some(c) = dts.concept(&f.concept) else {
                continue;
            };
            let Some(el) = f.inline.as_ref().and_then(|i| i.element) else {
                continue;
            };
            let n = inst.node(el);
            match n.local() {
                "nonFraction" if !c.is_numeric => {
                    out.push(
                        Diagnostic::error(
                            codes::NON_FRACTION_NON_NUMERIC,
                            format!("ix:nonFraction used for non-numeric concept {}", f.concept),
                        )
                        .at(n),
                    );
                }
                "fraction" if !c.is_fraction => {
                    out.push(
                        Diagnostic::error(
                            codes::FRACTION_NOT_FRACTION_TYPE,
                            format!(
                                "ix:fraction used for concept {} which is not a fraction type",
                                f.concept
                            ),
                        )
                        .at(n),
                    );
                }
                _ => {}
            }
        }
    }
    out
}