oximedia-edl 0.1.8

Edit Decision List (EDL) parser and generator for media workflows
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
//! EDL validation rules, errors, and report.
//!
//! Provides `EdlValidationRule`, `ValidationError`, `EdlValidator`,
//! and `EdlValidationReport` for checking EDL event sequences for
//! common conformance issues.

#![allow(dead_code)]

use crate::edl_event::{EditType, EdlEvent, EdlEventList};

/// A named rule that can be evaluated against EDL events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EdlValidationRule {
    /// Event numbers must be sequential starting from 1.
    SequentialNumbering,
    /// Record-out of each event must equal the record-in of the next.
    ContinuousTimeline,
    /// Reel name must not be empty or longer than 8 characters.
    ValidReelName,
    /// Source duration must be positive (source_out > source_in).
    PositiveSourceDuration,
    /// Record duration must be positive (record_out > record_in).
    PositiveRecordDuration,
    /// Wipe events must have a wipe number set.
    WipeHasPattern,
}

impl EdlValidationRule {
    /// The human-readable rule name.
    #[must_use]
    pub fn rule_name(self) -> &'static str {
        match self {
            Self::SequentialNumbering => "SequentialNumbering",
            Self::ContinuousTimeline => "ContinuousTimeline",
            Self::ValidReelName => "ValidReelName",
            Self::PositiveSourceDuration => "PositiveSourceDuration",
            Self::PositiveRecordDuration => "PositiveRecordDuration",
            Self::WipeHasPattern => "WipeHasPattern",
        }
    }

    /// Returns `true` if violations of this rule are fatal (blocking delivery).
    #[must_use]
    pub fn is_fatal(self) -> bool {
        matches!(
            self,
            Self::PositiveSourceDuration | Self::PositiveRecordDuration | Self::ValidReelName
        )
    }
}

/// A single validation failure.
#[derive(Debug, Clone)]
pub struct ValidationError {
    /// The event number where the violation occurred (0 = global).
    pub event_number: u32,
    /// Which rule was violated.
    pub rule: EdlValidationRule,
    /// Human-readable description of the problem.
    pub message: String,
}

impl ValidationError {
    /// Create a new `ValidationError`.
    #[must_use]
    pub fn new(event_number: u32, rule: EdlValidationRule, message: impl Into<String>) -> Self {
        Self {
            event_number,
            rule,
            message: message.into(),
        }
    }

    /// Returns `true` if this error is fatal (derived from the rule).
    #[must_use]
    pub fn is_fatal(&self) -> bool {
        self.rule.is_fatal()
    }
}

/// Validator that checks an `EdlEventList` against a configurable set of rules.
#[derive(Debug, Clone)]
pub struct EdlValidator {
    rules: Vec<EdlValidationRule>,
}

impl EdlValidator {
    /// Create a validator with all rules enabled.
    #[must_use]
    pub fn all_rules() -> Self {
        Self {
            rules: vec![
                EdlValidationRule::SequentialNumbering,
                EdlValidationRule::ContinuousTimeline,
                EdlValidationRule::ValidReelName,
                EdlValidationRule::PositiveSourceDuration,
                EdlValidationRule::PositiveRecordDuration,
                EdlValidationRule::WipeHasPattern,
            ],
        }
    }

    /// Create a validator with only fatal rules enabled.
    #[must_use]
    pub fn fatal_only() -> Self {
        Self {
            rules: vec![
                EdlValidationRule::ValidReelName,
                EdlValidationRule::PositiveSourceDuration,
                EdlValidationRule::PositiveRecordDuration,
            ],
        }
    }

