Skip to main content

ass_editor/extensions/
extension.rs

1//! Core extension trait and user-message handlers.
2//!
3//! Defines the `EditorExtension` trait that all extensions implement along
4//! with the `MessageHandler` trait and its std / `no_std` implementations.
5
6use crate::core::Result;
7use crate::events::DocumentEvent;
8
9use super::command::{ExtensionCommand, ExtensionResult, ExtensionState, MessageLevel};
10use super::context::ExtensionContext;
11use super::info::ExtensionInfo;
12
13#[cfg(feature = "std")]
14use std::collections::HashMap;
15
16#[cfg(not(feature = "std"))]
17use alloc::{collections::BTreeMap as HashMap, string::String, vec::Vec};
18
19/// Main extension trait that extensions must implement
20pub trait EditorExtension: Send + Sync {
21    /// Get extension metadata
22    fn info(&self) -> &ExtensionInfo;
23
24    /// Initialize the extension
25    fn initialize(&mut self, context: &mut dyn ExtensionContext) -> Result<()>;
26
27    /// Shutdown the extension
28    fn shutdown(&mut self, context: &mut dyn ExtensionContext) -> Result<()>;
29
30    /// Get the current state of the extension
31    fn state(&self) -> ExtensionState;
32
33    /// Execute a command provided by this extension
34    fn execute_command(
35        &mut self,
36        command_id: &str,
37        args: &HashMap<String, String>,
38        context: &mut dyn ExtensionContext,
39    ) -> Result<ExtensionResult>;
40
41    /// Get commands provided by this extension
42    fn commands(&self) -> Vec<ExtensionCommand> {
43        Vec::new()
44    }
45
46    /// Handle a document event (optional)
47    fn handle_event(
48        &mut self,
49        _event: &DocumentEvent,
50        _context: &mut dyn ExtensionContext,
51    ) -> Result<()> {
52        // Default implementation does nothing
53        Ok(())
54    }
55
56    /// Get configuration schema (optional)
57    fn config_schema(&self) -> HashMap<String, String> {
58        HashMap::new()
59    }
60
61    /// Validate configuration (optional)
62    fn validate_config(&self, _config: &HashMap<String, String>) -> Result<()> {
63        Ok(())
64    }
65
66    /// Pause the extension (optional)
67    fn pause(&mut self) -> Result<()> {
68        Ok(())
69    }
70
71    /// Resume the extension (optional)
72    fn resume(&mut self) -> Result<()> {
73        Ok(())
74    }
75
76    /// Get extension-specific data (optional)
77    fn get_data(&self, _key: &str) -> Option<String> {
78        None
79    }
80
81    /// Set extension-specific data (optional)
82    fn set_data(&mut self, _key: String, _value: String) -> Result<()> {
83        Ok(())
84    }
85}
86
87/// Message handler trait for showing messages to users
88pub trait MessageHandler: Send + Sync {
89    /// Show a message to the user
90    fn show(&mut self, message: &str, level: MessageLevel) -> Result<()>;
91}
92
93/// Default message handler implementation for std environments
94#[cfg(feature = "std")]
95pub struct StdMessageHandler;
96
97#[cfg(feature = "std")]
98impl MessageHandler for StdMessageHandler {
99    fn show(&mut self, message: &str, level: MessageLevel) -> Result<()> {
100        match level {
101            MessageLevel::Error => eprintln!("[ERROR] {message}"),
102            MessageLevel::Warning => eprintln!("[WARN] {message}"),
103            MessageLevel::Info => println!("[INFO] {message}"),
104            MessageLevel::Success => println!("[SUCCESS] {message}"),
105        }
106        Ok(())
107    }
108}
109
110/// No-op message handler for no_std environments
111#[cfg(not(feature = "std"))]
112pub struct NoOpMessageHandler;
113
114#[cfg(not(feature = "std"))]
115impl MessageHandler for NoOpMessageHandler {
116    fn show(&mut self, _message: &str, _level: MessageLevel) -> Result<()> {
117        Ok(())
118    }
119}