asterust_junos/system/
scripts.rs1use crate::{arg_of, child_block, has_leaf, named_children};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Scripts {
10 pub commit_files: Vec<String>,
12 pub op_files: Vec<String>,
13 pub event_files: Vec<String>,
14 pub snmp_files: Vec<String>,
15 pub language: Option<String>,
17 pub synchronize: bool,
18 pub max_datasize: Option<String>,
19}
20
21pub(super) fn parse_scripts(scripts: &ConfigLine) -> Scripts {
22 let files = |kind: &str| -> Vec<String> {
23 child_block(scripts, kind)
24 .map(|block| {
25 named_children(block, "file")
26 .map(|(name, _)| name.to_string())
27 .collect()
28 })
29 .unwrap_or_default()
30 };
31 Scripts {
32 commit_files: files("commit"),
33 op_files: files("op"),
34 event_files: files("event"),
35 snmp_files: files("snmp"),
36 language: arg_of(scripts, "language").map(str::to_string),
37 synchronize: has_leaf(scripts, "synchronize"),
38 max_datasize: arg_of(scripts, "max-datasize").map(str::to_string),
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use crate::system::system;
45 use asterust::config::juniper;
46
47 #[test]
49 fn extracts_script_files_by_kind() {
50 let scripts = system(&juniper::parse_braces(
51 r#"
52system {
53 scripts {
54 language python3;
55 synchronize;
56 max-datasize 128m;
57 commit {
58 file check-mtu.slax;
59 file require-description.py {
60 optional;
61 }
62 }
63 op {
64 file show-bgp.py;
65 }
66 event {
67 file on-link-down.slax;
68 }
69 snmp {
70 file oid.py;
71 }
72 }
73}
74"#,
75 ))
76 .unwrap()
77 .scripts
78 .unwrap();
79 assert_eq!(scripts.commit_files, ["check-mtu.slax", "require-description.py"]);
80 assert_eq!(scripts.op_files, ["show-bgp.py"]);
81 assert_eq!(scripts.event_files, ["on-link-down.slax"]);
82 assert_eq!(scripts.snmp_files, ["oid.py"]);
83 assert_eq!(scripts.language.as_deref(), Some("python3"));
84 assert!(scripts.synchronize);
85 assert_eq!(scripts.max_datasize.as_deref(), Some("128m"));
86 }
87
88 #[test]
89 fn scripts_default_to_empty() {
90 let scripts = system(&juniper::parse_braces(
91 "system {\n scripts {\n synchronize;\n }\n}\n",
92 ))
93 .unwrap()
94 .scripts
95 .unwrap();
96 assert!(scripts.commit_files.is_empty());
97 assert!(scripts.op_files.is_empty());
98 assert_eq!(scripts.language, None);
99 }
100
101 #[test]
102 fn no_scripts_is_none() {
103 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
104 assert!(system.scripts.is_none());
105 }
106}