asterust-junos 0.5.10

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
//! `access-line`
//! `system { access-line { ... } }`.
//!
//! # See also
//!
//! - [access-line (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/access-line-edit-system.html)
//! - [attributes (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/attributes-edit-system-access-line.html)
//! - [dsl (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dsl-edit-system-access-line.html)
//! - [pon (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pon-edit-system-access-line.html)

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

/// The adjustments of one access line type, such as `adsl2-plus` or `type 145`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AccessLineAdjustment {
    /// The statement as written, e.g. `gfast` or `type 145`.
    pub line_type: String,
    pub overhead_adjust: Option<String>,
    pub overhead_bytes: Option<String>,
    pub total_adjust: Option<String>,
}

/// The `system access-line` rate adjustment settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AccessLine {
    /// `dsl` or `pon`: the TLVs kept when both are received.
    pub preference: Option<String>,
    pub dsl: Vec<AccessLineAdjustment>,
    pub pon: Vec<AccessLineAdjustment>,
}

fn adjustments(family: Option<&ConfigLine>) -> Vec<AccessLineAdjustment> {
    family
        .map(|family| {
            active_children(family)
                .map(|line| AccessLineAdjustment {
                    line_type: line.text.clone(),
                    overhead_adjust: arg_of(line, "overhead-adjust").map(str::to_string),
                    overhead_bytes: arg_of(line, "overhead-bytes").map(str::to_string),
                    total_adjust: arg_of(line, "total-adjust").map(str::to_string),
                })
                .collect()
        })
        .unwrap_or_default()
}

pub(super) fn parse_access_line(access_line: &ConfigLine) -> AccessLine {
    AccessLine {
        preference: child_block(access_line, "attributes")
            .and_then(|attributes| arg_of(attributes, "preference"))
            .map(str::to_string),
        dsl: adjustments(child_block(access_line, "dsl")),
        pon: adjustments(child_block(access_line, "pon")),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_the_access_line_adjustments() {
        let access_line = system(&juniper::parse_braces(
            r#"
system {
    access-line {
        attributes {
            preference dsl;
        }
        dsl {
            adsl2-plus {
                overhead-bytes 10;
                total-adjust 95;
            }
            gfast {
                overhead-adjust 90;
            }
            type 145 {
                overhead-bytes 4;
            }
        }
        pon {
            gpon {
                overhead-adjust 85;
                total-adjust 98;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .access_line
        .unwrap();
        assert_eq!(access_line.preference.as_deref(), Some("dsl"));
        assert_eq!(access_line.dsl.len(), 3);
        assert_eq!(access_line.dsl[0].line_type, "adsl2-plus");
        assert_eq!(access_line.dsl[0].overhead_bytes.as_deref(), Some("10"));
        assert_eq!(access_line.dsl[0].total_adjust.as_deref(), Some("95"));
        assert_eq!(access_line.dsl[0].overhead_adjust, None);
        assert_eq!(access_line.dsl[1].overhead_adjust.as_deref(), Some("90"));
        assert_eq!(access_line.dsl[2].line_type, "type 145");
        assert_eq!(access_line.pon[0].line_type, "gpon");
        assert_eq!(access_line.pon[0].overhead_adjust.as_deref(), Some("85"));
    }

    #[test]
    fn no_access_line_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.access_line.is_none());
    }
}