asterust-junos 0.5.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `fragmentation-maps`
//! `class-of-service { fragmentation-maps { <name> { forwarding-class <class> { ... } } } }`.
//!
//! # See also
//!
//! - [fragmentation-maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/fragmentation-maps-edit-cos-cs.html)

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

/// A `forwarding-class <name>` entry of a fragmentation map.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FragmentationClass {
    pub forwarding_class: String,
    /// Milliseconds.
    pub drop_timeout: Option<String>,
    /// Bytes.
    pub fragment_threshold: Option<String>,
    pub multilink_class: Option<String>,
    pub no_fragmentation: bool,
}

/// A `fragmentation-maps` entry.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FragmentationMap {
    pub name: String,
    pub classes: Vec<FragmentationClass>,
}

pub(super) fn parse_fragmentation_maps(cos: &ConfigLine) -> Vec<FragmentationMap> {
    let Some(block) = child_block(cos, "fragmentation-maps") else {
        return Vec::new();
    };
    active_children(block)
        .filter_map(|map| {
            Some(FragmentationMap {
                name: map.text.split_whitespace().next()?.to_string(),
                classes: named_children(map, "forwarding-class")
                    .map(|(class, line)| {
                        let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
                        FragmentationClass {
                            forwarding_class: class.to_string(),
                            drop_timeout: field("drop-timeout"),
                            fragment_threshold: field("fragment-threshold"),
                            multilink_class: field("multilink-class"),
                            no_fragmentation: has_leaf(line, "no-fragmentation"),
                        }
                    })
                    .collect(),
            })
        })
        .collect()
}

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

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
class-of-service {
    fragmentation-maps {
        FRAG {
            forwarding-class best-effort {
                fragment-threshold 128;
                drop-timeout 50;
            }
            forwarding-class expedited-forwarding {
                no-fragmentation;
            }
            forwarding-class assured-forwarding {
                multilink-class 2;
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_fragmentation_maps() {
        let maps = class_of_service(&juniper::parse_braces(EXTRA))
            .unwrap()
            .fragmentation_maps;
        assert_eq!(maps.len(), 1);
        assert_eq!(maps[0].name, "FRAG");
        let classes = &maps[0].classes;
        assert_eq!(classes.len(), 3);
        assert_eq!(classes[0].forwarding_class, "best-effort");
        assert_eq!(classes[0].fragment_threshold.as_deref(), Some("128"));
        assert_eq!(classes[0].drop_timeout.as_deref(), Some("50"));
        assert!(!classes[0].no_fragmentation);
        assert!(classes[1].no_fragmentation);
        assert_eq!(classes[2].multilink_class.as_deref(), Some("2"));
    }
}