1use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7fn default_true() -> bool {
8 true
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13#[schemars(extend("x-display-field" = "/enabled"))]
14pub struct PluginConfig {
15 #[serde(default = "default_true")]
18 pub enabled: bool,
19
20 #[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 #[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 #[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}