asterust-junos 0.5.12

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `reverse`
//! `system { services { reverse { ... } } }`.
//!
//! # See also
//!
//! - [reverse](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-services-reverse.html)

use crate::{arg_of, child_block};
use asterust::config::ConfigLine;

/// The `system services reverse` ports for connections to the device on the AUX port.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Reverse {
    pub ssh_port: Option<String>,
    pub telnet_port: Option<String>,
}

pub(super) fn parse_reverse(reverse: &ConfigLine) -> Reverse {
    let port = |service: &str| {
        child_block(reverse, service)
            .and_then(|block| arg_of(block, "port"))
            .map(str::to_string)
    };
    Reverse {
        ssh_port: port("ssh"),
        telnet_port: port("telnet"),
    }
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_reverse_ports() {
        let reverse = system(&juniper::parse_braces(
            "system {\n    services {\n        reverse {\n            ssh {\n                port 7001;\n            }\n            telnet {\n                port 7002;\n            }\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .reverse
        .unwrap();
        assert_eq!(reverse.ssh_port.as_deref(), Some("7001"));
        assert_eq!(reverse.telnet_port.as_deref(), Some("7002"));
    }

    #[test]
    fn no_reverse_is_none() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        assert!(services.reverse.is_none());
    }
}