crabrl 0.1.0

High-performance XBRL parser and validator
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
// Comprehensive XBRL validation
use crate::{model::*, Result, Error};
use std::collections::HashSet;

#[derive(Debug, Clone)]
pub enum ValidationError {
    InvalidContextRef { fact_index: usize, context_id: u16 },
    InvalidUnitRef { fact_index: usize, unit_id: u16 },
    CalculationInconsistency { concept: String, expected: f64, actual: f64 },
    InvalidDataType { concept: String, expected_type: String, actual_value: String },
    MissingRequiredElement { element: String },
    DuplicateId { id: String },
}

pub struct XbrlValidator {
    strict_mode: bool,
    #[allow(dead_code)]
    check_calculations: bool,
    check_duplicates: bool,
    check_contexts: bool,
    check_units: bool,
    #[allow(dead_code)]
    check_datatypes: bool,
    decimal_tolerance: f64,
}

impl XbrlValidator {
    pub fn new() -> Self {
        Self {
            strict_mode: false,
            check_calculations: true,
            check_duplicates: true,
            check_contexts: true,
            check_units: true,
            check_datatypes: true,
            decimal_tolerance: 0.01,
        }
    }

    pub fn strict(mut self) -> Self {
        self.strict_mode = true;
        self
    }

    pub fn with_tolerance(mut self, tolerance: f64) -> Self {
        self.decimal_tolerance = tolerance;
        self
    }

    pub fn validate(&self, doc: &mut Document) -> Result<()> {
        let mut validation_errors = Vec::new();
        
        // Context validation
        if self.check_contexts {
            validation_errors.extend(self.validate_contexts(doc));
        }

        // Unit validation
        if self.check_units {
            validation_errors.extend(self.validate_units(doc));
        }

        // Fact validation
        validation_errors.extend(self.validate_facts(doc));

        // Duplicate detection
        if self.check_duplicates {
            validation_errors.extend(self.check_duplicate_facts(doc));
        }

        // Return error in strict mode if any validation errors
        if self.strict_mode && !validation_errors.is_empty() {
            return Err(Error::Validation(format!(
                "Validation failed with {} errors",
                validation_errors.len()
            )));
        }

        Ok(())
    }

    fn validate_contexts(&self, doc: &Document) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let mut context_ids = HashSet::new();
        
        for ctx in &doc.contexts {
            // Check for duplicate context IDs
            if !context_ids.insert(ctx.id.clone()) {
                errors.push(ValidationError::DuplicateId {
                    id: ctx.id.to_string(),
                });
            }

            // Validate entity identifier
            if ctx.entity.identifier.is_empty() {
                errors.push(ValidationError::MissingRequiredElement {
                    element: format!("Entity identifier for context {}", ctx.id),
                });
            }

            // Validate period
            match &ctx.period {
                Period::Duration { start, end } => {
                    if start > end {
                        errors.push(ValidationError::InvalidDataType {
                            concept: format!("context_{}", ctx.id),
                            expected_type: "valid period".to_string(),
                            actual_value: format!("start {} > end {}", start, end),
                        });
                    }
                }
                _ => {}
            }
        }
        
