Skip to main content

asterust_junos/system/
backup_router.rs

1//! `system { backup-router ... }` and `system { inet6-backup-router ... }`.
2
3use crate::{list_values, split_name};
4
5/// A `backup-router` or `inet6-backup-router`.
6#[derive(Clone, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct BackupRouter {
9    pub address: String,
10    /// Empty when the router is reachable for all destinations.
11    pub destinations: Vec<String>,
12}
13
14/// `<address> [destination <prefix | [ prefixes ]>]`.
15pub(super) fn parse_backup_router(arg: &str) -> BackupRouter {
16    let (address, rest) = split_name(arg);
17    BackupRouter {
18        address: address.to_string(),
19        destinations: rest.strip_prefix("destination").map(list_values).unwrap_or_default(),
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use crate::system::system;
26    use asterust::config::juniper;
27
28    #[test]
29    fn extracts_backup_routers() {
30        let system = system(&juniper::parse_braces(
31            "system {\n    backup-router 192.0.2.1 destination [ 10.0.0.0/8 172.16.0.0/12 ];\n    inet6-backup-router 2001:db8::1;\n}\n",
32        ))
33        .unwrap();
34        let backup = system.backup_router.unwrap();
35        assert_eq!(backup.address, "192.0.2.1");
36        assert_eq!(backup.destinations, ["10.0.0.0/8", "172.16.0.0/12"]);
37
38        let backup6 = system.inet6_backup_router.unwrap();
39        assert_eq!(backup6.address, "2001:db8::1");
40        assert!(backup6.destinations.is_empty());
41    }
42
43    #[test]
44    fn single_destination_backup_router() {
45        let system = system(&juniper::parse_braces(
46            "system {\n    backup-router 192.0.2.1 destination 10.0.0.0/8;\n}\n",
47        ))
48        .unwrap();
49        assert_eq!(system.backup_router.unwrap().destinations, ["10.0.0.0/8"]);
50    }
51}