Skip to main content

ass_core/tokenizer/state/
collector.rs

1//! Accumulation of tokenizer issues during lexical analysis.
2//!
3//! Defines [`IssueCollector`], a convenience container for gathering
4//! [`TokenIssue`] values produced while tokenizing an ASS script.
5
6use super::issue::TokenIssue;
7use alloc::{string::String, vec::Vec};
8
9/// Issue collector for accumulating tokenization problems
10///
11/// Provides convenient methods for collecting and managing tokenization
12/// issues during lexical analysis.
13#[derive(Debug, Clone, Default)]
14pub struct IssueCollector<'a> {
15    /// Collection of tokenization issues found during parsing
16    issues: Vec<TokenIssue<'a>>,
17}
18
19impl<'a> IssueCollector<'a> {
20    /// Create new empty issue collector
21    #[must_use]
22    pub const fn new() -> Self {
23        Self { issues: Vec::new() }
24    }
25
26    /// Add issue to collection
27    pub fn add_issue(&mut self, issue: TokenIssue<'a>) {
28        self.issues.push(issue);
29    }
30
31    /// Add warning issue
32    pub fn add_warning(&mut self, message: String, span: &'a str, line: usize, column: usize) {
33        self.add_issue(TokenIssue::warning(message, span, line, column));
34    }
35
36    /// Add error issue
37    pub fn add_error(&mut self, message: String, span: &'a str, line: usize, column: usize) {
38        self.add_issue(TokenIssue::error(message, span, line, column));
39    }
40
41    /// Add critical issue
42    pub fn add_critical(&mut self, message: String, span: &'a str, line: usize, column: usize) {
43        self.add_issue(TokenIssue::critical(message, span, line, column));
44    }
45
46    /// Get all collected issues
47    #[must_use]
48    pub fn issues(&self) -> &[TokenIssue<'a>] {
49        &self.issues
50    }
51
52    /// Check if any issues were collected
53    #[must_use]
54    pub fn has_issues(&self) -> bool {
55        !self.issues.is_empty()
56    }
57
58    /// Check if any error-level issues were collected
59    pub fn has_errors(&self) -> bool {
60        self.issues.iter().any(TokenIssue::is_error)
61    }
62
63    /// Get count of issues
64    #[must_use]
65    pub fn issue_count(&self) -> usize {
66        self.issues.len()
67    }
68
69    /// Clear all issues
70    pub fn clear(&mut self) {
71        self.issues.clear();
72    }
73
74    /// Take all issues, leaving collector empty
75    pub fn take_issues(&mut self) -> Vec<TokenIssue<'a>> {
76        core::mem::take(&mut self.issues)
77    }
78}