use crate::{active_children, arg_of, child_block};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AccessLineAdjustment {
pub line_type: String,
pub overhead_adjust: Option<String>,
pub overhead_bytes: Option<String>,
pub total_adjust: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AccessLine {
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;
#[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());
}
}