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