Skip to main content

asterust_junos/interfaces/
forwarding_class_accounting.rs

1//! `forwarding-class-accounting`
2//! `interfaces { <name> { forwarding-class-accounting { ... } } }`.
3//!
4//! # See also
5//!
6//! - [forwarding-class-accounting](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forwarding-class-accounting-edit-interfaces.html)
7//! - [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)
8//! - [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)
9
10use crate::{arg_of, child_block};
11use asterust::config::ConfigLine;
12
13/// The `enhanced` block of `forwarding-class-accounting`.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct EnhancedForwardingClassAccounting {
17    /// 0 to 255 bytes.
18    pub overhead_bytes: Option<String>,
19    /// `ucast` or `mcast`.
20    pub traffic_type: Option<String>,
21    /// `ipv4`, `ipv6` or `both`.
22    pub family: Option<String>,
23    /// `ingress`, `egress` or `both`.
24    pub direction: Option<String>,
25}
26
27/// The `forwarding-class-accounting` of an interface.
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct ForwardingClassAccounting {
31    /// `ingress`, `egress` or `both`.
32    pub direction: Option<String>,
33    pub enhanced: Option<EnhancedForwardingClassAccounting>,
34}
35
36pub(super) fn parse_forwarding_class_accounting(accounting: &ConfigLine) -> ForwardingClassAccounting {
37    ForwardingClassAccounting {
38        direction: arg_of(accounting, "direction").map(str::to_string),
39        enhanced: child_block(accounting, "enhanced").map(|enhanced| EnhancedForwardingClassAccounting {
40            overhead_bytes: arg_of(enhanced, "overhead-bytes").map(str::to_string),
41            traffic_type: arg_of(enhanced, "traffic-type").map(str::to_string),
42            family: arg_of(enhanced, "family").map(str::to_string),
43            direction: arg_of(enhanced, "direction").map(str::to_string),
44        }),
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::interfaces::interfaces;
51    use asterust::config::juniper;
52
53    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
54    #[test]
55    fn extracts_forwarding_class_accounting() {
56        let ifaces = interfaces(&juniper::parse_braces(
57            r#"
58interfaces {
59    xe-0/0/0 {
60        forwarding-class-accounting {
61            direction both;
62            enhanced {
63                overhead-bytes 20;
64                traffic-type ucast;
65                family ipv4;
66                direction egress;
67            }
68        }
69    }
70    xe-0/0/1 {
71        forwarding-class-accounting;
72    }
73    xe-0/0/2 {
74        disable;
75    }
76}
77"#,
78        ));
79        let first = ifaces[0].forwarding_class_accounting.as_ref().unwrap();
80        assert_eq!(first.direction.as_deref(), Some("both"));
81        let enhanced = first.enhanced.as_ref().unwrap();
82        assert_eq!(enhanced.overhead_bytes.as_deref(), Some("20"));
83        assert_eq!(enhanced.traffic_type.as_deref(), Some("ucast"));
84        assert_eq!(enhanced.family.as_deref(), Some("ipv4"));
85        assert_eq!(enhanced.direction.as_deref(), Some("egress"));
86        let bare = ifaces[1].forwarding_class_accounting.as_ref().unwrap();
87        assert!(bare.direction.is_none() && bare.enhanced.is_none());
88        assert!(ifaces[2].forwarding_class_accounting.is_none());
89    }
90}