Skip to main content

fresh_core/
config.rs

1//! Configuration types shared across crates
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7fn default_true() -> bool {
8    true
9}
10
11/// Configuration for a single plugin
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13#[schemars(extend("x-display-field" = "/enabled"))]
14pub struct PluginConfig {
15    /// Whether this plugin is enabled (default: true)
16    /// When disabled, the plugin will not be loaded or executed.
17    #[serde(default = "default_true")]
18    pub enabled: bool,
19
20    /// Path to the plugin file (populated automatically when scanning)
21    /// This is filled in by the plugin system and should not be set manually.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    #[schemars(extend("readOnly" = true))]
24    pub path: Option<PathBuf>,
25}
26
27impl Default for PluginConfig {
28    fn default() -> Self {
29        Self {
30            enabled: true,
31            path: None,
32        }
33    }
34}
35
36impl PluginConfig {
37    pub fn new_with_path(path: PathBuf) -> Self {
38        Self {
39            enabled: true,
40            path: Some(path),
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    /// An empty config `{}` deserializes with `enabled = true` (from the
50    /// `default_true` helper) and no path. An explicit `false` is preserved.
51    #[test]
52    fn enabled_defaults_to_true_when_missing() {
53        let c: PluginConfig = serde_json::from_str("{}").unwrap();
54        assert!(c.enabled);
55        assert!(c.path.is_none());
56
57        let c: PluginConfig = serde_json::from_str(r#"{"enabled": false}"#).unwrap();
58        assert!(!c.enabled);
59    }
60
61    /// `new_with_path` populates the path field, unlike `Default::default()`
62    /// which leaves it `None`.
63    #[test]
64    fn new_with_path_sets_path_and_enabled() {
65        let p = PathBuf::from("/plugins/foo.js");
66        let c = PluginConfig::new_with_path(p.clone());
67        assert!(c.enabled);
68        assert_eq!(c.path.as_ref(), Some(&p));
69    }
70}