plugin_interfaces/
config.rs

1use std::fs;
2
3/// 插件配置结构
4#[derive(Debug, Clone)]
5pub struct PluginConfig {
6    pub id: String,
7    pub disabled: bool,
8    pub name: String,
9    pub description: String,
10    pub version: String,
11    pub author: Option<String>,
12}
13
14impl PluginConfig {
15    /// 从config.toml文件读取配置
16    pub fn from_file() -> Result<Self, Box<dyn std::error::Error>> {
17        // 读取当前目录下的config.toml文件
18        let config_content = fs::read_to_string("config.toml")?;
19
20        // 解析TOML配置
21        let config: toml::Value = toml::from_str(&config_content)?;
22
23        // 提取插件信息
24        let plugin = config
25            .get("plugin")
26            .ok_or("Missing [plugin] section in config.toml")?;
27
28        let id = plugin
29            .get("id")
30            .and_then(|v| v.as_str())
31            .ok_or("Missing 'id' in [plugin] section")?
32            .to_string();
33
34        let disabled = plugin
35            .get("disabled")
36            .and_then(|v| v.as_bool())
37            .unwrap_or(false);
38
39        let name = plugin
40            .get("name")
41            .and_then(|v| v.as_str())
42            .ok_or("Missing 'name' in [plugin] section")?
43            .to_string();
44
45        let description = plugin
46            .get("description")
47            .and_then(|v| v.as_str())
48            .ok_or("Missing 'description' in [plugin] section")?
49            .to_string();
50
51        let version = plugin
52            .get("version")
53            .and_then(|v| v.as_str())
54            .ok_or("Missing 'version' in [plugin] section")?
55            .to_string();
56
57        let author = plugin
58            .get("author")
59            .and_then(|v| v.as_str())
60            .map(|s| s.to_string());
61
62        Ok(PluginConfig {
63            id,
64            disabled,
65            name,
66            description,
67            version,
68            author,
69        })
70    }
71}