ass-editor 0.1.1

High-performance, ergonomic editor layer for ASS subtitles
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Lazy validation wrapper around ass-core's ScriptAnalysis
//!
//! Provides on-demand validation and linting for editor documents,
//! wrapping ass-core's analysis capabilities with caching and
//! incremental update support for better editor performance.

use crate::core::{errors::EditorError, EditorDocument, Result};

#[cfg(feature = "analysis")]
use ass_core::analysis::{AnalysisConfig, ScriptAnalysis, ScriptAnalysisOptions};

#[cfg(feature = "analysis")]
use ass_core::analysis::linting::IssueSeverity;

#[cfg(not(feature = "std"))]
use alloc::{
    format,
    string::{String, ToString},
    vec::Vec,
};

#[cfg(feature = "std")]
use std::time::Instant;

/// Validation severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ValidationSeverity {
    /// Informational message
    Info,
    /// Warning that doesn't prevent script execution
    Warning,
    /// Error that may cause rendering issues
    Error,
    /// Critical error that prevents script execution
    Critical,
}

impl Default for ValidationSeverity {
    fn default() -> Self {
        Self::Info
    }
}

/// A validation issue found in the document
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationIssue {
    /// Severity of the issue
    pub severity: ValidationSeverity,

    /// Line number where the issue occurs (1-indexed)
    pub line: Option<usize>,

    /// Column number where the issue occurs (1-indexed)  
    pub column: Option<usize>,

    /// Human-readable description of the issue
    pub message: String,

    /// Rule or check that generated this issue
    pub rule: String,

    /// Suggested fix for the issue (if available)
    pub suggestion: Option<String>,
}

impl ValidationIssue {
    /// Create a new validation issue
    ///
    /// # Examples
    ///
    /// ```
    /// use ass_editor::utils::validator::{ValidationIssue, ValidationSeverity};
    ///
    /// let issue = ValidationIssue::new(
    ///     ValidationSeverity::Warning,
    ///     "Missing subtitle end time".to_string(),
    ///     "timing_check".to_string()
    /// )
    /// .at_location(10, 25)
    /// .with_suggestion("Add explicit end time".to_string());
    ///
    /// assert_eq!(issue.line, Some(10));
    /// assert_eq!(issue.column, Some(25));
    /// assert!(!issue.is_error());
    /// ```
    pub fn new(severity: ValidationSeverity, message: String, rule: String) -> Self {
        Self {
            severity,
            line: None,
            column: None,
            message,
            rule,
            suggestion: None,
        }
    }

    /// Set the location of this issue
    #[must_use]
    pub fn at_location(mut self, line: usize, column: usize) -> Self {
        self.line = Some(line);
        self.column = Some(column);
        self
    }

    /// Add a suggestion for fixing this issue
    #[must_use]
    pub fn with_suggestion(mut self, suggestion: String) -> Self {
        self.suggestion = Some(suggestion);
        self
    }

    /// Check if this is an error or critical issue
    #[must_use]
    pub const fn is_error(&self) -> bool {
        matches!(
            self.severity,
            ValidationSeverity::Error | ValidationSeverity::Critical
        )
    }

    /// Check if this is a warning or higher
    #[must_use]
    pub const fn is_warning_or_higher(&self) -> bool {
        matches!(
            self.severity,
            ValidationSeverity::Warning | ValidationSeverity::Error | ValidationSeverity::Critical
        )
    }
}

/// Configuration for the lazy validator
#[derive(Debug, Clone)]
pub struct ValidatorConfig {
    /// Enable automatic validation after document changes
    pub auto_validate: bool,

    /// Minimum time between validations
    #[cfg(feature = "std")]
    pub min_validation_interval: std::time::Duration,

    /// Maximum number of issues to report
    pub max_issues: usize,

    /// Severity threshold for reporting issues
    pub severity_threshold: ValidationSeverity,

    /// Enable specific validation rules
    pub enable_performance_hints: bool,
    pub enable_accessibility_checks: bool,
    pub enable_spec_compliance: bool,
    pub enable_unicode_checks: bool,
}

impl Default for ValidatorConfig {
    fn default() -> Self {
        Self {
            auto_validate: true,
            #[cfg(feature = "std")]
            min_validation_interval: std::time::Duration::from_millis(500),
            max_issues: 100,
            severity_threshold: ValidationSeverity::Info,
            enable_performance_hints: true,
            enable_accessibility_checks: true,
            enable_spec_compliance: true,
            enable_unicode_checks: true,
        }
    }
}

