asterust_junos/interfaces/unit/family/
address.rs1use crate::{active_children, has_leaf, split_name};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct AddressArp {
10 pub address: String,
11 pub mac: Option<String>,
12 pub multicast_mac: bool,
14 pub publish: bool,
16}
17
18#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct UnitAddress {
22 pub address: String,
24 pub primary: bool,
25 pub preferred: bool,
26 pub master_only: bool,
27 pub eui_64: bool,
28 pub destination: Option<String>,
29 pub broadcast: Option<String>,
30 pub arps: Vec<AddressArp>,
31}
32
33pub(super) fn parse_addresses(family: &ConfigLine) -> Vec<UnitAddress> {
34 active_children(family)
35 .filter_map(|line| Some((line, line.arg("address")?)))
36 .map(|(line, arg)| parse_address(line, arg))
37 .collect()
38}
39
40fn parse_address(line: &ConfigLine, arg: &str) -> UnitAddress {
41 let (address, inline) = split_name(arg);
42 let inline: Vec<&str> = inline.split_whitespace().collect();
43 let flag = |name: &str| has_leaf(line, name) || inline.contains(&name);
44 let value = |keyword: &str| {
45 active_children(line)
46 .find_map(|c| c.arg(keyword))
47 .map(|arg| arg.trim_matches('"').to_string())
48 };
49 UnitAddress {
50 address: address.to_string(),
51 primary: flag("primary"),
52 preferred: flag("preferred"),
53 master_only: flag("master-only"),
54 eui_64: flag("eui-64"),
55 destination: value("destination"),
56 broadcast: value("broadcast"),
57 arps: active_children(line)
58 .filter_map(|c| c.arg("arp"))
59 .map(parse_arp)
60 .collect(),
61 }
62}
63
64fn parse_arp(arg: &str) -> AddressArp {
66 let mut words = arg.split_whitespace();
67 let address = words.next().unwrap_or_default().to_string();
68 let mut arp = AddressArp {
69 address,
70 mac: None,
71 multicast_mac: false,
72 publish: false,
73 };
74 while let Some(word) = words.next() {
75 match word {
76 "mac" => arp.mac = words.next().map(str::to_string),
77 "multicast-mac" => {
78 arp.multicast_mac = true;
79 arp.mac = words.next().map(str::to_string);
80 }
81 "publish" => arp.publish = true,
82 _ => {}
83 }
84 }
85 arp
86}