use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Location {
pub latitude: f64,
pub longitude: f64,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub altitude: Option<f64>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub relevant_text: Option<String>,
}
impl Default for Location {
fn default() -> Self {
Self {
latitude: 0.0,
longitude: 0.0,
altitude: None,
relevant_text: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn make_location() {
let location = Location {
latitude: 37.334_606,
longitude: -122.009_102,
relevant_text: Some(String::from("Apple Park, Cupertino, CA, USA")),
..Default::default()
};
let json = serde_json::to_string_pretty(&location).unwrap();
println!("{json}");
let json_expected = r#"{
"latitude": 37.334606,
"longitude": -122.009102,
"relevantText": "Apple Park, Cupertino, CA, USA"
}"#;
assert_eq!(json_expected, json);
let location: Location = serde_json::from_str(json_expected).unwrap();
let json = serde_json::to_string_pretty(&location).unwrap();
assert_eq!(json_expected, json);
}
}