Skip to main content

ralph/contracts/config/
plugin.rs

1//! Plugin configuration for enable/disable and per-plugin settings.
2//!
3//! Responsibilities:
4//! - Define plugin config structs and merge behavior for plugin activation.
5//!
6//! Not handled here:
7//! - Plugin loading and execution (see `crate::plugin` module).
8
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use std::collections::BTreeMap;
12
13/// Plugin configuration container.
14#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
15#[serde(default, deny_unknown_fields)]
16pub struct PluginsConfig {
17    /// Per-plugin configuration keyed by plugin id.
18    pub plugins: BTreeMap<String, PluginConfig>,
19}
20
21impl PluginsConfig {
22    pub fn merge_from(&mut self, other: Self) {
23        for (id, patch) in other.plugins {
24            self.plugins
25                .entry(id)
26                .and_modify(|existing| existing.merge_from(patch.clone()))
27                .or_insert(patch);
28        }
29    }
30}
31
32/// Per-plugin configuration.
33#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
34#[serde(default, deny_unknown_fields)]
35pub struct PluginConfig {
36    /// Enable/disable the plugin. If None, defaults to disabled.
37    pub enabled: Option<bool>,
38
39    /// Opaque plugin configuration blob (passed through to the plugin).
40    pub config: Option<serde_json::Value>,
41}
42
43impl PluginConfig {
44    pub fn merge_from(&mut self, other: Self) {
45        if other.enabled.is_some() {
46            self.enabled = other.enabled;
47        }
48        if other.config.is_some() {
49            self.config = other.config;
50        }
51    }
52}