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
//! `forwarding-class-accounting`
//! `interfaces { <name> { forwarding-class-accounting { ... } } }`.
//!
//! # See also
//!
//! - [forwarding-class-accounting](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forwarding-class-accounting-edit-interfaces.html)
//! - [direction (forwarding-class-accounting)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/direction-forwarding-class-accounting-edit-interfaces.html)
//! - [enhanced (forwarding-class-accounting)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enhanced-forwarding-class-accounting-edit-interfaces.html)

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

/// The `enhanced` block of `forwarding-class-accounting`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EnhancedForwardingClassAccounting {
    /// 0 to 255 bytes.
    pub overhead_bytes: Option<String>,
    /// `ucast` or `mcast`.
    pub traffic_type: Option<String>,
    /// `ipv4`, `ipv6` or `both`.
    pub family: Option<String>,
    /// `ingress`, `egress` or `both`.
    pub direction: Option<String>,
}

/// The `forwarding-class-accounting` of an interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ForwardingClassAccounting {
    /// `ingress`, `egress` or `both`.
    pub direction: Option<String>,
    pub enhanced: Option<EnhancedForwardingClassAccounting>,
}

pub(super) fn parse_forwarding_class_accounting(accounting: &ConfigLine) -> ForwardingClassAccounting {
    ForwardingClassAccounting {
        direction: arg_of(accounting, "direction").map(str::to_string),
        enhanced: child_block(accounting, "enhanced").map(|enhanced| EnhancedForwardingClassAccounting {
            overhead_bytes: arg_of(enhanced, "overhead-bytes").map(str::to_string),
            traffic_type: arg_of(enhanced, "traffic-type").map(str::to_string),
            family: arg_of(enhanced, "family").map(str::to_string),
            direction: arg_of(enhanced, "direction").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_forwarding_class_accounting() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    xe-0/0/0 {
        forwarding-class-accounting {
            direction both;
            enhanced {
                overhead-bytes 20;
                traffic-type ucast;
                family ipv4;
                direction egress;
            }
        }
    }
    xe-0/0/1 {
        forwarding-class-accounting;
    }
    xe-0/0/2 {
        disable;
    }
}
"#,
        ));
        let first = ifaces[0].forwarding_class_accounting.as_ref().unwrap();
        assert_eq!(first.direction.as_deref(), Some("both"));
        let enhanced = first.enhanced.as_ref().unwrap();
        assert_eq!(enhanced.overhead_bytes.as_deref(), Some("20"));
        assert_eq!(enhanced.traffic_type.as_deref(), Some("ucast"));
        assert_eq!(enhanced.family.as_deref(), Some("ipv4"));
        assert_eq!(enhanced.direction.as_deref(), Some("egress"));
        let bare = ifaces[1].forwarding_class_accounting.as_ref().unwrap();
        assert!(bare.direction.is_none() && bare.enhanced.is_none());
        assert!(ifaces[2].forwarding_class_accounting.is_none());
    }
}