xbrl-rs 0.3.0

XBRL parser and validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Document view built from the presentation linkbase.

use super::fact::{Fact, ItemFact};
use crate::{ExpandedName, Label, PresentationArc, TaxonomySet};
use std::{
    cmp::Ordering,
    collections::{HashMap, HashSet},
};

/// A hierarchical view of an XBRL document organised by presentation sections.
#[derive(Debug)]
pub struct DocumentView<'a> {
    /// One section per extended link role found in the presentation linkbase.
    pub sections: Vec<SectionView<'a>>,
}

impl<'a> DocumentView<'a> {
    /// Build a document view from instance facts and a taxonomy.
    ///
    /// `fact_indices` in the returned view are item-only indices in depth-first
    /// order, matching [`InstanceDocument::set_fact_value`] / `set_fact_nil`.
    pub fn build(facts: &[Fact], taxonomy: &'a TaxonomySet) -> Self {
        let mut item_facts = Vec::new();
        let mut parents = Vec::new();
        let mut tuple_fact_names = HashSet::new();

        for fact in facts {
            collect_item_facts_with_parent(fact, None, &mut item_facts, &mut parents);
            collect_tuple_fact_names(fact, &mut tuple_fact_names);
        }

        build_view(&item_facts, &parents, &tuple_fact_names, taxonomy)
    }
}

/// One report section (extended link role) from the presentation linkbase.
#[derive(Debug)]
pub struct SectionView<'a> {
    /// The extended link role URI identifying this section.
    pub role: &'a str,
    /// Root nodes of the presentation tree for this section.
    pub nodes: Vec<TreeNode<'a>>,
}

/// A single node in the presentation hierarchy.
#[derive(Debug)]
pub struct TreeNode<'a> {
    /// Concept name (e.g. `bs.ass`).
    pub concept_name: &'a str,
    /// All labels for this concept. The caller selects the desired language
    /// and role (e.g. `terseLabel`, `label`).
    pub labels: &'a [Label],
    /// Depth in the tree; root nodes have depth 0.
    pub depth: usize,
    /// Indices into the `InstanceDocument::facts()` slice for facts whose concept
    /// maps to this concept name.
    ///
    /// Storing indices rather than references means the view's lifetime is tied
    /// only to the taxonomy, leaving the instance free to be mutably borrowed
    /// while the view is alive.
    pub fact_indices: Vec<usize>,
    /// Child nodes, ordered by the presentation arc `order` attribute.
    pub children: Vec<TreeNode<'a>>,
}

/// Build a [`DocumentView`] from item-fact indices and tuple-presence context.
fn build_view<'a>(
    facts: &[&ItemFact],
    parents: &[Option<ExpandedName>],
    tuple_fact_names: &HashSet<ExpandedName>,
    taxonomy: &'a TaxonomySet,
) -> DocumentView<'a> {
    let mut fact_index: HashMap<ExpandedName, Vec<usize>> = HashMap::new();
    let mut tuple_child_index: HashMap<(ExpandedName, ExpandedName), Vec<usize>> = HashMap::new();

    for (i, (fact, parent)) in facts.iter().zip(parents.iter()).enumerate() {
        let concept = fact.concept_name().clone();
        fact_index.entry(concept.clone()).or_default().push(i);

        if let Some(parent) = parent {
            tuple_child_index
                .entry((parent.clone(), concept))
                .or_default()
                .push(i);
        }
    }

    let roles = taxonomy
        .presentations()
        .iter()
        .map(|(role, arcs)| (role.as_str(), arcs))
        .collect::<Vec<_>>();

    let mut sections = Vec::with_capacity(roles.len());

    for (role, arcs) in roles {
        let mut arc_index: HashMap<&'a ExpandedName, Vec<&'a PresentationArc>> = HashMap::new();

        for arc in arcs {
            arc_index.entry(&arc.from).or_default().push(arc);
        }

        // Sort children by `order` up front so `build_nodes` never needs to
        // re-sort.
        for children in arc_index.values_mut() {
            children.sort_by(|a, b| match (a.order, b.order) {
                (Some(x), Some(y)) => x.cmp(&y),
                (Some(_), None) => Ordering::Less,
                (None, Some(_)) => Ordering::Greater,
                (None, None) => Ordering::Equal,
            });
        }

        let roots = find_roots(arcs, &arc_index);
        let mut visited: HashSet<&'a ExpandedName> = HashSet::new();
        let nodes = roots
            .iter()
            .flat_map(|root_id| {
                build_nodes(
                    &arc_index,
                    root_id,
                    0,
                    taxonomy,
                    &fact_index,
                    &tuple_child_index,
                    tuple_fact_names,
                    &mut visited,
                )
            })
            .collect();

        sections.push(SectionView { role, nodes });
    }

    DocumentView { sections }
}

