use crate::{arg_of, child_block, has_leaf, split_keyword, statements, unquote};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Policer {
pub name: String,
pub bandwidth_limit: Option<String>,
pub bandwidth_percent: Option<String>,
pub burst_size_limit: Option<String>,
pub discard: bool,
pub forwarding_class: Option<String>,
pub loss_priority: Option<String>,
pub pps_limit: Option<String>,
pub packet_burst: Option<String>,
pub filter_specific: bool,
pub logical_interface_policer: bool,
pub physical_interface_policer: bool,
pub logical_bandwidth_policer: bool,
pub shared_bandwidth_policer: bool,
}
pub(super) fn parse_policer(name: &str, line: &ConfigLine) -> Policer {
let limit = |keyword: &str| {
child_block(line, "if-exceeding")
.and_then(|block| arg_of(block, keyword))
.map(str::to_string)
};
let mut policer = Policer {
name: name.to_string(),
bandwidth_limit: limit("bandwidth-limit"),
bandwidth_percent: limit("bandwidth-percent"),
burst_size_limit: limit("burst-size-limit"),
discard: false,
forwarding_class: None,
loss_priority: None,
pps_limit: child_block(line, "if-exceeding-pps")
.and_then(|b| arg_of(b, "pps-limit"))
.map(str::to_string),
packet_burst: child_block(line, "if-exceeding-pps")
.and_then(|b| arg_of(b, "packet-burst"))
.map(str::to_string),
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"),
logical_bandwidth_policer: has_leaf(line, "logical-bandwidth-policer"),
shared_bandwidth_policer: has_leaf(line, "shared-bandwidth-policer"),
};
for statement in statements(line, "then") {
let (action, arg) = split_keyword(statement);
match action {
"discard" => policer.discard = true,
"forwarding-class" => policer.forwarding_class = Some(unquote(arg).to_string()),
"loss-priority" => policer.loss_priority = Some(unquote(arg).to_string()),
_ => {}
}
}
policer
}