    /// Validate a single `EdlEvent` and return any errors found.
    #[must_use]
    pub fn validate_event(&self, event: &EdlEvent) -> Vec<ValidationError> {
        let mut errors = Vec::new();

        for &rule in &self.rules {
            match rule {
                EdlValidationRule::ValidReelName => {
                    if event.reel.is_empty() || event.reel.len() > 8 {
                        errors.push(ValidationError::new(
                            event.number,
                            rule,
                            format!("Reel name '{}' is invalid (must be 1-8 chars)", event.reel),
                        ));
                    }
                }
                EdlValidationRule::PositiveSourceDuration => {
                    if event.source_out <= event.source_in {
                        errors.push(ValidationError::new(
                            event.number,
                            rule,
                            "Source duration is zero or negative",
                        ));
                    }
                }
                EdlValidationRule::PositiveRecordDuration => {
                    if event.record_out <= event.record_in {
                        errors.push(ValidationError::new(
                            event.number,
                            rule,
                            "Record duration is zero or negative",
                        ));
                    }
                }
                EdlValidationRule::WipeHasPattern => {
                    if event.edit_type == EditType::Wipe && event.wipe_number.is_none() {
                        errors.push(ValidationError::new(
                            event.number,
                            rule,
                            "Wipe event is missing a wipe pattern number",
                        ));
                    }
                }
                // List-level rules are not checked per-event.
                EdlValidationRule::SequentialNumbering | EdlValidationRule::ContinuousTimeline => {}
            }
        }

        errors
    }

    /// Validate a full `EdlEventList` and return a `EdlValidationReport`.
    #[must_use]
    pub fn validate_list(&self, list: &EdlEventList) -> EdlValidationReport {
        let mut errors: Vec<ValidationError> = Vec::new();

        // Per-event rules.
        for event in list.events() {
            errors.extend(self.validate_event(event));
        }

        // List-level rules.
        if self.rules.contains(&EdlValidationRule::SequentialNumbering) {
            for (i, event) in list.events().iter().enumerate() {
                let expected = (i + 1) as u32;
                if event.number != expected {
                    errors.push(ValidationError::new(
                        event.number,
                        EdlValidationRule::SequentialNumbering,
                        format!("Expected event number {expected}, got {}", event.number),
                    ));
                }
            }
        }

        if self.rules.contains(&EdlValidationRule::ContinuousTimeline) {
            let events = list.events();
            for pair in events.windows(2) {
                if pair[0].record_out != pair[1].record_in {
                    errors.push(ValidationError::new(
                        pair[1].number,
                        EdlValidationRule::ContinuousTimeline,
                        format!(
                            "Gap/overlap: event {} record_out={} != event {} record_in={}",
                            pair[0].number, pair[0].record_out, pair[1].number, pair[1].record_in,
                        ),
                    ));
                }
            }
        }

        EdlValidationReport { errors }
    }
}

/// The result of running an `EdlValidator` over an event list.
#[derive(Debug, Clone, Default)]
pub struct EdlValidationReport {
    errors: Vec<ValidationError>,
}

impl EdlValidationReport {
    /// Returns `true` if there are any fatal errors.
    #[must_use]
    pub fn has_fatals(&self) -> bool {
        self.errors.iter().any(|e| e.is_fatal())
    }

    /// Returns `true` if the report contains no errors.
    #[must_use]
    pub fn is_ok(&self) -> bool {
        self.errors.is_empty()
    }

    /// Total number of errors (fatal + warnings).
    #[must_use]
    pub fn error_count(&self) -> usize {
        self.errors.len()
    }

    /// Number of fatal errors.
    #[must_use]
    pub fn fatal_count(&self) -> usize {
        self.errors.iter().filter(|e| e.is_fatal()).count()
    }

    /// Return all errors as a slice.
    #[must_use]
    pub fn errors(&self) -> &[ValidationError] {
        &self.errors
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::edl_event::EdlEvent;

    fn make_event(number: u32, reel: &str, si: u64, so: u64, ri: u64, ro: u64) -> EdlEvent {
        EdlEvent::new(number, reel, EditType::Cut, si, so, ri, ro)
    }

    // --- EdlValidationRule tests ---

    #[test]
    fn test_rule_name_sequential() {
        assert_eq!(
            EdlValidationRule::SequentialNumbering.rule_name(),
            "SequentialNumbering"
        );
    }

    #[test]
    fn test_rule_name_valid_reel() {
        assert_eq!(
            EdlValidationRule::ValidReelName.rule_name(),
            "ValidReelName"
        );
    }

    #[test]
    fn test_fatal_rules_are_fatal() {
        assert!(EdlValidationRule::PositiveSourceDuration.is_fatal());
        assert!(EdlValidationRule::PositiveRecordDuration.is_fatal());
        assert!(EdlValidationRule::ValidReelName.is_fatal());
    }

    #[test]
    fn test_non_fatal_rules() {
        assert!(!EdlValidationRule::SequentialNumbering.is_fatal());
        assert!(!EdlValidationRule::ContinuousTimeline.is_fatal());
        assert!(!EdlValidationRule::WipeHasPattern.is_fatal());
    }

    // --- ValidationError tests ---

    #[test]
    fn test_validation_error_is_fatal() {
        let e = ValidationError::new(1, EdlValidationRule::PositiveSourceDuration, "bad");
        assert!(e.is_fatal());
    }

    #[test]
    fn test_validation_error_not_fatal() {
        let e = ValidationError::new(1, EdlValidationRule::WipeHasPattern, "no wipe number");
        assert!(!e.is_fatal());
    }

    // --- EdlValidator per-event tests ---

    #[test]
    fn test_validate_event_ok() {
        let v = EdlValidator::all_rules();
        let ev = make_event(1, "A001", 0, 25, 0, 25);
        assert!(v.validate_event(&ev).is_empty());
    }

    #[test]
    fn test_validate_event_empty_reel() {
        let v = EdlValidator::all_rules();
        let ev = make_event(1, "", 0, 25, 0, 25);
        let errs = v.validate_event(&ev);
        assert!(!errs.is_empty());
        assert!(errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::ValidReelName));
    }

