crossfoot_inline/lib.rs
1//! crossfoot-inline: Inline XBRL 1.0/1.1 document sets → XBRL instances, the Inline XBRL
2//! validation rules and the transformation registry (ixt v1–v5, ixt-sec).
3//!
4//! Contract (frozen): `load_document_set`, `InlineSet`, `validate`, `transform::apply`,
5//! `is_inline`.
6//!
7//! Output-instance semantics follow the specification's mapping to target documents:
8//! `Fact::lang` is the `xml:lang` the fact has in the generated target document (copied
9//! from the ix element, an enclosing output tuple or the target root via `ix:references`),
10//! not the language in scope in the XHTML. The XHTML language is still reachable through
11//! `InlineInfo::element`.
12
13pub mod codes;
14mod extract;
15mod registry_data;
16mod schema;
17mod text;
18pub mod transform;
19
20use std::sync::Arc;
21
22use crossfoot_core::{ns, Diagnostic, Document, Dts, Instance};
23
24pub struct InlineSet {
25 /// One instance per ix target (the default target first, `None`).
26 pub instances: Vec<Instance>,
27 pub diags: Vec<Diagnostic>,
28}
29
30/// Is this document meant as an Inline XBRL document: an XHTML root using an ix namespace,
31/// or any root that declares an ix namespace (so that non-XHTML inputs to an inline
32/// document set are reported rather than read as XBRL instances)?
33pub fn is_inline(doc: &Document) -> bool {
34 let Some(r) = doc.document_element() else {
35 return false;
36 };
37 if r.ns_decls()
38 .iter()
39 .any(|(_, u)| &**u == ns::IX11 || &**u == ns::IX10)
40 && !r.is(ns::XBRLI, "xbrl")
41 && !r.is(ns::LINK, "linkbase")
42 && r.local() != "schema"
43 {
44 return true;
45 }
46 if r.local() != "html" {
47 return false;
48 }
49 r.descendant_elements()
50 .any(|n| n.ns() == ns::IX11 || n.ns() == ns::IX10)
51}
52
53/// Load an inline document set. Documents are the XHTML files of one report.
54pub fn load_document_set(docs: Vec<Arc<Document>>) -> InlineSet {
55 extract::load(docs)
56}
57
58/// Inline XBRL validation rules that need the DTS (e.g. nonFraction on numeric concepts).
59pub fn validate(dts: &Dts, set: &InlineSet) -> Vec<Diagnostic> {
60 let mut out = Vec::new();
61 for inst in &set.instances {
62 for f in &inst.facts {
63 let Some(c) = dts.concept(&f.concept) else {
64 continue;
65 };
66 let Some(el) = f.inline.as_ref().and_then(|i| i.element) else {
67 continue;
68 };
69 let n = inst.node(el);
70 match n.local() {
71 "nonFraction" if !c.is_numeric => {
72 out.push(
73 Diagnostic::error(
74 codes::NON_FRACTION_NON_NUMERIC,
75 format!("ix:nonFraction used for non-numeric concept {}", f.concept),
76 )
77 .at(n),
78 );
79 }
80 "fraction" if !c.is_fraction => {
81 out.push(
82 Diagnostic::error(
83 codes::FRACTION_NOT_FRACTION_TYPE,
84 format!(
85 "ix:fraction used for concept {} which is not a fraction type",
86 f.concept
87 ),
88 )
89 .at(n),
90 );
91 }
92 _ => {}
93 }
94 }
95 }
96 out
97}