Skip to main content

asterust_junos/system/
location.rs

1//! `system { location { ... } }`.
2
3use crate::arg_of;
4use asterust::config::ConfigLine;
5
6/// The `system location` block. The Bellcore coordinates (`hcoord`, `vcoord`, `lata`, `npa-nxx`) are not extracted.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Location {
10    pub building: Option<String>,
11    pub floor: Option<String>,
12    pub rack: Option<String>,
13    pub country_code: Option<String>,
14    pub postal_code: Option<String>,
15    pub latitude: Option<String>,
16    pub longitude: Option<String>,
17    /// Feet above sea level.
18    pub altitude: Option<String>,
19}
20
21pub(super) fn parse_location(location: &ConfigLine) -> Location {
22    let field = |keyword: &str| arg_of(location, keyword).map(str::to_string);
23    Location {
24        building: field("building"),
25        floor: field("floor"),
26        rack: field("rack"),
27        country_code: field("country-code"),
28        postal_code: field("postal-code"),
29        latitude: field("latitude"),
30        longitude: field("longitude"),
31        altitude: field("altitude"),
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use crate::system::system;
38    use asterust::config::juniper;
39
40    #[test]
41    fn extracts_location() {
42        let system = system(&juniper::parse_braces(
43            r#"
44system {
45    location {
46        building "DC East";
47        floor 2;
48        rack 14;
49        country-code US;
50        postal-code 10001;
51        latitude 40.7128;
52        longitude -74.006;
53        altitude 33;
54    }
55}
56"#,
57        ))
58        .unwrap();
59        let location = system.location.unwrap();
60        assert_eq!(location.building.as_deref(), Some("DC East"));
61        assert_eq!(location.floor.as_deref(), Some("2"));
62        assert_eq!(location.rack.as_deref(), Some("14"));
63        assert_eq!(location.country_code.as_deref(), Some("US"));
64        assert_eq!(location.postal_code.as_deref(), Some("10001"));
65        assert_eq!(location.latitude.as_deref(), Some("40.7128"));
66        assert_eq!(location.longitude.as_deref(), Some("-74.006"));
67        assert_eq!(location.altitude.as_deref(), Some("33"));
68    }
69
70    #[test]
71    fn no_location_is_none() {
72        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
73        assert!(system.location.is_none());
74    }
75}