        errors
    }

    fn validate_units(&self, doc: &Document) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let mut unit_ids = HashSet::new();
        
        for unit in &doc.units {
            // Check for duplicate unit IDs
            if !unit_ids.insert(unit.id.clone()) {
                errors.push(ValidationError::DuplicateId {
                    id: unit.id.to_string(),
                });
            }

            // Validate measures
            match &unit.unit_type {
                UnitType::Simple(measures) => {
                    if measures.is_empty() {
                        errors.push(ValidationError::MissingRequiredElement {
                            element: format!("Measures for unit {}", unit.id),
                        });
                    }
                }
                UnitType::Divide { numerator, denominator } => {
                    if numerator.is_empty() || denominator.is_empty() {
                        errors.push(ValidationError::MissingRequiredElement {
                            element: format!("Numerator/denominator for unit {}", unit.id),
                        });
                    }
                }
                UnitType::Multiply(measures) => {
                    if measures.is_empty() {
                        errors.push(ValidationError::MissingRequiredElement {
                            element: format!("Measures for unit {}", unit.id),
                        });
                    }
                }
            }
        }
        
        errors
    }

    fn validate_facts(&self, doc: &Document) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        
        // Validate fact references
        for i in 0..doc.facts.len() {
            if i < doc.facts.context_ids.len() {
                let context_id = doc.facts.context_ids[i];
                if context_id as usize >= doc.contexts.len() {
                    errors.push(ValidationError::InvalidContextRef {
                        fact_index: i,
                        context_id,
                    });
                }
            }
            
            if i < doc.facts.unit_ids.len() {
                let unit_id = doc.facts.unit_ids[i];
                if unit_id > 0 && unit_id as usize > doc.units.len() {
                    errors.push(ValidationError::InvalidUnitRef {
                        fact_index: i,
                        unit_id,
                    });
                }
            }
        }
        
        errors
    }

    fn check_duplicate_facts(&self, doc: &Document) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        let mut fact_keys = HashSet::new();
        
        for i in 0..doc.facts.len() {
            if i < doc.facts.concept_ids.len() && i < doc.facts.context_ids.len() {
                let key = (doc.facts.concept_ids[i], doc.facts.context_ids[i]);
                if !fact_keys.insert(key) && self.strict_mode {
                    errors.push(ValidationError::DuplicateId {
                        id: format!("Duplicate fact at index {}", i),
                    });
                }
            }
        }
        
        errors
    }
}

// Validation context and rules
pub struct ValidationContext {
    pub profile: ValidationProfile,
    pub custom_rules: Vec<Box<dyn Fn(&Document) -> Vec<ValidationError>>>,
}

#[derive(Debug, Clone, Copy)]
pub enum ValidationProfile {
    Generic,
    SecEdgar,
    Ifrs,
    UsGaap,
}

impl ValidationContext {
    pub fn new(profile: ValidationProfile) -> Self {
        Self {
            profile,
            custom_rules: Vec::new(),
        }
    }

    pub fn add_rule<F>(&mut self, rule: F) 
    where
        F: Fn(&Document) -> Vec<ValidationError> + 'static
    {
        self.custom_rules.push(Box::new(rule));
    }

    pub fn validate(&self, doc: &Document) -> Vec<ValidationError> {
        let mut errors = Vec::new();
        
        // Apply profile-specific rules
        match self.profile {
            ValidationProfile::SecEdgar => {
                errors.extend(sec_validation_rules(doc));
            }
            ValidationProfile::Ifrs => {
                errors.extend(ifrs_validation_rules(doc));
            }
            _ => {}
        }
        
        // Apply custom rules
        for rule in &self.custom_rules {
            errors.extend(rule(doc));
        }
        
        errors
    }
}

