Skip to main content

ass_core/plugin/
traits.rs

1//! Core extension traits and their result types.
2//!
3//! Defines [`TagHandler`] and [`SectionProcessor`] for processing custom ASS
4//! override tags and sections, along with the [`TagResult`] and
5//! [`SectionResult`] values they return.
6
7use alloc::string::String;
8
9/// Result of tag processing operations
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum TagResult {
12    /// Tag was successfully processed
13    Processed,
14    /// Tag was ignored (not handled by this processor)
15    Ignored,
16    /// Tag processing failed with error message
17    Failed(String),
18}
19
20/// Result of section processing operations
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum SectionResult {
23    /// Section was successfully processed
24    Processed,
25    /// Section was ignored (not handled by this processor)
26    Ignored,
27    /// Section processing failed with error message
28    Failed(String),
29}
30
31/// Trait for handling custom ASS override tags
32///
33/// Implementors can process custom tags that extend standard ASS functionality.
34/// Tag handlers are called during parsing when unknown tags are encountered.
35pub trait TagHandler: Send + Sync {
36    /// Unique name identifier for this tag handler
37    fn name(&self) -> &'static str;
38
39    /// Process a tag with its arguments
40    ///
41    /// # Arguments
42    /// * `args` - Raw tag arguments as string slice
43    ///
44    /// # Returns
45    /// * `TagResult::Processed` - Tag was handled successfully
46    /// * `TagResult::Ignored` - Tag not recognized by this handler
47    /// * `TagResult::Failed` - Error occurred during processing
48    fn process(&self, args: &str) -> TagResult;
49
50    /// Optional validation of tag arguments during parsing
51    fn validate(&self, args: &str) -> bool {
52        !args.is_empty()
53    }
54}
55
56/// Trait for handling custom ASS sections
57///
58/// Implementors can process non-standard sections that extend ASS functionality.
59/// Section processors are called when unknown section headers are encountered.
60pub trait SectionProcessor: Send + Sync {
61    /// Unique name identifier for this section processor
62    fn name(&self) -> &'static str;
63
64    /// Process section header and content lines
65    ///
66    /// # Arguments
67    /// * `header` - Section header (e.g., "Aegisub Project")
68    /// * `lines` - All lines belonging to this section
69    ///
70    /// # Returns
71    /// * `SectionResult::Processed` - Section was handled successfully
72    /// * `SectionResult::Ignored` - Section not recognized by this processor
73    /// * `SectionResult::Failed` - Error occurred during processing
74    fn process(&self, header: &str, lines: &[&str]) -> SectionResult;
75
76    /// Optional validation of section format
77    fn validate(&self, header: &str, lines: &[&str]) -> bool {
78        !header.is_empty() && !lines.is_empty()
79    }
80}