asterust-junos 0.5.13

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
//! `accept-source-mac` and `policer-overhead`
//! `interfaces { <name> { unit <n> { accept-source-mac { ... } policer-overhead { ... } } } }`.
//!
//! # See also
//!
//! - [accept-source-mac](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-source-mac-edit-interfaces.html)
//! - [accept-source-mac (SRX)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-accept-source-mac-srx.html)
//! - [mac-address (Accept Source Mac)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-address-edit-interfaces.html)
//! - [policer-overhead](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-policer-overhead.html)

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

/// A `mac-address` accepted as a source by `accept-source-mac`, with its policers.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AcceptedMac {
    pub address: String,
    pub policer_input: Option<String>,
    pub policer_output: Option<String>,
}

/// The `policer-overhead` of a unit, in bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PolicerOverhead {
    pub egress: Option<String>,
    pub ingress: Option<String>,
}

/// The `mac-address` entries of `accept-source-mac`.
pub(super) fn parse_accept_source_mac(unit: &ConfigLine) -> Vec<AcceptedMac> {
    let Some(accept) = child_block(unit, "accept-source-mac") else {
        return Vec::new();
    };
    active_children(accept)
        .filter_map(|entry| {
            // Written `mac-address <mac> [policer ...]` as a leaf or a block.
            let (address, _) = split_name(entry.arg("mac-address")?);
            let policer = child_block(entry, "policer");
            Some(AcceptedMac {
                address: address.to_string(),
                policer_input: policer.and_then(|p| arg_of(p, "input")).map(str::to_string),
                policer_output: policer.and_then(|p| arg_of(p, "output")).map(str::to_string),
            })
        })
        .collect()
}

pub(super) fn parse_policer_overhead(overhead: &ConfigLine) -> PolicerOverhead {
    PolicerOverhead {
        egress: arg_of(overhead, "egress").map(str::to_string),
        ingress: arg_of(overhead, "ingress").map(str::to_string),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_accept_source_mac_and_policer_overhead() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            accept-source-mac {
                mac-address 00:11:22:33:44:55 {
                    policer {
                        input pin;
                        output pout;
                    }
                }
                mac-address 00:11:22:33:44:66;
            }
            policer-overhead {
                egress 20;
                ingress 8;
            }
        }
        unit 1 {
            description plain;
        }
    }
}
"#,
        ));
        let units = &ifaces[0].units;
        let macs = &units[0].accept_source_mac;
        assert_eq!(macs.len(), 2);
        assert_eq!(macs[0].address, "00:11:22:33:44:55");
        assert_eq!(macs[0].policer_input.as_deref(), Some("pin"));
        assert_eq!(macs[0].policer_output.as_deref(), Some("pout"));
        assert_eq!(macs[1].address, "00:11:22:33:44:66");
        assert!(macs[1].policer_input.is_none());
        let overhead = units[0].policer_overhead.as_ref().unwrap();
        assert_eq!(overhead.egress.as_deref(), Some("20"));
        assert_eq!(overhead.ingress.as_deref(), Some("8"));
        assert!(units[1].accept_source_mac.is_empty() && units[1].policer_overhead.is_none());
    }
}