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
use super::schema::TaxonomySchema;
use crate::{
    CalculationArc, ConceptId, DefinitionArc, ExpandedName, Label, PresentationArc, Reference,
    RoleUri, SchemaRefUrl,
    error::{Result, XbrlError},
    taxonomy::{
        RoleType,
        linkbases::{
            parser::{LinkbaseParser, RawLinkbases},
            resolver::{self, Linkbases},
        },
        schema::Concept,
    },
};
use indexmap::{IndexMap, IndexSet};
use std::{
    collections::{HashMap, HashSet, VecDeque},
    fs, io,
    path::{Path, PathBuf},
};

/// The complete Discoverable Taxonomy Set (DTS).
///
/// Built by following all schema imports, includes, and linkbase references
/// starting from one or more entry point schemas.
#[derive(Debug, Default)]
pub struct TaxonomySet {
    /// The directory of the taxonomy files, used to resolve relative
    /// references.
    entry_point: PathBuf,
    /// Entry point schema URLs mapped to their resolved local paths, in the
    /// order they were passed to [`TaxonomySet::discover`].
    schema_refs: IndexMap<SchemaRefUrl, PathBuf>,
    /// All schemas in the DTS, keyed by their canonical absolute path.
    schemas: IndexMap<PathBuf, TaxonomySchema>,
    /// All linkbase file paths discovered (canonical absolute paths).
    linkbase_paths: Vec<PathBuf>,
    /// Resolved linkbase data merged from all linkbase files.
    linkbases: Linkbases,
    /// Maps each role URI to the schema file that defines it (`link:roleType`).
    role_source_schema: HashMap<RoleUri, PathBuf>,
    /// Taxonomy date extracted from the schema ref URLs.
    /// German-style taxonomies yield a date (e.g. `"2020-04-01"`); US GAAP
    /// taxonomies yield a year (e.g. `"2023"`). `None` if neither pattern
    /// matches.
    date: Option<String>,
}

