use crate::config::action::ConfigActionDef;
use crate::config::component_core::ComponentCore;
use crate::config::component_type::ComponentType;
use crate::config::error::ConfigError;
use crate::config::setting_def::{SettingDefinition, SettingsContribution};
use async_trait::async_trait;
#[async_trait]
pub trait Configurable: Send + Sync {
fn core(&self) -> &ComponentCore;
fn component_id(&self) -> &str {
self.core().component_id()
}
fn component_name(&self) -> &str {
self.core().component_name()
}
fn component_type(&self) -> ComponentType {
self.core().component_type()
}
fn priority(&self) -> u32 {
self.core().priority()
}
fn component_description(&self) -> &str {
self.core().component_description()
}
fn setting_schema(&self) -> Vec<SettingDefinition>;
fn settings_contribution(&self) -> Result<SettingsContribution, ConfigError> {
SettingsContribution::from_entries(self.setting_schema())
.map_err(ConfigError::ValidationFailed)
}
fn get_settings(&self) -> serde_json::Value {
serde_json::json!({})
}
async fn apply_settings(&self, settings: serde_json::Value) -> Result<(), ConfigError> {
let _ = settings;
Ok(())
}
async fn validate_settings(&self, settings: &serde_json::Value) -> Result<(), ConfigError> {
let contribution = self.settings_contribution()?;
if contribution.properties.is_empty() && settings.is_null() {
return Ok(());
}
contribution
.validate_values(settings)
.map_err(ConfigError::ValidationFailed)
}
fn get_default_settings(&self) -> serde_json::Value {
match self.settings_contribution() {
Ok(c) => c.default_settings(),
Err(e) => {
tracing::warn!("获取默认设置失败: {}", e);
serde_json::Value::Object(serde_json::Map::new())
}
}
}
fn runtime_state(&self) -> Option<serde_json::Value> {
None
}
fn restore_runtime_state(&self, state: serde_json::Value) {
let _ = state;
}
fn on_settings_changed(&self) {}
fn config_actions(&self) -> Vec<ConfigActionDef> {
Vec::new()
}
async fn execute_config_action(
&self,
action: &str,
params: &serde_json::Value,
) -> Result<serde_json::Value, String> {
let _ = params;
Err(format!("Unknown config action: {}", action))
}
fn default_enabled(&self) -> bool {
true
}
}