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
//! Validation logic for TextGrid data.
//!
//! This module provides functionality to validate the integrity of a `TextGrid` structure.
//! It checks for consistent time bounds, non-overlapping intervals, and proper tier alignment,
//! ensuring the data adheres to the expected constraints of a Praat TextGrid.
//!
//! ## Validation Checks
//! - **TextGrid Bounds**: Ensures `xmin < xmax`.
//! - **Tier Bounds**: Verifies each tier's bounds are within the TextGrid's bounds and `xmin < xmax`.
//! - **IntervalTiers**: Confirms intervals are non-overlapping, sequential, and have valid bounds (`xmin < xmax`).
//! - **PointTiers**: Ensures all points fall within the tier's time bounds.
//!
//! ## Usage
//! ```rust
//! use textgrid::{TextGrid, Tier, TierType, Interval, validate_textgrid};
//!
//! fn main() -> Result<(), textgrid::TextGridError> {
//! // Create a valid TextGrid
//! let mut tg = TextGrid::new(0.0, 10.0)?;
//! let tier = Tier {
//! name: "words".to_string(),
//! tier_type: TierType::IntervalTier,
//! xmin: 0.0,
//! xmax: 10.0,
//! intervals: vec![Interval {
//! xmin: 1.0,
//! xmax: 2.0,
//! text: "hello".to_string(),
//! }],
//! points: vec![],
//! };
//! tg.add_tier(tier)?;
//!
//! // Validate the TextGrid
//! validate_textgrid(&tg)?;
//! println!("TextGrid is valid!");
//! Ok(())
//! }
//! ```
use crate;
/// Validates the integrity of a `TextGrid` structure.
///
/// # Arguments
/// * `textgrid` - The `TextGrid` to validate.
///
/// # Returns
/// Returns a `Result` indicating success (`Ok(())`) or a `TextGridError` if validation fails.
///
/// # Errors
/// - `TextGridError::Format` if any of the following conditions are met:
/// - TextGrid `xmin >= xmax`.
/// - Tier bounds are outside TextGrid bounds or `xmin >= xmax`.
/// - IntervalTiers have overlapping or invalid intervals (`xmin >= xmax`).
/// - PointTiers have points outside tier bounds.
///
/// # Examples
/// ```rust
/// let tg = TextGrid::new(0.0, 5.0).unwrap(); // Assume valid tiers are added
/// assert!(textgrid::validate_textgrid(&tg).is_ok());
/// ```