Skip to main content

ass_core/parser/script/
builder.rs

1//! Configurable builder for [`Script`] parsing.
2//!
3//! Provides [`ScriptBuilder`], a fluent entry point that optionally wires an
4//! extension registry of custom tag handlers and section processors into the
5//! parser before producing a [`Script`].
6
7use crate::parser::main::Parser;
8use crate::Result;
9
10#[cfg(feature = "plugins")]
11use crate::plugin::ExtensionRegistry;
12
13use super::Script;
14
15/// Builder for configuring script parsing with optional extensions
16///
17/// Provides a fluent API for setting up parsing configuration including
18/// extension registry for custom tag handlers and section processors.
19#[derive(Debug)]
20pub struct ScriptBuilder<'a> {
21    /// Extension registry for custom handlers
22    #[cfg(feature = "plugins")]
23    registry: Option<&'a ExtensionRegistry>,
24}
25
26impl<'a> ScriptBuilder<'a> {
27    /// Create a new script builder
28    #[must_use]
29    pub const fn new() -> Self {
30        Self {
31            #[cfg(feature = "plugins")]
32            registry: None,
33        }
34    }
35
36    /// Set the extension registry for custom tag handlers and section processors
37    ///
38    /// # Arguments
39    /// * `registry` - Registry containing custom extensions
40    #[cfg(feature = "plugins")]
41    #[must_use]
42    pub const fn with_registry(mut self, registry: &'a ExtensionRegistry) -> Self {
43        self.registry = Some(registry);
44        self
45    }
46
47    /// Parse ASS script with configured options
48    ///
49    /// # Arguments
50    /// * `source` - Source text to parse
51    ///
52    /// # Returns
53    /// Parsed script with zero-copy design
54    ///
55    /// # Errors
56    /// Returns an error if parsing fails due to malformed syntax
57    pub fn parse(self, source: &'a str) -> Result<Script<'a>> {
58        #[cfg(feature = "plugins")]
59        let parser = Parser::new_with_registry(source, self.registry);
60        #[cfg(not(feature = "plugins"))]
61        let parser = Parser::new(source);
62
63        Ok(parser.parse())
64    }
65}
66
67impl Default for ScriptBuilder<'_> {
68    fn default() -> Self {
69        Self::new()
70    }
71}