Skip to main content

telos_agent/integrations/plugin/registry/
persistence.rs

1//! Plugin registry state persistence.
2
3use crate::integrations::plugin::registry::lifecycle::PluginRegistry;
4use crate::integrations::plugin::registry::types::PluginStatus;
5use crate::integrations::plugin::{PluginError, PluginId};
6use serde_json;
7use std::collections::HashMap;
8
9impl PluginRegistry {
10    /// Save enabled/disabled state to `plugin_state.json`.
11    pub fn save_state(&self) -> Result<(), PluginError> {
12        let state: HashMap<String, String> = self
13            .plugins
14            .iter()
15            .map(|(id, entry)| {
16                let status_str = match entry.status {
17                    PluginStatus::Enabled => "enabled",
18                    PluginStatus::Degraded => "degraded",
19                    PluginStatus::Disabled => "disabled",
20                    PluginStatus::Error => "error",
21                };
22                (id.to_string(), status_str.to_string())
23            })
24            .collect();
25
26        let json = serde_json::to_string_pretty(&serde_json::json!({
27            "version": 1,
28            "plugins": state,
29        }))?;
30
31        if let Some(parent) = self.state_path().parent() {
32            std::fs::create_dir_all(parent)?;
33        }
34        std::fs::write(self.state_path(), json)?;
35        Ok(())
36    }
37    /// Load enabled/disabled state from `plugin_state.json`.
38    pub fn load_state(&mut self) -> Result<(), PluginError> {
39        let path = self.state_path();
40        if !path.exists() {
41            return Ok(());
42        }
43
44        let content = std::fs::read_to_string(&path)?;
45        let value: serde_json::Value = serde_json::from_str(&content)?;
46
47        let plugins = value.get("plugins").and_then(|v| v.as_object());
48
49        if let Some(plugins) = plugins {
50            for (id_str, status_val) in plugins {
51                if let Some(id) = PluginId::parse(id_str)
52                    && let Some(status_str) = status_val.as_str()
53                    && let Some(entry) = self.plugins.get_mut(&id)
54                {
55                    match status_str {
56                        "enabled" => {
57                            entry.status = PluginStatus::Enabled;
58                            entry.plugin.enabled = true;
59                        }
60                        "degraded" => {
61                            entry.status = PluginStatus::Degraded;
62                            entry.plugin.enabled = true;
63                        }
64                        "disabled" => {
65                            entry.status = PluginStatus::Disabled;
66                            entry.plugin.enabled = false;
67                        }
68                        "error" => {
69                            entry.status = PluginStatus::Error;
70                        }
71                        _ => {}
72                    }
73                }
74            }
75        }
76
77        Ok(())
78    }
79}