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;
17pub mod sec_tr;
18mod sec_tr_data;
19mod text;
20pub mod transform;
21mod xhtml_attrs;
22
23use std::sync::Arc;
24
25use crossfoot_core::{ns, Diagnostic, Document, Dts, Instance};
26
27pub struct InlineSet {
28 /// One instance per ix target (the default target first, `None`).
29 pub instances: Vec<Instance>,
30 pub diags: Vec<Diagnostic>,
31}
32
33/// Is this document meant as an Inline XBRL document: an XHTML root using an ix namespace,
34/// or any root that declares an ix namespace (so that non-XHTML inputs to an inline
35/// document set are reported rather than read as XBRL instances)?
36pub fn is_inline(doc: &Document) -> bool {
37 let Some(r) = doc.document_element() else {
38 return false;
39 };
40 if r.ns_decls()
41 .iter()
42 .any(|(_, u)| &**u == ns::IX11 || &**u == ns::IX10)
43 && !r.is(ns::XBRLI, "xbrl")
44 && !r.is(ns::LINK, "linkbase")
45 && r.local() != "schema"
46 {
47 return true;
48 }
49 if r.local() != "html" {
50 return false;
51 }
52 r.descendant_elements()
53 .any(|n| n.ns() == ns::IX11 || n.ns() == ns::IX10)
54}
55
56/// Load an inline document set. Documents are the XHTML files of one report.
57pub fn load_document_set(docs: Vec<Arc<Document>>) -> InlineSet {
58 extract::load(docs)
59}
60
61/// Inline XBRL validation rules that need the DTS (e.g. nonFraction on numeric concepts).
62pub fn validate(dts: &Dts, set: &InlineSet) -> Vec<Diagnostic> {
63 let mut out = Vec::new();
64 for inst in &set.instances {
65 for f in &inst.facts {
66 let Some(c) = dts.concept(&f.concept) else {
67 continue;
68 };
69 let Some(el) = f.inline.as_ref().and_then(|i| i.element) else {
70 continue;
71 };
72 let n = inst.node(el);
73 match n.local() {
74 "nonFraction" if !c.is_numeric => {
75 out.push(
76 Diagnostic::error(
77 codes::NON_FRACTION_NON_NUMERIC,
78 format!("ix:nonFraction used for non-numeric concept {}", f.concept),
79 )
80 .at(n),
81 );
82 }
83 "fraction" if !c.is_fraction => {
84 out.push(
85 Diagnostic::error(
86 codes::FRACTION_NOT_FRACTION_TYPE,
87 format!(
88 "ix:fraction used for concept {} which is not a fraction type",
89 f.concept
90 ),
91 )
92 .at(n),
93 );
94 }
95 _ => {}
96 }
97 }
98 }
99 out
100}