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    /// Get theme data (JSON) by name from the in-memory cache.
57    fn get_theme_data(&self, _name: &str) -> Option<serde_json::Value> {
58        None
59    }
60
61    /// Save a theme file to the user themes directory.
62    /// Returns the path where the file was written.
63    fn save_theme_file(&self, _name: &str, _content: &str) -> Result<String, String> {
64        Err("not implemented".to_string())
65    }
66
67    /// Check if a user theme file exists (for overwrite confirmation).
68    fn theme_file_exists(&self, _name: &str) -> bool {
69        false
70    }
71}
72
73/// A no-op implementation of the service bridge for testing
74pub struct NoopServiceBridge;
75
76impl PluginServiceBridge for NoopServiceBridge {
77    fn as_any(&self) -> &dyn std::any::Any {
78        self
79    }
80    fn translate(&self, _plugin_name: &str, key: &str, _args: &HashMap<String, String>) -> String {
81        key.to_string()
82    }
83    fn current_locale(&self) -> String {
84        "en".to_string()
85    }
86    fn set_js_execution_state(&self, _state: String) {}
87    fn clear_js_execution_state(&self) {}
88    fn get_theme_schema(&self) -> serde_json::Value {
89        serde_json::Value::Null
90    }
91    fn get_builtin_themes(&self) -> serde_json::Value {
92        serde_json::Value::Null
93    }
94    fn register_plugin_strings(
95        &self,
96        _plugin_name: &str,
97        _strings: HashMap<String, HashMap<String, String>>,
98    ) {
99    }
100    fn unregister_plugin_strings(&self, _plugin_name: &str) {}
101    fn register_command(&self, _command: crate::command::Command) {}
102    fn unregister_command(&self, _name: &str) {}
103    fn unregister_commands_by_prefix(&self, _prefix: &str) {}
104    fn unregister_commands_by_plugin(&self, _plugin_name: &str) {}
105    fn plugins_dir(&self) -> std::path::PathBuf {
106        std::path::PathBuf::from("/tmp/plugins")
107    }
108    fn config_dir(&self) -> std::path::PathBuf {
109        std::path::PathBuf::from("/tmp/config")
110    }
111}