asterust-junos 0.4.3

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `scheduler-maps`
//! `class-of-service { scheduler-maps { <name> { forwarding-class <class> scheduler <scheduler>; } } }`.
//!
//! # See also
//!
//! - [scheduler-maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/scheduler-maps-edit-cos.html)
//! - [Example: Configuring and Applying Scheduler Maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/cos/topics/example/cos-scheduler-map-security-applying-security-configuring.html)

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

/// A `forwarding-class <class> scheduler <scheduler>` entry of a scheduler map.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SchedulerMapEntry {
    pub forwarding_class: String,
    pub scheduler: Option<String>,
}

/// A `scheduler-maps` entry.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SchedulerMap {
    pub name: String,
    pub entries: Vec<SchedulerMapEntry>,
}

fn parse_entry(arg: &str) -> SchedulerMapEntry {
    let (class, rest) = split_name(arg);
    let words: Vec<&str> = rest.split_whitespace().collect();
    SchedulerMapEntry {
        forwarding_class: class.to_string(),
        scheduler: value_after(&words, "scheduler"),
    }
}

pub(super) fn parse_scheduler_maps(cos: &ConfigLine) -> Vec<SchedulerMap> {
    let Some(block) = child_block(cos, "scheduler-maps") else {
        return Vec::new();
    };
    active_children(block)
        .filter_map(|map| {
            Some(SchedulerMap {
                name: map.text.split_whitespace().next()?.to_string(),
                entries: args_of(map, "forwarding-class").into_iter().map(parse_entry).collect(),
            })
        })
        .collect()
}

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

    // The `scheduler-maps` of Example: Configuring and Applying Scheduler Maps and Example: Configuring Priority
    // Scheduling (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
    // "Sources".
    const DOC_MAPS: &str = r#"
class-of-service {
    scheduler-maps {
        diffserv-cos-map {
            forwarding-class be-class scheduler be-scheduler;
            forwarding-class ef-class scheduler ef-scheduler;
            forwarding-class af-class scheduler af-scheduler;
            forwarding-class nc-class scheduler nc-scheduler;
        }
        be-map {
            forwarding-class best-effort scheduler be-sched;
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_scheduler_maps() {
        let maps = class_of_service(&juniper::parse_braces(DOC_MAPS))
            .unwrap()
            .scheduler_maps;
        assert_eq!(maps.len(), 2);
        assert_eq!(maps[0].name, "diffserv-cos-map");
        assert_eq!(maps[0].entries.len(), 4);
        assert_eq!(maps[0].entries[2].forwarding_class, "af-class");
        assert_eq!(maps[0].entries[2].scheduler.as_deref(), Some("af-scheduler"));
        assert_eq!(maps[1].name, "be-map");
        assert_eq!(maps[1].entries[0].forwarding_class, "best-effort");
        assert_eq!(maps[1].entries[0].scheduler.as_deref(), Some("be-sched"));
    }

    #[test]
    fn a_map_without_a_scheduler_has_none() {
        let cos = class_of_service(&juniper::parse_braces(
            "class-of-service {\n    scheduler-maps {\n        m {\n            forwarding-class be;\n        }\n    }\n}\n",
        ))
        .unwrap();
        assert_eq!(cos.scheduler_maps[0].entries[0].forwarding_class, "be");
        assert_eq!(cos.scheduler_maps[0].entries[0].scheduler, None);
    }
}