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