/// Recursively collect item facts, tracking the parent tuple concept for each
/// fact.
fn collect_item_facts_with_parent<'a>(
    fact: &'a Fact,
    parent_tuple: Option<&ExpandedName>,
    facts: &mut Vec<&'a ItemFact>,
    parents: &mut Vec<Option<ExpandedName>>,
) {
    match fact {
        Fact::Item(item) => {
            facts.push(item);
            parents.push(parent_tuple.cloned());
        }
        Fact::Tuple(tuple) => {
            for child in tuple.children() {
                collect_item_facts_with_parent(child, Some(tuple.concept_name()), facts, parents);
            }
        }
    }
}

/// Recursively collect concept names of all tuple facts.
fn collect_tuple_fact_names(fact: &Fact, names: &mut HashSet<ExpandedName>) {
    match fact {
        Fact::Item(_) => {}
        Fact::Tuple(tuple) => {
            names.insert(tuple.concept_name().clone());
            for child in tuple.children() {
                collect_tuple_fact_names(child, names);
            }
        }
    }
}

/// Find root concept IDs: those that appear as `from` but never as `to`.
pub(crate) fn find_roots<'a>(
    arcs: &'a [PresentationArc],
    arc_index: &HashMap<&'a ExpandedName, Vec<&'a PresentationArc>>,
) -> Vec<&'a ExpandedName> {
    let to_set: HashSet<&ExpandedName> = arcs.iter().map(|a| &a.to).collect();
    let mut seen: HashSet<&ExpandedName> = HashSet::new();
    let mut roots: Vec<&'a ExpandedName> = Vec::new();

    for arc in arcs {
        let from = &arc.from;

        if !to_set.contains(from) && seen.insert(from) {
            roots.push(from);
        }
    }

    // Order roots by their minimum outgoing arc order using the pre-built index.
    roots.sort_by(|a, b| {
        let min_order = |id: &&ExpandedName| {
            arc_index
                .get(*id)
                .and_then(|arcs| arcs.iter().filter_map(|a| a.order).min())
        };
        match (min_order(a), min_order(b)) {
            (Some(x), Some(y)) => x.cmp(&y),
            (Some(_), None) => Ordering::Less,
            (None, Some(_)) => Ordering::Greater,
            (None, None) => Ordering::Equal,
        }
    });

    roots
}