// SEC EDGAR specific validation rules
pub fn sec_validation_rules(doc: &Document) -> Vec<ValidationError> {
    let mut errors = Vec::new();
    
    // Check for required DEI contexts
    let mut has_current_period = false;
    let mut has_entity_info = false;
    let mut has_dei_elements = false;
    
    for ctx in &doc.contexts {
        // Check for current period context
        if ctx.id.contains("CurrentYear") || ctx.id.contains("CurrentPeriod") || 
           ctx.id.contains("DocumentPeriodEndDate") {
            has_current_period = true;
        }
        
        // Validate CIK format (10 digits)
        if ctx.entity.scheme.contains("sec.gov/CIK") {
            has_entity_info = true;
            let cik = &ctx.entity.identifier;
            if cik.len() != 10 || !cik.chars().all(|c| c.is_ascii_digit()) {
                errors.push(ValidationError::InvalidDataType {
                    concept: "CIK".to_string(),
                    expected_type: "10-digit number".to_string(),
                    actual_value: cik.to_string(),
                });
            }
        }
    }
    
    // Check for DEI elements in facts
    for i in 0..doc.facts.concept_ids.len() {
        if i < doc.concept_names.len() {
            let concept = &doc.concept_names[i];
            if concept.contains("dei:") || concept.contains("DocumentType") || 
               concept.contains("EntityRegistrantName") {
                has_dei_elements = true;
            }
        }
    }
    
    // Required elements validation
    if !has_current_period {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Current period context required for SEC filing".to_string(),
        });
    }
    
    if !has_entity_info {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Entity CIK information required for SEC filing".to_string(),
        });
    }
    
    if !has_dei_elements {
        errors.push(ValidationError::MissingRequiredElement {
            element: "DEI (Document and Entity Information) elements required".to_string(),
        });
    }
    
    // Validate segment reporting if present
    for ctx in &doc.contexts {
        if let Some(segment) = &ctx.entity.segment {
            // Check explicit members have valid dimension references
            for member in &segment.explicit_members {
                if member.dimension.is_empty() || member.member.is_empty() {
                    errors.push(ValidationError::InvalidDataType {
                        concept: format!("segment_{}", ctx.id),
                        expected_type: "valid dimension member".to_string(),
                        actual_value: format!("{}:{}", member.dimension, member.member),
                    });
                }
            }
        }
    }
    
    // Validate calculation consistency for monetary items
    let mut monetary_facts: Vec<(usize, f64)> = Vec::new();
    for i in 0..doc.facts.len() {
        if i < doc.facts.values.len() {
            if let FactValue::Decimal(val) = &doc.facts.values[i] {
                // Check if this is a monetary fact (has USD unit)
                if i < doc.facts.unit_ids.len() {
                    let unit_id = doc.facts.unit_ids[i] as usize;
                    if unit_id < doc.units.len() {
                        if let UnitType::Simple(measures) = &doc.units[unit_id].unit_type {
                            if measures.iter().any(|m| m.name == "USD" || m.name == "usd") {
                                monetary_facts.push((i, *val));
                            }
                        }
                    }
                }
            }
        }
    }
    
    // Basic calculation validation - check for reasonable values
    for (idx, value) in monetary_facts {
        if value.is_nan() || value.is_infinite() {
            errors.push(ValidationError::InvalidDataType {
                concept: format!("fact_{}", idx),
                expected_type: "valid monetary amount".to_string(),
                actual_value: format!("{}", value),
            });
        }
        // Check for suspiciously large values (> $10 trillion)
        if value.abs() > 10_000_000_000_000.0 {
            errors.push(ValidationError::InvalidDataType {
                concept: format!("fact_{}", idx),
                expected_type: "reasonable monetary amount".to_string(),
                actual_value: format!("${:.2}", value),
            });
        }
    }
    
    errors
}

