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    /// Full theme registry (builtins + user + packages + bundles) as a JSON
28    /// object keyed by canonical registry key. Each value is the parsed theme
29    /// with `_key` / `_pack` metadata fields (see `ThemeRegistry::to_json_map`).
30    fn get_all_themes(&self) -> serde_json::Value;
31
32    /// Register custom i18n strings for a plugin
33    fn register_plugin_strings(
34        &self,
35        _plugin_name: &str,
36        _strings: HashMap<String, HashMap<String, String>>,
37    ) {
38    }
39
40    /// Unregister custom i18n strings for a plugin
41    fn unregister_plugin_strings(&self, _plugin_name: &str) {}
42
43    /// Register a plugin command
44    fn register_command(&self, command: crate::command::Command);
45
46    /// Unregister a command by name
47    fn unregister_command(&self, name: &str);
48
49    /// Unregister all commands with a given prefix
50    fn unregister_commands_by_prefix(&self, prefix: &str);
51
52    /// Unregister all commands registered by a specific plugin
53    fn unregister_commands_by_plugin(&self, plugin_name: &str);
54
55    /// Get the plugins directory path
56    fn plugins_dir(&self) -> std::path::PathBuf;
57
58    /// Get the config directory path
59    fn config_dir(&self) -> std::path::PathBuf;
60
61    /// Get the persistent data directory path (DirectoryContext::data_dir).
62    /// Used for long-lived plugin state such as review-diff comment history.
63    fn data_dir(&self) -> std::path::PathBuf;
64
65    /// Get theme data (JSON) by name from the in-memory cache.
66    fn get_theme_data(&self, _name: &str) -> Option<serde_json::Value> {
67        None
68    }
69
70    /// Save a theme file to the user themes directory.
71    /// Returns the path where the file was written.
72    fn save_theme_file(&self, _name: &str, _content: &str) -> Result<String, String> {
73        Err("not implemented".to_string())
74    }
75
76    /// Check if a user theme file exists (for overwrite confirmation).
77    fn theme_file_exists(&self, _name: &str) -> bool {
78        false
79    }
80}
81
82/// A no-op implementation of the service bridge for testing
83pub struct NoopServiceBridge;
84
85impl PluginServiceBridge for NoopServiceBridge {
86    fn as_any(&self) -> &dyn std::any::Any {
87        self
88    }
89    fn translate(&self, _plugin_name: &str, key: &str, _args: &HashMap<String, String>) -> String {
90        key.to_string()
91    }
92    fn current_locale(&self) -> String {
93        "en".to_string()
94    }
95    fn set_js_execution_state(&self, _state: String) {}
96    fn clear_js_execution_state(&self) {}
97    fn get_theme_schema(&self) -> serde_json::Value {
98        serde_json::Value::Null
99    }
100    fn get_builtin_themes(&self) -> serde_json::Value {
101        serde_json::Value::Null
102    }
103    fn get_all_themes(&self) -> serde_json::Value {
104        serde_json::Value::Null
105    }
106    fn register_plugin_strings(
107        &self,
108        _plugin_name: &str,
109        _strings: HashMap<String, HashMap<String, String>>,
110    ) {
111    }
112    fn unregister_plugin_strings(&self, _plugin_name: &str) {}
113    fn register_command(&self, _command: crate::command::Command) {}
114    fn unregister_command(&self, _name: &str) {}
115    fn unregister_commands_by_prefix(&self, _prefix: &str) {}
116    fn unregister_commands_by_plugin(&self, _plugin_name: &str) {}
117    fn plugins_dir(&self) -> std::path::PathBuf {
118        std::path::PathBuf::from("/tmp/plugins")
119    }
120    fn config_dir(&self) -> std::path::PathBuf {
121        std::path::PathBuf::from("/tmp/config")
122    }
123    fn data_dir(&self) -> std::path::PathBuf {
124        std::path::PathBuf::from("/tmp/data")
125    }
126}