    #[test]
    fn test_validate_event_reel_too_long() {
        let v = EdlValidator::all_rules();
        let ev = make_event(1, "TOOLONGNAME", 0, 25, 0, 25);
        let errs = v.validate_event(&ev);
        assert!(errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::ValidReelName));
    }

    #[test]
    fn test_validate_event_zero_source_duration() {
        let v = EdlValidator::all_rules();
        let ev = make_event(1, "A001", 10, 10, 0, 10);
        let errs = v.validate_event(&ev);
        assert!(errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::PositiveSourceDuration));
    }

    #[test]
    fn test_validate_event_zero_record_duration() {
        let v = EdlValidator::all_rules();
        let ev = make_event(1, "A001", 0, 25, 10, 10);
        let errs = v.validate_event(&ev);
        assert!(errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::PositiveRecordDuration));
    }

    #[test]
    fn test_validate_wipe_missing_pattern() {
        let v = EdlValidator::all_rules();
        let ev = EdlEvent::new(1, "A001", EditType::Wipe, 0, 10, 0, 10);
        let errs = v.validate_event(&ev);
        assert!(errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::WipeHasPattern));
    }

    #[test]
    fn test_validate_wipe_with_pattern_ok() {
        let v = EdlValidator::all_rules();
        let mut ev = EdlEvent::new(1, "A001", EditType::Wipe, 0, 10, 0, 10);
        ev.set_wipe_number(3);
        let errs = v.validate_event(&ev);
        assert!(!errs
            .iter()
            .any(|e| e.rule == EdlValidationRule::WipeHasPattern));
    }

    // --- EdlValidationReport tests ---

    #[test]
    fn test_report_ok_for_valid_list() {
        let v = EdlValidator::all_rules();
        let mut list = EdlEventList::new();
        list.add(make_event(1, "A001", 0, 25, 0, 25));
        list.add(make_event(2, "B001", 0, 25, 25, 50));
        let report = v.validate_list(&list);
        assert!(report.is_ok());
        assert!(!report.has_fatals());
    }

    #[test]
    fn test_report_detects_gap() {
        let v = EdlValidator::all_rules();
        let mut list = EdlEventList::new();
        list.add(make_event(1, "A001", 0, 25, 0, 25));
        // gap: record_in=30 but previous record_out=25
        list.add(make_event(2, "B001", 0, 25, 30, 55));
        let report = v.validate_list(&list);
        assert!(!report.is_ok());
        assert!(report
            .errors()
            .iter()
            .any(|e| e.rule == EdlValidationRule::ContinuousTimeline));
    }

    #[test]
    fn test_report_has_fatals_on_bad_reel() {
        let v = EdlValidator::all_rules();
        let mut list = EdlEventList::new();
        list.add(make_event(1, "", 0, 10, 0, 10));
        let report = v.validate_list(&list);
        assert!(report.has_fatals());
    }

    #[test]
    fn test_report_counts() {
        let v = EdlValidator::all_rules();
        let mut list = EdlEventList::new();
        list.add(make_event(1, "", 0, 0, 0, 0)); // 3 violations: reel, src, rec
        let report = v.validate_list(&list);
        assert!(report.error_count() >= 3);
        assert!(report.fatal_count() >= 2);
    }
}