/// Validation results with caching and statistics
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// All validation issues found
    pub issues: Vec<ValidationIssue>,

    /// Time taken for validation in microseconds
    #[cfg(feature = "std")]
    pub validation_time_us: u64,

    /// Whether the document passed validation
    pub is_valid: bool,

    /// Number of warnings found
    pub warning_count: usize,

    /// Number of errors found
    pub error_count: usize,

    /// Validation timestamp for cache invalidation
    #[cfg(feature = "std")]
    pub timestamp: Instant,
}

impl ValidationResult {
    /// Create a new validation result
    pub fn new(issues: Vec<ValidationIssue>) -> Self {
        let warning_count = issues
            .iter()
            .filter(|i| i.severity == ValidationSeverity::Warning)
            .count();
        let error_count = issues.iter().filter(|i| i.is_error()).count();
        let is_valid = error_count == 0;

        Self {
            issues,
            #[cfg(feature = "std")]
            validation_time_us: 0,
            is_valid,
            warning_count,
            error_count,
            #[cfg(feature = "std")]
            timestamp: Instant::now(),
        }
    }

    /// Filter issues by severity
    pub fn issues_with_severity(&self, min_severity: ValidationSeverity) -> Vec<&ValidationIssue> {
        self.issues
            .iter()
            .filter(|i| i.severity >= min_severity)
            .collect()
    }

    /// Get summary statistics
    pub fn summary(&self) -> String {
        if self.is_valid {
            if self.warning_count > 0 {
                format!("{} warnings", self.warning_count)
            } else {
                "Valid".to_string()
            }
        } else {
            format!(
                "{} errors, {} warnings",
                self.error_count, self.warning_count
            )
        }
    }
}

/// Lazy validator that wraps ass-core's ScriptAnalysis
///
/// Provides on-demand validation with caching and incremental updates
/// as specified in the architecture (line 164).
#[derive(Debug)]
pub struct LazyValidator {
    /// Configuration for validation behavior
    config: ValidatorConfig,

    /// Cached validation result
    cached_result: Option<ValidationResult>,

    /// Hash of last validated content
    content_hash: u64,

    /// Last validation timestamp
    #[cfg(feature = "std")]
    last_validation: Option<Instant>,

    /// Core analysis configuration
    #[cfg(feature = "analysis")]
    analysis_config: AnalysisConfig,
}

impl LazyValidator {
    /// Create a new lazy validator with default configuration
    pub fn new() -> Self {
        Self::with_config(ValidatorConfig::default())
    }

    /// Create a new lazy validator with custom configuration
    pub fn with_config(config: ValidatorConfig) -> Self {
        Self {
            #[cfg(feature = "analysis")]
            analysis_config: AnalysisConfig {
                options: {
                    let mut options = ScriptAnalysisOptions::empty();
                    if config.enable_unicode_checks {
                        options |= ScriptAnalysisOptions::UNICODE_LINEBREAKS;
                    }
                    if config.enable_performance_hints {
                        options |= ScriptAnalysisOptions::PERFORMANCE_HINTS;
                    }
                    if config.enable_spec_compliance {
                        options |= ScriptAnalysisOptions::STRICT_COMPLIANCE;
                    }
                    if config.enable_accessibility_checks {
                        options |= ScriptAnalysisOptions::BIDI_ANALYSIS;
                    }
                    options
                },
                max_events_threshold: 1000,
            },
            config,
            cached_result: None,
            content_hash: 0,
            #[cfg(feature = "std")]
            last_validation: None,
        }
    }

    /// Validate document using ass-core's ScriptAnalysis
    pub fn validate(&mut self, document: &EditorDocument) -> Result<&ValidationResult> {
        let content = document.text();
        let content_hash = self.calculate_hash(&content);

        // Check if we can use cached result
        if self.should_use_cache(content_hash) {
            return self.cached_result.as_ref().ok_or_else(|| {
                EditorError::command_failed(
                    "Cache validation inconsistency: cached result expected but not found",
                )
            });
        }

        #[cfg(feature = "std")]
        let start_time = Instant::now();

        // Perform validation using ass-core
        let issues = self.validate_with_core(&content, document)?;

        // Update cache
        #[cfg(feature = "std")]
        let mut result = ValidationResult::new(issues);
        #[cfg(not(feature = "std"))]
        let result = ValidationResult::new(issues);

        #[cfg(feature = "std")]
        {
            result.validation_time_us = start_time.elapsed().as_micros() as u64;
        }

        self.cached_result = Some(result);
        self.content_hash = content_hash;

        #[cfg(feature = "std")]
        {
            self.last_validation = Some(Instant::now());
        }

        self.cached_result.as_ref().ok_or_else(|| {
            EditorError::command_failed("Validation completed but cached result is missing")
        })
    }

