Skip to main content

fresh_core/
services.rs

1use std::collections::HashMap;
2
3/// Trait for the editor to provide services to the plugin runtime
4/// without the runtime depending directly on UI or complex system logic.
5pub trait PluginServiceBridge: Send + Sync + 'static {
6    /// Support downcasting for tests
7    fn as_any(&self) -> &dyn std::any::Any;
8
9    /// Translate a string for a plugin
10    fn translate(&self, plugin_name: &str, key: &str, args: &HashMap<String, String>) -> String;
11
12    /// Get the current locale
13    fn current_locale(&self) -> String;
14
15    /// Update the current JavaScript execution state (for debugging/signal handlers)
16    fn set_js_execution_state(&self, state: String);
17
18    /// Clear the JavaScript execution state
19    fn clear_js_execution_state(&self);
20
21    /// Get the JSON schema for themes
22    fn get_theme_schema(&self) -> serde_json::Value;
23
24    /// Get a list of builtin theme names
25    fn get_builtin_themes(&self) -> serde_json::Value;
26
27    /// Register custom i18n strings for a plugin
28    fn register_plugin_strings(
29        &self,
30        _plugin_name: &str,
31        _strings: HashMap<String, HashMap<String, String>>,
32    ) {
33    }
34
35    /// Unregister custom i18n strings for a plugin
36    fn unregister_plugin_strings(&self, _plugin_name: &str) {}
37
38    /// Register a plugin command
39    fn register_command(&self, command: crate::command::Command);
40
41    /// Unregister a command by name
42    fn unregister_command(&self, name: &str);
43
44    /// Unregister all commands with a given prefix
45    fn unregister_commands_by_prefix(&self, prefix: &str);
46
47    /// Unregister all commands registered by a specific plugin
48    fn unregister_commands_by_plugin(&self, plugin_name: &str);
49
50    /// Get the plugins directory path
51    fn plugins_dir(&self) -> std::path::PathBuf;
52
53    /// Get the config directory path
54    fn config_dir(&self) -> std::path::PathBuf;
55}
56
57/// A no-op implementation of the service bridge for testing
58pub struct NoopServiceBridge;
59
60impl PluginServiceBridge for NoopServiceBridge {
61    fn as_any(&self) -> &dyn std::any::Any {
62        self
63    }
64    fn translate(&self, _plugin_name: &str, key: &str, _args: &HashMap<String, String>) -> String {
65        key.to_string()
66    }
67    fn current_locale(&self) -> String {
68        "en".to_string()
69    }
70    fn set_js_execution_state(&self, _state: String) {}
71    fn clear_js_execution_state(&self) {}
72    fn get_theme_schema(&self) -> serde_json::Value {
73        serde_json::Value::Null
74    }
75    fn get_builtin_themes(&self) -> serde_json::Value {
76        serde_json::Value::Null
77    }
78    fn register_plugin_strings(
79        &self,
80        _plugin_name: &str,
81        _strings: HashMap<String, HashMap<String, String>>,
82    ) {
83    }
84    fn unregister_plugin_strings(&self, _plugin_name: &str) {}
85    fn register_command(&self, _command: crate::command::Command) {}
86    fn unregister_command(&self, _name: &str) {}
87    fn unregister_commands_by_prefix(&self, _prefix: &str) {}
88    fn unregister_commands_by_plugin(&self, _plugin_name: &str) {}
89    fn plugins_dir(&self) -> std::path::PathBuf {
90        std::path::PathBuf::from("/tmp/plugins")
91    }
92    fn config_dir(&self) -> std::path::PathBuf {
93        std::path::PathBuf::from("/tmp/config")
94    }
95}