impl TaxonomySet {
    /// Discover the DTS starting from one or more entry point schema files.
    ///
    /// Starts from the provided `entry_point` directory and follows the given
    /// `schema_refs` to find the initial schemas. Then recursively follows all
    /// `xs:import` and `xs:include` references to discover the full set of
    /// schemas in the DTS.
    ///
    /// Automatic download of taxonomy files is not supported. All referenced
    /// files must be present locally.
    pub fn discover(schema_refs: Vec<String>, entry_point: PathBuf) -> Result<Self> {
        let date = schema_refs.first().and_then(|url| extract_date(url));

        if schema_refs.len() > 1
            && let Some(ref expected) = date
        {
            for url in schema_refs.iter().skip(1) {
                if let Some(found) = extract_date(url)
                    && &found != expected
                {
                    return Err(XbrlError::VersionMismatch {
                        expected: expected.clone(),
                        found,
                        schema_ref: url.clone(),
                    });
                }
            }
        }

        let mut visited: HashSet<PathBuf> = HashSet::new();
        let mut queue: VecDeque<PathBuf> = VecDeque::new();
        let mut schemas: IndexMap<PathBuf, TaxonomySchema> = IndexMap::new();
        let mut linkbase_set: IndexSet<PathBuf> = IndexSet::new();

        let canonical_entry_point =
            fs::canonicalize(&entry_point).map_err(|err| XbrlError::FileRead {
                path: entry_point.clone(),
                source: err,
            })?;

        let mut schema_refs_map: IndexMap<SchemaRefUrl, PathBuf> = IndexMap::new();
        for url in &schema_refs {
            let canonical = canonical_entry_point.join(strip_prefix(url));
            schema_refs_map.insert(url.clone().into(), canonical.clone());
            if visited.insert(canonical.clone()) {
                queue.push_back(canonical);
            }
        }

        while let Some(path) = queue.pop_front() {
            let schema = TaxonomySchema::from_file(&path)?;
            let schema_dir = path.parent().unwrap_or(Path::new("."));

            // Collect linkbase refs
            for lbref in &schema.linkbase_refs {
                if let Some(resolved) = resolve_local_path(schema_dir, &lbref.href) {
                    if !resolved.exists() {
                        return Err(XbrlError::FileRead {
                            path: resolved,
                            source: io::Error::new(
                                io::ErrorKind::NotFound,
                                "referenced linkbase file does not exist",
                            ),
                        });
                    }
                    let canonical =
                        fs::canonicalize(&resolved).map_err(|err| XbrlError::FileRead {
                            path: resolved.clone(),
                            source: err,
                        })?;
                    linkbase_set.insert(canonical);
                }
            }

            // Follow xs:import schemaLocation
            for import in &schema.imports {
                if let Some(ref location) = import.schema_location
                    && let Some(resolved) = resolve_local_path(schema_dir, location)
                    && resolved.exists()
                    && let Ok(canonical) = std::fs::canonicalize(&resolved)
                    && visited.insert(canonical.clone())
                {
                    queue.push_back(canonical);
                }
            }

            // Follow xs:include schemaLocation
            for include in &schema.includes {
                if let Some(resolved) = resolve_local_path(schema_dir, &include.schema_location)
                    && resolved.exists()
                    && let Ok(canonical) = fs::canonicalize(&resolved)
                    && visited.insert(canonical.clone())
                {
                    queue.push_back(canonical);
                }
            }

            schemas.insert(path, schema);
        }

        let linkbase_paths: Vec<PathBuf> = linkbase_set.into_iter().collect();
        let mut linkbases = RawLinkbases::default();

        for path in &linkbase_paths {
            let mut parser = LinkbaseParser::from_file(path)?;
            parser.parse(&mut linkbases)?;
        }

        let concepts_by_id = schemas
            .values()
            .flat_map(|schema| &schema.concepts)
            .filter_map(|concept| concept.id.clone().map(|id| (ConceptId::from(id), concept)))
            .collect::<HashMap<_, _>>();

        let linkbases = resolver::resolve_linkbases(linkbases, &concepts_by_id)?;

        // Build role → source schema map
        let mut role_source_schema: HashMap<RoleUri, PathBuf> = HashMap::new();
        for (path, schema) in &schemas {
            for role_type in &schema.role_types {
                role_source_schema
                    .entry(role_type.role_uri.clone())
                    .or_insert_with(|| path.clone());
            }
        }

        let taxonomy = TaxonomySet {
            entry_point,
            schema_refs: schema_refs_map,
            schemas,
            linkbase_paths,
            linkbases,
            role_source_schema,
            date,
        };

        Ok(taxonomy)
    }

    /// Get the entry point directory of taxonomy files.
    pub fn entry_point(&self) -> &Path {
        &self.entry_point
    }

    /// Get the taxonomy date, if present.
    ///
    /// Returns a date string for German-style taxonomies (e.g. `"2020-04-01"`)
    /// or a year string for US GAAP (e.g. `"2023"`).
    pub fn date(&self) -> Option<&str> {
        self.date.as_deref()
    }

    /// Get the entry point schema URLs and their resolved local paths, in declaration order.
    pub fn schema_refs(&self) -> &IndexMap<SchemaRefUrl, PathBuf> {
        &self.schema_refs
    }

    /// Get the resolved local path of the schema file that defines the given role URI.
    pub fn role_source_path(&self, role: &str) -> Option<&Path> {
        self.role_source_schema.get(role).map(PathBuf::as_path)
    }

    /// Get all schemas in the DTS.
    pub fn schemas(&self) -> &IndexMap<PathBuf, TaxonomySchema> {
        &self.schemas
    }

    /// Get all discovered linkbase file paths.
    pub fn linkbase_paths(&self) -> &[PathBuf] {
        &self.linkbase_paths
    }

    /// Get all role type definitions across all schemas in the DTS.
    pub fn role_types(&self) -> Vec<&RoleType> {
        self.schemas.values().flat_map(|s| &s.role_types).collect()
    }

    /// Get all element definitions across all schemas in the DTS.
    pub fn concepts(&self) -> impl Iterator<Item = &Concept> {
        self.schemas
            .values()
            .flat_map(|schema| schema.concepts.iter())
    }

