Skip to main content

asterust_junos/interfaces/unit/
mod.rs

1//! `interfaces { <name> { unit <n> { ... } } }`.
2
3mod family;
4
5pub use family::{
6    AddressArp, BandwidthThreshold, FamilyFilter, FamilyPolicer, FamilySampling, RpfCheck, TrackedInterface,
7    TrackedRoute, UnitAddress, UnitFamily, VrrpGroup, VrrpInheritFrom, VrrpTrack,
8};
9
10use crate::{arg_of, args_of, named_children};
11use asterust::config::ConfigLine;
12use family::parse_family;
13
14/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g. `"10.0.0.1/24"`;
15/// IS-IS NETs for `family iso`).
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct InterfaceUnit {
19    pub number: String,
20    pub description: Option<String>,
21    pub vlan_id: Option<u32>,
22    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
23    pub families: Vec<String>,
24    pub inet_addresses: Vec<String>,
25    pub inet6_addresses: Vec<String>,
26    pub iso_addresses: Vec<String>,
27    /// The settings of each `family`, in the order written.
28    pub family_settings: Vec<UnitFamily>,
29}
30
31pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
32    let families: Vec<_> = named_children(unit, "family").collect();
33    let addresses = |family: &str| -> Vec<String> {
34        families
35            .iter()
36            .filter(|(name, _)| *name == family)
37            .flat_map(|(_, f)| args_of(f, "address"))
38            .map(str::to_string)
39            .collect()
40    };
41    InterfaceUnit {
42        number: number.to_string(),
43        description: arg_of(unit, "description").map(str::to_string),
44        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
45        inet_addresses: addresses("inet"),
46        inet6_addresses: addresses("inet6"),
47        iso_addresses: addresses("iso"),
48        families: families.iter().map(|(name, _)| name.to_string()).collect(),
49        family_settings: families
50            .iter()
51            .map(|(name, family)| parse_family(name, family))
52            .collect(),
53    }
54}