Skip to main content

ass_editor/extensions/
manager_access.rs

1//! Command execution and state/config accessors for the manager.
2//!
3//! Exposes command dispatch to loaded extensions plus read/write access to
4//! extension state, registered commands, configuration, and shared data.
5
6use crate::core::Result;
7
8use super::command::{ExtensionResult, ExtensionState};
9use super::context::ExtensionContext;
10use super::manager::ExtensionManager;
11
12#[cfg(feature = "std")]
13use std::collections::HashMap;
14
15#[cfg(not(feature = "std"))]
16use alloc::collections::BTreeMap as HashMap;
17
18#[cfg(not(feature = "std"))]
19use alloc::{format, string::String, vec::Vec};
20
21impl ExtensionManager {
22    /// Execute a command from an extension
23    pub fn execute_command(
24        &mut self,
25        command_id: &str,
26        args: &HashMap<String, String>,
27        context: &mut dyn ExtensionContext,
28    ) -> Result<ExtensionResult> {
29        let extension_name = self
30            .with_inner(|inner| inner.commands.get(command_id).map(|(name, _)| name.clone()))
31            .ok_or_else(|| crate::core::EditorError::CommandFailed {
32                message: format!("Command '{command_id}' not found"),
33            })?;
34
35        self.with_inner_mut(|inner| {
36            if let Some(extension) = inner.extensions.get_mut(&extension_name) {
37                extension.execute_command(command_id, args, context)
38            } else {
39                Err(crate::core::EditorError::CommandFailed {
40                    message: format!("Extension '{extension_name}' not found"),
41                })
42            }
43        })
44    }
45
46    /// Get list of loaded extensions
47    pub fn list_extensions(&self) -> Vec<String> {
48        self.with_inner(|inner| inner.extensions.keys().cloned().collect())
49    }
50
51    /// Get extension state
52    pub fn get_extension_state(&self, extension_name: &str) -> Option<ExtensionState> {
53        self.with_inner(|inner| inner.extension_states.get(extension_name).copied())
54    }
55
56    /// Get all available commands
57    pub fn list_commands(&self) -> Vec<String> {
58        self.with_inner(|inner| inner.commands.keys().cloned().collect())
59    }
60
61    /// Get configuration value
62    pub fn get_config(&self, key: &str) -> Option<String> {
63        self.with_inner(|inner| inner.config.get(key).cloned())
64    }
65
66    /// Set configuration value
67    pub fn set_config(&mut self, key: String, value: String) {
68        self.with_inner_mut(|inner| {
69            inner.config.insert(key, value);
70        });
71    }
72
73    /// Get extension data
74    pub fn get_extension_data(&self, extension_name: &str, key: &str) -> Option<String> {
75        self.with_inner(|inner| {
76            inner
77                .extension_data
78                .get(extension_name)
79                .and_then(|data| data.get(key))
80                .cloned()
81        })
82    }
83
84    /// Set extension data
85    pub fn set_extension_data(&mut self, extension_name: String, key: String, value: String) {
86        self.with_inner_mut(|inner| {
87            inner
88                .extension_data
89                .entry(extension_name)
90                .or_default()
91                .insert(key, value);
92        });
93    }
94}