    /// Force validation even if cached result exists
    pub fn force_validate(&mut self, document: &EditorDocument) -> Result<&ValidationResult> {
        self.cached_result = None; // Clear cache
        self.validate(document)
    }

    /// Check if document is valid (quick check using cache if available)
    pub fn is_valid(&mut self, document: &EditorDocument) -> Result<bool> {
        Ok(self.validate(document)?.is_valid)
    }

    /// Get cached validation result without revalidating
    pub fn cached_result(&self) -> Option<&ValidationResult> {
        self.cached_result.as_ref()
    }

    /// Clear validation cache
    pub fn clear_cache(&mut self) {
        self.cached_result = None;
        self.content_hash = 0;
        #[cfg(feature = "std")]
        {
            self.last_validation = None;
        }
    }

    /// Update configuration
    pub fn set_config(&mut self, config: ValidatorConfig) {
        self.config = config;
        self.clear_cache(); // Config change invalidates cache

        #[cfg(feature = "analysis")]
        {
            self.analysis_config = AnalysisConfig {
                options: {
                    let mut options = ScriptAnalysisOptions::empty();
                    if self.config.enable_unicode_checks {
                        options |= ScriptAnalysisOptions::UNICODE_LINEBREAKS;
                    }
                    if self.config.enable_performance_hints {
                        options |= ScriptAnalysisOptions::PERFORMANCE_HINTS;
                    }
                    if self.config.enable_spec_compliance {
                        options |= ScriptAnalysisOptions::STRICT_COMPLIANCE;
                    }
                    if self.config.enable_accessibility_checks {
                        options |= ScriptAnalysisOptions::BIDI_ANALYSIS;
                    }
                    options
                },
                max_events_threshold: 1000,
            };
        }
    }

    /// Validate using ass-core's ScriptAnalysis
    #[cfg(feature = "analysis")]
    fn validate_with_core(
        &self,
        content: &str,
        document: &EditorDocument,
    ) -> Result<Vec<ValidationIssue>> {
        let mut issues = Vec::new();

        // Parse and analyze with ass-core
        document.parse_script_with(|script| {
            // Create ScriptAnalysis with our configuration
            match ScriptAnalysis::analyze_with_config(script, self.analysis_config.clone()) {
                Ok(analysis) => {
                    // Convert core lint issues to our format
                    for lint_issue in analysis.lint_issues() {
                        let severity = match lint_issue.severity() {
                            IssueSeverity::Hint => ValidationSeverity::Info,
                            IssueSeverity::Info => ValidationSeverity::Info,
                            IssueSeverity::Warning => ValidationSeverity::Warning,
                            IssueSeverity::Error => ValidationSeverity::Error,
                            IssueSeverity::Critical => ValidationSeverity::Critical,
                        };

                        let (line, column) = if let Some(location) = lint_issue.location() {
                            (Some(location.line), Some(location.column))
                        } else {
                            (None, None)
                        };

                        let issue = ValidationIssue {
                            severity,
                            line,
                            column,
                            message: lint_issue.message().to_string(),
                            rule: lint_issue.rule_id().to_string(),
                            suggestion: lint_issue.suggested_fix().map(|s| s.to_string()),
                        };

                        issues.push(issue);
                    }
                }
                Err(_) => {
                    // If analysis fails, add a basic error
                    issues.push(ValidationIssue::new(
                        ValidationSeverity::Error,
                        "Failed to analyze script".to_string(),
                        "analyzer".to_string(),
                    ));
                }
            }
        })?;

        // Add basic structural checks even with analysis feature
        self.add_basic_checks(content, &mut issues);

        // Apply severity threshold filter
        issues.retain(|issue| issue.severity >= self.config.severity_threshold);

        // Apply max issues limit
        if self.config.max_issues > 0 && issues.len() > self.config.max_issues {
            issues.truncate(self.config.max_issues);
        }

        Ok(issues)
    }

    /// Fallback validation without ass-core analysis
    #[cfg(not(feature = "analysis"))]
    fn validate_with_core(
        &self,
        content: &str,
        _document: &EditorDocument,
    ) -> Result<Vec<ValidationIssue>> {
        let mut issues = Vec::new();

        // Basic validation without core analysis
        // Note: We can't do full parsing validation without the analysis feature,
        // so we do basic structural checks only
        self.add_basic_checks(content, &mut issues);

        // Apply severity threshold filter
        issues.retain(|issue| issue.severity >= self.config.severity_threshold);

        // Apply max issues limit
        if self.config.max_issues > 0 && issues.len() > self.config.max_issues {
            issues.truncate(self.config.max_issues);
        }

        Ok(issues)
    }

