use crate::{active_children, arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ThreeColorRate {
pub color_aware: Option<bool>,
pub committed_information_rate: Option<String>,
pub committed_burst_size: Option<String>,
pub excess_burst_size: Option<String>,
pub peak_information_rate: Option<String>,
pub peak_burst_size: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ThreeColorPolicer {
pub name: String,
pub actions: Vec<String>,
pub filter_specific: bool,
pub logical_interface_policer: bool,
pub physical_interface_policer: bool,
pub shared_bandwidth_policer: bool,
pub single_rate: Option<ThreeColorRate>,
pub two_rate: Option<ThreeColorRate>,
}
pub(super) fn parse_three_color_policer(name: &str, line: &ConfigLine) -> ThreeColorPolicer {
ThreeColorPolicer {
name: name.to_string(),
actions: child_block(line, "action")
.map(|action| active_children(action).map(|c| c.text.clone()).collect())
.unwrap_or_default(),
filter_specific: has_leaf(line, "filter-specific"),
logical_interface_policer: has_leaf(line, "logical-interface-policer"),
physical_interface_policer: has_leaf(line, "physical-interface-policer"),
shared_bandwidth_policer: has_leaf(line, "shared-bandwidth-policer"),
single_rate: child_block(line, "single-rate").map(parse_rate),
two_rate: child_block(line, "two-rate").map(parse_rate),
}
}
fn parse_rate(rate: &ConfigLine) -> ThreeColorRate {
let field = |keyword: &str| arg_of(rate, keyword).map(str::to_string);
ThreeColorRate {
color_aware: if has_leaf(rate, "color-aware") {
Some(true)
} else if has_leaf(rate, "color-blind") {
Some(false)
} else {
None
},
committed_information_rate: field("committed-information-rate"),
committed_burst_size: field("committed-burst-size"),
excess_burst_size: field("excess-burst-size"),
peak_information_rate: field("peak-information-rate"),
peak_burst_size: field("peak-burst-size"),
}
}