Skip to main content

ass_editor/extensions/registry_integration/
integration.rs

1//! Core `RegistryIntegration` type with construction, accessors, and custom
2//! handler registration.
3//!
4//! Holds the ass-core [`ExtensionRegistry`] together with the editor extensions
5//! that provide tag handlers and section processors. Built-in registration
6//! lives in the sibling `builtins` module.
7
8use crate::core::{EditorError, Result};
9use crate::extensions::EditorExtension;
10use ass_core::plugin::{ExtensionRegistry, SectionProcessor, TagHandler};
11
12#[cfg(not(feature = "std"))]
13use alloc::{boxed::Box, format, string::String, vec::Vec};
14
15/// Wrapper that connects editor extensions to ass-core's ExtensionRegistry
16pub struct RegistryIntegration {
17    /// The ass-core extension registry
18    pub(super) registry: ExtensionRegistry,
19    /// Editor extensions that provide tag handlers
20    pub(super) tag_providers: Vec<Box<dyn EditorExtension>>,
21    /// Editor extensions that provide section processors
22    pub(super) section_providers: Vec<Box<dyn EditorExtension>>,
23}
24
25impl core::fmt::Debug for RegistryIntegration {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        f.debug_struct("RegistryIntegration")
28            .field("registry", &self.registry)
29            .field(
30                "tag_providers",
31                &format!("{} providers", self.tag_providers.len()),
32            )
33            .field(
34                "section_providers",
35                &format!("{} providers", self.section_providers.len()),
36            )
37            .finish()
38    }
39}
40
41impl RegistryIntegration {
42    /// Create a new registry integration
43    pub fn new() -> Self {
44        Self {
45            registry: ExtensionRegistry::new(),
46            tag_providers: Vec::new(),
47            section_providers: Vec::new(),
48        }
49    }
50
51    /// Get the underlying ExtensionRegistry for use in parsing
52    pub fn registry(&self) -> &ExtensionRegistry {
53        &self.registry
54    }
55
56    /// Get mutable access to the registry
57    pub fn registry_mut(&mut self) -> &mut ExtensionRegistry {
58        &mut self.registry
59    }
60
61    /// Register a custom tag handler from an editor extension
62    pub fn register_custom_tag_handler(
63        &mut self,
64        extension_name: String,
65        handler: Box<dyn TagHandler>,
66    ) -> Result<()> {
67        self.registry
68            .register_tag_handler(handler)
69            .map_err(|e| EditorError::ExtensionError {
70                extension: extension_name,
71                message: format!("Failed to register tag handler: {e}"),
72            })
73    }
74
75    /// Register a custom section processor from an editor extension
76    pub fn register_custom_section_processor(
77        &mut self,
78        extension_name: String,
79        processor: Box<dyn SectionProcessor>,
80    ) -> Result<()> {
81        self.registry
82            .register_section_processor(processor)
83            .map_err(|e| EditorError::ExtensionError {
84                extension: extension_name,
85                message: format!("Failed to register section processor: {e}"),
86            })
87    }
88}
89
90impl Default for RegistryIntegration {
91    fn default() -> Self {
92        Self::new()
93    }
94}