Skip to main content

asterust_junos/system/
static_host_mapping.rs

1//! `system { static-host-mapping { <hostname> { ... } } }`.
2
3use crate::{active_children, arg_of, args_of, child_block, list_values, unquote};
4use asterust::config::ConfigLine;
5
6/// A `static-host-mapping <hostname>` entry.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct StaticHostMapping {
10    pub hostname: String,
11    pub aliases: Vec<String>,
12    pub inet: Vec<String>,
13    pub inet6: Vec<String>,
14    /// The ISO system identifier, e.g. `0100.2550.0001`.
15    pub sysid: Option<String>,
16}
17
18pub(super) fn static_host_mappings(system: &ConfigLine) -> Vec<StaticHostMapping> {
19    child_block(system, "static-host-mapping")
20        .map(|mappings| active_children(mappings).map(parse_static_host_mapping).collect())
21        .unwrap_or_default()
22}
23
24fn parse_static_host_mapping(mapping: &ConfigLine) -> StaticHostMapping {
25    let list = |keyword: &str| args_of(mapping, keyword).into_iter().flat_map(list_values).collect();
26    StaticHostMapping {
27        hostname: unquote(&mapping.text).to_string(),
28        aliases: list("alias"),
29        inet: list("inet"),
30        inet6: list("inet6"),
31        sysid: arg_of(mapping, "sysid").map(str::to_string),
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use crate::system::system;
38    use asterust::config::juniper;
39
40    #[test]
41    fn extracts_static_host_mappings() {
42        let system = system(&juniper::parse_braces(
43            r#"
44system {
45    static-host-mapping {
46        p1.example.net {
47            inet [ 10.255.0.2 10.255.1.2 ];
48            inet6 2001:db8::2;
49            alias [ p1 core1 ];
50            sysid 0100.2550.0002;
51        }
52        p2.example.net {
53            inet 10.255.0.3;
54        }
55    }
56}
57"#,
58        ))
59        .unwrap();
60        assert_eq!(system.static_host_mappings.len(), 2);
61
62        let p1 = &system.static_host_mappings[0];
63        assert_eq!(p1.hostname, "p1.example.net");
64        assert_eq!(p1.inet, ["10.255.0.2", "10.255.1.2"]);
65        assert_eq!(p1.inet6, ["2001:db8::2"]);
66        assert_eq!(p1.aliases, ["p1", "core1"]);
67        assert_eq!(p1.sysid.as_deref(), Some("0100.2550.0002"));
68
69        let p2 = &system.static_host_mappings[1];
70        assert_eq!(p2.inet, ["10.255.0.3"]);
71        assert!(p2.aliases.is_empty());
72        assert_eq!(p2.sysid, None);
73    }
74}