    /// Add basic structural checks that work regardless of analysis feature
    fn add_basic_checks(&self, content: &str, issues: &mut Vec<ValidationIssue>) {
        // Basic checks
        if content.is_empty() {
            issues.push(ValidationIssue::new(
                ValidationSeverity::Warning,
                "Document is empty".to_string(),
                "basic".to_string(),
            ));
        }

        if !content.contains("[Script Info]") {
            issues.push(ValidationIssue::new(
                ValidationSeverity::Warning,
                "Missing [Script Info] section".to_string(),
                "structure".to_string(),
            ));
        }

        if !content.contains("[Events]") {
            issues.push(ValidationIssue::new(
                ValidationSeverity::Warning,
                "Missing [Events] section".to_string(),
                "structure".to_string(),
            ));
        }
    }

    /// Check if cached result can be used
    fn should_use_cache(&self, content_hash: u64) -> bool {
        if self.cached_result.is_none() || self.content_hash != content_hash {
            return false;
        }

        #[cfg(feature = "std")]
        {
            if let Some(last_validation) = self.last_validation {
                return last_validation.elapsed() < self.config.min_validation_interval;
            }
        }

        true
    }

    /// Calculate hash of content for cache invalidation
    fn calculate_hash(&self, content: &str) -> u64 {
        // Simple FNV hash
        let mut hash = 0xcbf29ce484222325u64;
        for byte in content.bytes() {
            hash ^= byte as u64;
            hash = hash.wrapping_mul(0x100000001b3);
        }
        hash
    }
}

impl Default for LazyValidator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::EditorDocument;
    #[cfg(not(feature = "std"))]
    use alloc::{string::ToString, vec};

    #[test]
    fn test_validation_issue_creation() {
        let issue = ValidationIssue::new(
            ValidationSeverity::Warning,
            "Test issue".to_string(),
            "test_rule".to_string(),
        )
        .at_location(10, 5)
        .with_suggestion("Fix this".to_string());

        assert_eq!(issue.severity, ValidationSeverity::Warning);
        assert_eq!(issue.line, Some(10));
        assert_eq!(issue.column, Some(5));
        assert_eq!(issue.suggestion, Some("Fix this".to_string()));
        assert!(issue.is_warning_or_higher());
        assert!(!issue.is_error());
    }

    #[test]
    fn test_validation_result() {
        let issues = vec![
            ValidationIssue::new(
                ValidationSeverity::Warning,
                "Warning".to_string(),
                "rule1".to_string(),
            ),
            ValidationIssue::new(
                ValidationSeverity::Error,
                "Error".to_string(),
                "rule2".to_string(),
            ),
        ];

        let result = ValidationResult::new(issues);
        assert!(!result.is_valid);
        assert_eq!(result.warning_count, 1);
        assert_eq!(result.error_count, 1);
        assert!(result.summary().contains("1 errors"));
    }

    #[test]
    fn test_lazy_validator() {
        let content = r#"[Script Info]
Title: Test

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,10,10,10,1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:05.00,0:00:10.00,Default,John,0,0,0,,Hello"#;

        let document = EditorDocument::from_content(content).unwrap();
        let mut validator = LazyValidator::new();

        let result = validator.validate(&document).unwrap();
        // Should pass basic validation
        assert!(result.is_valid);
        let issues_count = result.issues.len();

        // Test caching
        let result2 = validator.validate(&document).unwrap();
        assert_eq!(issues_count, result2.issues.len());
    }

    #[test]
    fn test_validator_config() {
        let config = ValidatorConfig {
            enable_performance_hints: false,
            max_issues: 5,
            severity_threshold: ValidationSeverity::Warning,
            ..Default::default()
        };

        let mut validator = LazyValidator::with_config(config);

        // Test config update
        let new_config = ValidatorConfig {
            max_issues: 10,
            ..Default::default()
        };
        validator.set_config(new_config);

        // Cache should be cleared
        assert!(validator.cached_result().is_none());
    }

    #[test]
    fn test_validation_with_missing_sections() {
        let content = "Title: Incomplete";
        let document = EditorDocument::from_content(content).unwrap();
        let mut validator = LazyValidator::new();

        let result = validator.validate(&document).unwrap();
        // Should have warnings about missing sections
        assert!(result.warning_count > 0);
        let warnings = result.issues_with_severity(ValidationSeverity::Warning);
        assert!(!warnings.is_empty());
    }
}