Skip to main content

asterust_junos/system/
backup_router.rs

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