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