// IFRS specific validation rules  
pub fn ifrs_validation_rules(doc: &Document) -> Vec<ValidationError> {
    let mut errors = Vec::new();
    
    // Check for IFRS-required contexts
    let mut has_reporting_period = false;
    let mut has_comparative_period = false;
    let mut has_entity_info = false;
    
    for ctx in &doc.contexts {
        // Check for reporting period
        match &ctx.period {
            Period::Duration { start, end: _ } => {
                has_reporting_period = true;
                // IFRS requires comparative information
                if start.contains("PY") || ctx.id.contains("PriorYear") || 
                   ctx.id.contains("Comparative") {
                    has_comparative_period = true;
                }
            }
            Period::Instant { date } => {
                if !date.is_empty() {
                    has_reporting_period = true;
                }
            }
            _ => {}
        }
        
        // Validate entity information
        if !ctx.entity.identifier.is_empty() {
            has_entity_info = true;
        }
    }
    
    // Required contexts validation
    if !has_reporting_period {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Reporting period required for IFRS filing".to_string(),
        });
    }
    
    if !has_comparative_period {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Comparative period information required by IFRS".to_string(),
        });
    }
    
    if !has_entity_info {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Entity identification required for IFRS filing".to_string(),
        });
    }
    
    // Validate dimensional structure
    let mut dimension_validations = Vec::new();
    for ctx in &doc.contexts {
        // Check segment dimensions
        if let Some(segment) = &ctx.entity.segment {
            for member in &segment.explicit_members {
                // IFRS dimensions should follow specific patterns
                if !member.dimension.contains(":") {
                    dimension_validations.push(format!("Invalid dimension format: {}", member.dimension));
                }
                if member.dimension.contains("ifrs") || member.dimension.contains("ifrs-full") {
                    // Valid IFRS dimension
                    if member.member.is_empty() {
                        errors.push(ValidationError::InvalidDataType {
                            concept: format!("dimension_{}", ctx.id),
                            expected_type: "valid IFRS dimension member".to_string(),
                            actual_value: member.dimension.to_string(),
                        });
                    }
                }
            }
            
            // Check typed members for IFRS compliance
            for typed in &segment.typed_members {
                if typed.dimension.contains("ifrs") && typed.value.is_empty() {
                    errors.push(ValidationError::InvalidDataType {
                        concept: format!("typed_dimension_{}", ctx.id),
                        expected_type: "non-empty typed dimension value".to_string(),
                        actual_value: typed.dimension.to_string(),
                    });
                }
            }
        }
        
        // Check scenario dimensions (alternative to segment)
        if let Some(scenario) = &ctx.scenario {
            for member in &scenario.explicit_members {
                if member.dimension.contains("ifrs") && member.member.is_empty() {
                    errors.push(ValidationError::InvalidDataType {
                        concept: format!("scenario_dimension_{}", ctx.id),
                        expected_type: "valid IFRS scenario member".to_string(),
                        actual_value: member.dimension.to_string(),
                    });
                }
            }
        }
    }
    
    // Check for mandatory IFRS disclosures in facts
    let mut has_financial_position = false;
    let mut has_comprehensive_income = false;
    let mut has_cash_flows = false;
    let mut has_changes_in_equity = false;
    
    for i in 0..doc.concept_names.len() {
        let concept = &doc.concept_names[i];
        let lower = concept.to_lowercase();
        
        if lower.contains("financialposition") || lower.contains("balancesheet") || 
           lower.contains("assets") || lower.contains("liabilities") {
            has_financial_position = true;
        }
        
        if lower.contains("comprehensiveincome") || lower.contains("profitorloss") || 
           lower.contains("income") || lower.contains("revenue") {
            has_comprehensive_income = true;
        }
        
        if lower.contains("cashflow") || lower.contains("cashflows") {
            has_cash_flows = true;
        }
        
        if lower.contains("changesinequity") || lower.contains("equity") {
            has_changes_in_equity = true;
        }
    }
    
    // Validate mandatory statements
    if !has_financial_position {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Statement of Financial Position required by IFRS".to_string(),
        });
    }
    
    if !has_comprehensive_income {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Statement of Comprehensive Income required by IFRS".to_string(),
        });
    }
    
    if !has_cash_flows {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Statement of Cash Flows required by IFRS".to_string(),
        });
    }
    
    if !has_changes_in_equity {
        errors.push(ValidationError::MissingRequiredElement {
            element: "Statement of Changes in Equity required by IFRS".to_string(),
        });
    }
    
    // Validate presentation linkbase relationships
    for link in &doc.presentation_links {
        // Check order is valid (typically 1.0 to 999.0)
        if link.order < 0.0 || link.order > 1000.0 {
            errors.push(ValidationError::InvalidDataType {
                concept: format!("presentation_link_{}_{}", link.from, link.to),
                expected_type: "valid presentation order (0-1000)".to_string(),
                actual_value: format!("{}", link.order),
            });
        }
    }
    
    // Validate calculation relationships
    for link in &doc.calculation_links {
        // Check weight is reasonable (-1.0 or 1.0 typically)
        if link.weight != 1.0 && link.weight != -1.0 && link.weight != 0.0 {
            // Unusual weight, might be an error
            if link.weight.abs() > 10.0 {
                errors.push(ValidationError::InvalidDataType {
                    concept: format!("calculation_link_{}_{}", link.from, link.to),
                    expected_type: "reasonable calculation weight".to_string(),
                    actual_value: format!("{}", link.weight),
                });
            }
        }
    }
    
    errors
}