    /// Find a concept definition by name across all schemas.
    pub fn find_concept(&self, name: &ExpandedName) -> Option<&Concept> {
        self.schemas
            .values()
            .flat_map(|schema| &schema.concepts)
            .find(|concept| &concept.name == name)
    }

    /// Find a concept definition by its ID attribute (e.g., `de-gaap-ci_bs.ass`).
    pub fn find_concept_by_id(&self, id: &str) -> Option<&Concept> {
        self.schemas
            .values()
            .flat_map(|schema| &schema.concepts)
            .find(|concept| concept.id.as_deref() == Some(id))
    }

    /// Find the tuple element that directly contains the given concept, if any.
    ///
    /// A concept belongs to a tuple when its `substitutionGroup` points to an abstract
    /// head element that is listed as an `xs:element[@ref]` inside the tuple's inline
    /// `xs:complexType`. Only one level of indirection is resolved (direct parent tuple).
    pub fn find_parent_tuple(&self, concept_id: &str) -> Option<&Concept> {
        let element = self.find_concept_by_id(concept_id)?;
        // Find a tuple whose xs:complexType references this child element QName
        self.schemas
            .values()
            .flat_map(|schema| &schema.concepts)
            .find(|concept| {
                concept.is_tuple()
                    && concept
                        .content_model
                        .as_ref()
                        .map(|model| model.allows_local_name(&element.name.local_name))
                        .unwrap_or(false)
            })
    }

    /// Find all tuple ancestor IDs from root tuple to direct parent tuple.
    pub fn tuple_ancestor_ids(&self, concept_id: &str) -> Vec<String> {
        let mut ancestors = Vec::new();
        let mut current = concept_id.to_string();
        let mut seen = HashSet::new();

        while seen.insert(current.clone()) {
            let Some(parent_tuple) = self.find_parent_tuple(&current) else {
                break;
            };

            ancestors.push(parent_tuple.id.clone().unwrap_or_default().to_string());
            current = parent_tuple.id.clone().unwrap_or_default().to_string();
        }

        ancestors.reverse();
        ancestors
    }

    /// Map an element ID from a schema to the qualified concept name used in
    /// instance facts.
    ///
    /// For example, `de-gaap-ci_bs.ass` becomes `de-gaap-ci:bs.ass`. Returns
    /// `None` if the element is not found.
    pub fn qualified_name(&self, element_id: &str) -> Option<ExpandedName> {
        for schema in self.schemas.values() {
            if let Some(concept) = schema
                .concepts
                .iter()
                .find(|concept| concept.id.as_deref() == Some(element_id))
            {
                return Some(concept.name.clone());
            }
        }

        None
    }

    /// Get all concept labels.
    pub fn labels_map(&self) -> &HashMap<ExpandedName, Vec<Label>> {
        &self.linkbases.labels
    }

    /// Get labels for a specific concept by its name.
    pub fn labels(&self, concept_name: &ExpandedName) -> Option<&[Label]> {
        self.linkbases
            .labels
            .get(concept_name)
            .map(|labels| labels.as_slice())
    }

    /// Get all presentation arcs grouped by role URI, in entry-point discovery order.
    pub fn presentations(&self) -> &IndexMap<RoleUri, Vec<PresentationArc>> {
        &self.linkbases.presentations
    }

    /// Get presentation arcs for a specific role URI.
    pub fn presentation_arcs(&self, role: &str) -> Option<&[PresentationArc]> {
        self.linkbases
            .presentations
            .get(role)
            .map(|arcs| arcs.as_slice())
    }

    /// Get all calculation arcs grouped by role URI.
    pub fn calculations(&self) -> &HashMap<RoleUri, Vec<CalculationArc>> {
        &self.linkbases.calculations
    }

    /// Get calculation arcs for a specific role URI.
    pub fn calculation_arcs(&self, role: &str) -> Option<&[CalculationArc]> {
        self.linkbases
            .calculations
            .get(role)
            .map(|arcs| arcs.as_slice())
    }

