use crate::{active_children, args_of, child_block, has_leaf, statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FamilyAccounting {
pub destination_class_usage: bool,
pub source_class_usage: bool,
pub source_class_usage_directions: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TargetedBroadcast {
pub forward_only: bool,
pub forward_and_send_to_re: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnnumberedAddress {
pub interface: String,
pub preferred_source_address: Option<String>,
pub destination: Option<String>,
pub destination_profile: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VlanTranslation {
pub from: String,
pub to: String,
}
pub(super) fn parse_accounting(accounting: &ConfigLine) -> FamilyAccounting {
let source =
active_children(accounting).find(|c| c.text == "source-class-usage" || c.arg("source-class-usage").is_some());
FamilyAccounting {
destination_class_usage: has_leaf(accounting, "destination-class-usage"),
source_class_usage: source.is_some(),
source_class_usage_directions: source
.map(|line| {
line.text
.split_whitespace()
.skip(1)
.map(str::to_string)
.chain(active_children(line).map(|direction| direction.text.clone()))
.collect()
})
.unwrap_or_default(),
}
}
pub(super) fn parse_targeted_broadcast(broadcast: &ConfigLine) -> TargetedBroadcast {
TargetedBroadcast {
forward_only: has_leaf(broadcast, "forward-only"),
forward_and_send_to_re: has_leaf(broadcast, "forward-and-send-to-re"),
}
}
pub(super) fn parse_unnumbered_address(family: &ConfigLine) -> Option<UnnumberedAddress> {
let words = statement_words(family, "unnumbered-address")?;
Some(UnnumberedAddress {
interface: words.first()?.to_string(),
preferred_source_address: value_after(&words, "preferred-source-address"),
destination: value_after(&words, "destination"),
destination_profile: value_after(&words, "destination-profile"),
})
}
pub(super) fn parse_vlan_rewrite(family: &ConfigLine) -> Vec<VlanTranslation> {
child_block(family, "vlan-rewrite").map_or_else(Vec::new, |rewrite| {
args_of(rewrite, "translate")
.into_iter()
.filter_map(|arg| {
let mut ids = arg.split_whitespace();
Some(VlanTranslation {
from: ids.next()?.to_string(),
to: ids.next()?.to_string(),
})
})
.collect()
})
}