Skip to main content

asterust_junos/interfaces/unit/family/
address.rs

1//! `interfaces { <name> { unit <n> { family <name> { address <address> { ... } } } } }`.
2
3use crate::{active_children, has_leaf, split_name};
4use asterust::config::ConfigLine;
5
6/// A static `arp` entry of an address.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct AddressArp {
10    pub address: String,
11    pub mac: Option<String>,
12    /// The MAC is written with `multicast-mac`.
13    pub multicast_mac: bool,
14    /// The device also answers ARP requests for the address.
15    pub publish: bool,
16}
17
18/// An `address <address>` of a family, with its options. VRRP groups are not extracted.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct UnitAddress {
22    /// e.g. `"10.0.0.1/24"`.
23    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
64/// `<ip-address> (mac | multicast-mac) <mac-address> [publish]`.
65fn 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}