    /// Get all definition arcs grouped by role URI.
    pub fn definitions(&self) -> &HashMap<RoleUri, Vec<DefinitionArc>> {
        &self.linkbases.definitions
    }

    /// Get definition arcs for a specific role URI.
    pub fn definition_arcs(&self, role: &str) -> Option<&[DefinitionArc]> {
        self.linkbases
            .definitions
            .get(role)
            .map(|arcs| arcs.as_slice())
    }

    /// Get all concept references.
    pub fn references(&self) -> &HashMap<ConceptId, Vec<Reference>> {
        &self.linkbases.references
    }

    /// Get references for a specific concept by its element ID.
    pub fn references_for(&self, concept_id: &str) -> Option<&[Reference]> {
        self.linkbases
            .references
            .get(concept_id)
            .map(|references| references.as_slice())
    }

    /// Get a schema by its target namespace.
    pub fn schema_by_namespace(&self, namespace: &str) -> Option<&TaxonomySchema> {
        self.schemas
            .values()
            .find(|s| s.target_namespace.as_deref() == Some(namespace))
    }
}

#[cfg(test)]
impl TaxonomySet {
    /// Insert a presentation arc for a role URI. Used in unit tests.
    pub fn add_presentation_arc(&mut self, role: String, arc: PresentationArc) {
        self.linkbases
            .presentations
            .entry(role.into())
            .or_default()
            .push(arc);
    }

    /// Insert a label for a concept name.
    pub fn add_label(&mut self, concept_name: ExpandedName, label: Label) {
        self.linkbases
            .labels
            .entry(concept_name)
            .or_default()
            .push(label);
    }
}

/// Resolve a relative reference to a local file path.
/// Returns `None` for HTTP/HTTPS URLs.
fn resolve_local_path(base_dir: &Path, reference: &str) -> Option<PathBuf> {
    if reference.contains("://") {
        return None;
    }
    Some(base_dir.join(reference))
}

/// Extracts the taxonomy date from a schema ref URL.
///
/// Two patterns are recognized:
/// - German style: the first path segment ends with `YYYY-MM-DD`
///   (e.g. `de-gcd-2020-04-01` → `"2020-04-01"`)
/// - US GAAP style: a standalone 4-digit year appears as a path segment
///   (e.g. `us-gaap/2023/elts/…` → `"2023"`)
///
/// Returns `None` if neither pattern matches.
fn extract_date(url: &str) -> Option<String> {
    let stripped = strip_prefix(url);

    // German style: first segment ends with YYYY-MM-DD (e.g. de-gcd-2020-04-01).
    if let Some(segment) = stripped.split('/').next() {
        let parts: Vec<&str> = segment.split('-').collect();
        if parts.len() >= 3 {
            let tail = &parts[parts.len() - 3..];
            if tail[0].len() == 4
                && tail[1].len() == 2
                && tail[2].len() == 2
                && tail.iter().all(|p| p.chars().all(|c| c.is_ascii_digit()))
            {
                return Some(tail.join("-"));
            }
        }
    }

    // US GAAP style: standalone 4-digit year segment (e.g. us-gaap/2023/elts/…).
    for segment in stripped.split('/') {
        if segment.len() == 4 && segment.chars().all(|c| c.is_ascii_digit()) {
            return Some(segment.to_string());
        }
    }

    None
}

/// Strips the URL scheme, host, and leading `/taxonomies/` segment to
/// produce paths suitable for joining with a local taxonomy directory.
///
/// For example:
/// `http://www.xbrl.de/taxonomies/de-gcd-2020-04-01/de-gcd-2020-04-01-shell.xsd`
/// becomes `de-gcd-2020-04-01/de-gcd-2020-04-01-shell.xsd`.
pub fn strip_prefix(href: &str) -> &str {
    let path = href
        .find("://")
        .and_then(|i| href[i + 3..].find('/'))
        .map(|i| &href[href.find("://").unwrap() + 3 + i..])
        .unwrap_or(href);
    // Strip leading "/taxonomies/" if present
    path.strip_prefix("/taxonomies/")
        .or_else(|| path.strip_prefix("/"))
        .unwrap_or(path)
}