asterust_junos/interfaces/unit/family/
accounting.rs1use crate::{active_children, args_of, child_block, has_leaf, statement_words, value_after};
17use asterust::config::ConfigLine;
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct FamilyAccounting {
23 pub destination_class_usage: bool,
24 pub source_class_usage: bool,
25 pub source_class_usage_directions: Vec<String>,
27}
28
29#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct TargetedBroadcast {
33 pub forward_only: bool,
34 pub forward_and_send_to_re: bool,
35}
36
37#[derive(Clone, Debug, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct UnnumberedAddress {
41 pub interface: String,
43 pub preferred_source_address: Option<String>,
44 pub destination: Option<String>,
45 pub destination_profile: Option<String>,
46}
47
48#[derive(Clone, Debug, PartialEq, Eq)]
50#[non_exhaustive]
51pub struct VlanTranslation {
52 pub from: String,
53 pub to: String,
54}
55
56pub(super) fn parse_accounting(accounting: &ConfigLine) -> FamilyAccounting {
57 let source =
58 active_children(accounting).find(|c| c.text == "source-class-usage" || c.arg("source-class-usage").is_some());
59 FamilyAccounting {
60 destination_class_usage: has_leaf(accounting, "destination-class-usage"),
61 source_class_usage: source.is_some(),
62 source_class_usage_directions: source
63 .map(|line| {
64 line.text
65 .split_whitespace()
66 .skip(1)
67 .map(str::to_string)
68 .chain(active_children(line).map(|direction| direction.text.clone()))
69 .collect()
70 })
71 .unwrap_or_default(),
72 }
73}
74
75pub(super) fn parse_targeted_broadcast(broadcast: &ConfigLine) -> TargetedBroadcast {
76 TargetedBroadcast {
77 forward_only: has_leaf(broadcast, "forward-only"),
78 forward_and_send_to_re: has_leaf(broadcast, "forward-and-send-to-re"),
79 }
80}
81
82pub(super) fn parse_unnumbered_address(family: &ConfigLine) -> Option<UnnumberedAddress> {
83 let words = statement_words(family, "unnumbered-address")?;
84 Some(UnnumberedAddress {
85 interface: words.first()?.to_string(),
86 preferred_source_address: value_after(&words, "preferred-source-address"),
87 destination: value_after(&words, "destination"),
88 destination_profile: value_after(&words, "destination-profile"),
89 })
90}
91
92pub(super) fn parse_vlan_rewrite(family: &ConfigLine) -> Vec<VlanTranslation> {
93 child_block(family, "vlan-rewrite").map_or_else(Vec::new, |rewrite| {
94 args_of(rewrite, "translate")
95 .into_iter()
96 .filter_map(|arg| {
97 let mut ids = arg.split_whitespace();
98 Some(VlanTranslation {
99 from: ids.next()?.to_string(),
100 to: ids.next()?.to_string(),
101 })
102 })
103 .collect()
104 })
105}