zerolaunch_plugin_api/config/
configurable.rs1use crate::config::action::ConfigActionDef;
2use crate::config::component_core::ComponentCore;
3use crate::config::component_type::ComponentType;
4use crate::config::error::ConfigError;
5use crate::config::setting_def::{SettingDefinition, SettingsContribution};
6use async_trait::async_trait;
7
8#[async_trait]
12pub trait Configurable: Send + Sync {
13 fn core(&self) -> &ComponentCore;
17
18 fn component_id(&self) -> &str {
19 self.core().component_id()
20 }
21
22 fn component_name(&self) -> &str {
23 self.core().component_name()
24 }
25
26 fn component_type(&self) -> ComponentType {
27 self.core().component_type()
28 }
29
30 fn priority(&self) -> u32 {
32 self.core().priority()
33 }
34
35 fn component_description(&self) -> &str {
37 self.core().component_description()
38 }
39
40 fn setting_schema(&self) -> Vec<SettingDefinition>;
45
46 fn settings_contribution(&self) -> Result<SettingsContribution, ConfigError> {
51 SettingsContribution::from_entries(self.setting_schema())
52 .map_err(ConfigError::ValidationFailed)
53 }
54
55 fn get_settings(&self) -> serde_json::Value {
57 serde_json::json!({})
58 }
59
60 async fn apply_settings(&self, settings: serde_json::Value) -> Result<(), ConfigError> {
64 let _ = settings;
65 Ok(())
66 }
67
68 async fn validate_settings(&self, settings: &serde_json::Value) -> Result<(), ConfigError> {
71 let contribution = self.settings_contribution()?;
72 if contribution.properties.is_empty() && settings.is_null() {
74 return Ok(());
75 }
76 contribution
77 .validate_values(settings)
78 .map_err(ConfigError::ValidationFailed)
79 }
80
81 fn get_default_settings(&self) -> serde_json::Value {
82 match self.settings_contribution() {
83 Ok(c) => c.default_settings(),
84 Err(e) => {
85 tracing::warn!("获取默认设置失败: {}", e);
86 serde_json::Value::Object(serde_json::Map::new())
87 }
88 }
89 }
90
91 fn runtime_state(&self) -> Option<serde_json::Value> {
97 None
98 }
99
100 fn restore_runtime_state(&self, state: serde_json::Value) {
106 let _ = state;
107 }
108
109 fn on_settings_changed(&self) {}
110
111 fn config_actions(&self) -> Vec<ConfigActionDef> {
115 Vec::new()
116 }
117
118 async fn execute_config_action(
123 &self,
124 action: &str,
125 params: &serde_json::Value,
126 ) -> Result<serde_json::Value, String> {
127 let _ = params;
128 Err(format!("Unknown config action: {}", action))
129 }
130
131 fn default_enabled(&self) -> bool {
135 true
136 }
137}