/// Recursively build tree nodes for all children of `parent_id`.
#[allow(clippy::too_many_arguments)]
fn build_nodes<'a>(
    arc_index: &HashMap<&'a ExpandedName, Vec<&'a PresentationArc>>,
    parent_id: &'a ExpandedName,
    depth: usize,
    taxonomy: &'a TaxonomySet,
    fact_index: &HashMap<ExpandedName, Vec<usize>>,
    tuple_child_index: &HashMap<(ExpandedName, ExpandedName), Vec<usize>>,
    tuple_fact_names: &HashSet<ExpandedName>,
    visited: &mut HashSet<&'a ExpandedName>,
) -> Vec<TreeNode<'a>> {
    if !visited.insert(parent_id) {
        // The branch is skipped if a cycle is detected.
        return Vec::new();
    }

    // Children are already sorted by `order`
    let children_arcs = arc_index.get(parent_id).map(Vec::as_slice).unwrap_or(&[]);

    let mut nodes = Vec::with_capacity(children_arcs.len());

    for arc in children_arcs {
        let child_id = &arc.to;
        let labels = taxonomy.labels(child_id).unwrap_or_default();
        // Prefer scoped lookup by (parent_tuple, child) when available, so
        // each tree node only receives facts from its own tuple context.
        let fact_indices = tuple_child_index
            .get(&(parent_id.clone(), child_id.clone()))
            .or_else(|| fact_index.get(child_id))
            .cloned()
            .unwrap_or_default();
        let children = build_nodes(
            arc_index,
            child_id,
            depth + 1,
            taxonomy,
            fact_index,
            tuple_child_index,
            tuple_fact_names,
            visited,
        );

        // Only include the node if it has facts or children; otherwise it's
        // just a concept with no reported facts.
        //
        // Tuple names are retained when present in the instance even if
        // they currently have no item descendants.
        if !fact_indices.is_empty() || !children.is_empty() || tuple_fact_names.contains(child_id) {
            nodes.push(TreeNode {
                concept_name: &child_id.local_name,
                labels,
                depth,
                fact_indices,
                children,
            });
        }
    }

    visited.remove(parent_id);

    nodes
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Fact, ItemFact, taxonomy::TaxonomySet};
    use rust_decimal::Decimal;

    fn create_taxonomy(
        arcs: Vec<(String, PresentationArc)>,
        labels: Vec<(ExpandedName, Label)>,
    ) -> TaxonomySet {
        let mut taxonomy = TaxonomySet::default();
        for (role, arc) in arcs {
            taxonomy.add_presentation_arc(role, arc);
        }
        for (concept_name, label) in labels {
            taxonomy.add_label(concept_name, label);
        }
        taxonomy
    }

    #[test]
    fn build_view_empty_taxonomy() {
        let taxonomy = TaxonomySet::default();
        let view = DocumentView::build(&[], &taxonomy);
        assert!(view.sections.is_empty());
    }

    #[test]
    fn build_view_single_section_with_hierarchy() {
        let role = "http://example.com/role/bs".to_string();
        let arcs = vec![
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "root".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "child_a".into()),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "root".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "child_b".into()),
                    order: Some(Decimal::new(2, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new(
                        "http://example.com/namespace".into(),
                        "child_a".into(),
                    ),
                    to: ExpandedName::new(
                        "http://example.com/namespace".into(),
                        "grandchild".into(),
                    ),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
        ];
        let labels = vec![(
            ExpandedName::new("http://example.com/namespace".into(), "child_a".into()),
            Label {
                role: "http://www.xbrl.org/2003/role/label".to_string(),
                lang: "en".to_string(),
                text: "Child A".to_string(),
            },
        )];
        let taxonomy = create_taxonomy(arcs, labels);

        // Use a QName without a prefix so concept_id() == "child_a" directly,
        // matching the element ID used in the presentation arcs above.
        let fact = ItemFact::new(
            None,
            ExpandedName::new("http://example.com/namespace".into(), "child_a".into()),
            "ctx1".to_string(),
            None,
            "42".to_string(),
            false,
            None,
            None,
        );
        let facts = vec![Fact::Item(fact)];

        let view = DocumentView::build(&facts, &taxonomy);

        assert_eq!(view.sections.len(), 1);
        let section = &view.sections[0];
        assert_eq!(section.role, role);

        // child_b has no facts so it is excluded; grandchild has no facts either.
        assert_eq!(section.nodes.len(), 1);

        let node_a = &section.nodes[0];
        assert_eq!(node_a.concept_name, "child_a");
        assert_eq!(node_a.labels.len(), 1);
        assert_eq!(node_a.labels[0].text, "Child A");
        assert_eq!(node_a.labels[0].lang, "en");
        assert_eq!(node_a.depth, 0);
        assert_eq!(node_a.fact_indices.len(), 1);
        let first_fact = facts[node_a.fact_indices[0]]
            .as_item()
            .expect("expected item fact");
        assert_eq!(first_fact.value(), "42");
        assert_eq!(node_a.children.len(), 0);
    }

    #[test]
    fn build_view_cycle_protection() {
        let role = "http://example.com/role/cycle".to_string();
        // a -> b -> a  (cycle)
        let arcs = vec![
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "a".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "b".into()),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "b".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "a".into()),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
        ];
        let taxonomy = create_taxonomy(arcs, vec![]);
        // Should not hang or panic.
        let view = DocumentView::build(&[], &taxonomy);
        assert_eq!(view.sections.len(), 1);
    }

    #[test]
    fn tuple_context_scopes_fact_indices() {
        // Same item concept C appears as child of two different tuple parents
        // (TupleA and TupleB) in the presentation linkbase.  The instance has
        // one fact for C inside TupleA and one inside TupleB.  Each tree node
        // for C should receive only the fact from its own tuple context.
        let ns = "http://example.com/ns";
        let tuple_a = ExpandedName::new(ns.into(), "TupleA".into());
        let tuple_b = ExpandedName::new(ns.into(), "TupleB".into());
        let item_c = ExpandedName::new(ns.into(), "C".into());
        let role = "http://example.com/role".to_string();

        let arcs = vec![
            (
                role.clone(),
                PresentationArc {
                    from: tuple_a.clone(),
                    to: item_c.clone(),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
            (
                role.clone(),
                PresentationArc {
                    from: tuple_b.clone(),
                    to: item_c.clone(),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
        ];
        let taxonomy = create_taxonomy(arcs, vec![]);

        let fact_c_in_a = ItemFact::new(
            None,
            item_c.clone(),
            "ctx1".to_string(),
            None,
            "42".to_string(),
            false,
            None,
            None,
        );
        let fact_c_in_b = ItemFact::new(
            None,
            item_c.clone(),
            "ctx1".to_string(),
            None,
            "99".to_string(),
            false,
            None,
            None,
        );
        let facts = [Fact::Item(fact_c_in_a), Fact::Item(fact_c_in_b)];
        // fact 0 is a child of TupleA, fact 1 is a child of TupleB
        let view = build_view(
            &[
                facts[0].as_item().expect("expected item fact"),
                facts[1].as_item().expect("expected item fact"),
            ],
            &[Some(tuple_a.clone()), Some(tuple_b.clone())],
            &HashSet::new(),
            &taxonomy,
        );

        assert_eq!(view.sections.len(), 1);
        let section = &view.sections[0];
        // TupleA and TupleB are roots; build_nodes returns their children,
        // so section.nodes contains one C-node per tuple parent.
        assert_eq!(section.nodes.len(), 2);

        let c_under_a = &section.nodes[0];
        assert_eq!(c_under_a.concept_name, "C");
        assert_eq!(c_under_a.fact_indices, vec![0]);
        let first = facts[c_under_a.fact_indices[0]]
            .as_item()
            .expect("expected item fact");
        assert_eq!(first.value(), "42");

        let c_under_b = &section.nodes[1];
        assert_eq!(c_under_b.concept_name, "C");
        assert_eq!(c_under_b.fact_indices, vec![1]);
        let second = facts[c_under_b.fact_indices[0]]
            .as_item()
            .expect("expected item fact");
        assert_eq!(second.value(), "99");
    }

    #[test]
    fn sibling_order_respected() {
        let role = "http://example.com/role/order".to_string();
        let arcs = vec![
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "root".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "b".into()),
                    order: Some(Decimal::new(2, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
            (
                role.clone(),
                PresentationArc {
                    from: ExpandedName::new("http://example.com/namespace".into(), "root".into()),
                    to: ExpandedName::new("http://example.com/namespace".into(), "a".into()),
                    order: Some(Decimal::new(1, 0)),
                    preferred_label: None,
                    arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
                },
            ),
        ];
        let taxonomy = create_taxonomy(arcs, vec![]);
        let namespace = "http://example.com/namespace";
        let fact_a = ItemFact::new(
            None,
            ExpandedName::new(namespace.into(), "a".into()),
            "ctx1".to_string(),
            None,
            "1".to_string(),
            false,
            None,
            None,
        );
        let fact_b = ItemFact::new(
            None,
            ExpandedName::new(namespace.into(), "b".into()),
            "ctx1".to_string(),
            None,
            "2".to_string(),
            false,
            None,
            None,
        );
        let facts = [Fact::Item(fact_a), Fact::Item(fact_b)];
        let view = DocumentView::build(&facts, &taxonomy);

        let section = &view.sections[0];
        assert_eq!(section.nodes[0].concept_name, "a");
        assert_eq!(section.nodes[1].concept_name, "b");
    }

    #[test]
    fn tuple_presence_keeps_tuple_node_without_item_facts() {
        let role = "http://example.com/role/tuple-visibility".to_string();
        let root = ExpandedName::new("http://example.com/ns".into(), "root".into());
        let tuple = ExpandedName::new("http://example.com/ns".into(), "reportType".into());

        let arcs = vec![(
            role.clone(),
            PresentationArc {
                from: root,
                to: tuple.clone(),
                order: Some(Decimal::new(1, 0)),
                preferred_label: None,
                arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
            },
        )];
        let taxonomy = create_taxonomy(arcs, vec![]);

        let facts: [&ItemFact; 0] = [];
        let parents: [Option<ExpandedName>; 0] = [];
        let tuple_concepts = HashSet::from([tuple.clone()]);

        let view = build_view(&facts, &parents, &tuple_concepts, &taxonomy);

        assert_eq!(view.sections.len(), 1);
        let section = &view.sections[0];
        assert_eq!(section.nodes.len(), 1);
        assert_eq!(section.nodes[0].concept_name, "reportType");
        assert!(section.nodes[0].fact_indices.is_empty());
    }

    #[test]
    fn tuple_without_presence_stays_hidden_when_empty() {
        let role = "http://example.com/role/tuple-hidden".to_string();
        let root = ExpandedName::new("http://example.com/ns".into(), "root".into());
        let tuple = ExpandedName::new("http://example.com/ns".into(), "reportType".into());

        let arcs = vec![(
            role,
            PresentationArc {
                from: root,
                to: tuple,
                order: Some(Decimal::new(1, 0)),
                preferred_label: None,
                arcrole: "http://www.xbrl.org/2003/arcrole/parent-child".into(),
            },
        )];
        let taxonomy = create_taxonomy(arcs, vec![]);

        let facts: [&ItemFact; 0] = [];
        let parents: [Option<ExpandedName>; 0] = [];
        let tuple_concepts = HashSet::new();

        let view = build_view(&facts, &parents, &tuple_concepts, &taxonomy);

        assert_eq!(view.sections.len(), 1);
        assert!(view.sections[0].nodes.is_empty());
    }
}