asterust-junos 0.5.11

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
//! `proxy`
//! `system { proxy { ... } }`.
//!
//! # See also
//!
//! - [proxy (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/proxy-edit-system-proxy.html)

use crate::{arg_of, secret_value, Secret};
use asterust::config::ConfigLine;

/// The `system proxy` server settings. The `password` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Proxy {
    /// A hostname or IP address.
    pub server: Option<String>,
    pub port: Option<String>,
    pub username: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub password: Option<Secret>,
}

pub(super) fn parse_proxy(proxy: &ConfigLine) -> Proxy {
    Proxy {
        server: arg_of(proxy, "server").map(str::to_string),
        port: arg_of(proxy, "port").map(str::to_string),
        username: arg_of(proxy, "username").map(str::to_string),
        password: secret_value(arg_of(proxy, "password")),
    }
}

#[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_proxy_settings_without_the_password() {
        let proxy = system(&juniper::parse_braces(
            r#"
system {
    proxy {
        server proxy.example.net;
        port 3128;
        username ops;
        password "$9$secret"; ## SECRET-DATA
    }
}
"#,
        ))
        .unwrap()
        .proxy
        .unwrap();
        assert_eq!(proxy.server.as_deref(), Some("proxy.example.net"));
        assert_eq!(proxy.port.as_deref(), Some("3128"));
        assert_eq!(proxy.username.as_deref(), Some("ops"));
        assert_eq!(
            proxy.password.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
    }

    #[test]
    fn no_proxy_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.proxy.is_none());
    }
}