Skip to main content

asterust_junos/system/
commit.rs

1//! `system { commit { ... } }`.
2
3use crate::has_leaf;
4use asterust::config::ConfigLine;
5
6/// The `system commit` options. `peers`, which carries user credentials, is not extracted.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Commit {
10    pub synchronize: bool,
11    pub fast_synchronize: bool,
12    pub peers_synchronize: bool,
13    pub commit_synchronize_server: bool,
14    pub config_sync_with_scp: bool,
15    pub delta_export: bool,
16    pub force_commit_log: bool,
17    pub notification: bool,
18    pub server: bool,
19    /// `Some(true)` for `persist-groups-inheritance`, `Some(false)` for `no-persist-groups-inheritance`.
20    pub persist_groups_inheritance: Option<bool>,
21}
22
23pub(super) fn parse_commit(commit: &ConfigLine) -> Commit {
24    let persist_groups_inheritance = if has_leaf(commit, "persist-groups-inheritance") {
25        Some(true)
26    } else if has_leaf(commit, "no-persist-groups-inheritance") {
27        Some(false)
28    } else {
29        None
30    };
31    Commit {
32        synchronize: has_leaf(commit, "synchronize"),
33        fast_synchronize: has_leaf(commit, "fast-synchronize"),
34        peers_synchronize: has_leaf(commit, "peers-synchronize"),
35        commit_synchronize_server: has_leaf(commit, "commit-synchronize-server"),
36        config_sync_with_scp: has_leaf(commit, "config-sync-with-scp"),
37        delta_export: has_leaf(commit, "delta-export"),
38        force_commit_log: has_leaf(commit, "force-commit-log"),
39        notification: has_leaf(commit, "notification"),
40        server: has_leaf(commit, "server"),
41        persist_groups_inheritance,
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use crate::system::system;
48    use asterust::config::juniper;
49
50    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
51    #[test]
52    fn extracts_commit_options() {
53        let commit = system(&juniper::parse_braces(
54            r#"
55system {
56    commit {
57        synchronize;
58        fast-synchronize;
59        peers-synchronize;
60        commit-synchronize-server;
61        config-sync-with-scp;
62        delta-export;
63        force-commit-log;
64        notification;
65        server;
66        persist-groups-inheritance;
67    }
68}
69"#,
70        ))
71        .unwrap()
72        .commit
73        .unwrap();
74        assert!(commit.synchronize);
75        assert!(commit.fast_synchronize);
76        assert!(commit.peers_synchronize);
77        assert!(commit.commit_synchronize_server);
78        assert!(commit.config_sync_with_scp);
79        assert!(commit.delta_export);
80        assert!(commit.force_commit_log);
81        assert!(commit.notification);
82        assert!(commit.server);
83        assert_eq!(commit.persist_groups_inheritance, Some(true));
84    }
85
86    #[test]
87    fn commit_options_default_to_off() {
88        let commit = system(&juniper::parse_braces(
89            "system {\n    commit {\n        no-persist-groups-inheritance;\n    }\n}\n",
90        ))
91        .unwrap()
92        .commit
93        .unwrap();
94        assert!(!commit.synchronize);
95        assert!(!commit.delta_export);
96        assert_eq!(commit.persist_groups_inheritance, Some(false));
97    }
98
99    #[test]
100    fn unset_persist_groups_inheritance_is_none() {
101        let commit = system(&juniper::parse_braces(
102            "system {\n    commit {\n        synchronize;\n    }\n}\n",
103        ))
104        .unwrap()
105        .commit
106        .unwrap();
107        assert_eq!(commit.persist_groups_inheritance, None);
108    }
109
110    #[test]
111    fn no_commit_is_none() {
112        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
113        assert!(system.commit.is_none());
114    }
115}