use std::collections::HashMap;
use rust_decimal::Decimal;
use serde::{Deserialize, Deserializer};
use url::Url;
use crate::types::{BoundingBox, OsmId, PlaceId};
#[derive(Debug, Clone, Deserialize)]
pub struct JsonPlaceFormat {
place_id: PlaceId,
osm_type: String,
osm_id: OsmId,
boundingbox: BoundingBox,
lat: Decimal,
lon: Decimal,
display_name: String,
class: String,
r#type: String,
importance: f64,
#[serde(default)]
icon: Option<Url>,
#[serde(default)]
address: Option<AddressDetails>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
extratags: HashMap<String, String>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
namedetails: HashMap<String, String>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
entrances: Vec<Entrance>,
}
#[allow(missing_docs, reason = "self-explanatory getters")]
impl JsonPlaceFormat {
pub fn place_id(&self) -> PlaceId {
self.place_id
}
pub fn osm_type(&self) -> &str {
&self.osm_type
}
pub fn osm_id(&self) -> OsmId {
self.osm_id
}
pub fn boundingbox(&self) -> BoundingBox {
self.boundingbox
}
pub fn lat(&self) -> Decimal {
self.lat
}
pub fn lon(&self) -> Decimal {
self.lon
}
pub fn display_name(&self) -> &str {
&self.display_name
}
pub fn class(&self) -> &str {
&self.class
}
pub fn r#type(&self) -> &str {
&self.r#type
}
pub fn importance(&self) -> f64 {
self.importance
}
pub fn icon(&self) -> Option<&Url> {
self.icon.as_ref()
}
pub fn address(&self) -> Option<&AddressDetails> {
self.address.as_ref()
}
pub fn extratags(&self) -> &HashMap<String, String> {
&self.extratags
}
pub fn namedetails(&self) -> &HashMap<String, String> {
&self.namedetails
}
pub fn entrances(&self) -> &Vec<Entrance> {
&self.entrances
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct JsonV2PlaceFormat {
place_id: PlaceId,
osm_type: String,
osm_id: OsmId,
boundingbox: BoundingBox,
lat: Decimal,
lon: Decimal,
display_name: String,
category: String,
r#type: String,
importance: f64,
place_rank: u8,
#[serde(default)]
icon: Option<Url>,
#[serde(default)]
address: Option<AddressDetails>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
extratags: HashMap<String, String>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
namedetails: HashMap<String, String>,
#[serde(default, deserialize_with = "deserialize_default_if_null")]
entrances: Vec<Entrance>,
}
#[allow(missing_docs, reason = "self-explanatory getters")]
impl JsonV2PlaceFormat {
pub fn place_id(&self) -> PlaceId {
self.place_id
}
pub fn osm_type(&self) -> &str {
&self.osm_type
}
pub fn osm_id(&self) -> OsmId {
self.osm_id
}
pub fn boundingbox(&self) -> BoundingBox {
self.boundingbox
}
pub fn lat(&self) -> Decimal {
self.lat
}
pub fn lon(&self) -> Decimal {
self.lon
}
pub fn display_name(&self) -> &str {
&self.display_name
}
pub fn category(&self) -> &str {
&self.category
}
pub fn r#type(&self) -> &str {
&self.r#type
}
pub fn importance(&self) -> f64 {
self.importance
}
pub fn place_rank(&self) -> u8 {
self.place_rank
}
pub fn icon(&self) -> Option<&Url> {
self.icon.as_ref()
}
pub fn address(&self) -> Option<&AddressDetails> {
self.address.as_ref()
}
pub fn extratags(&self) -> &HashMap<String, String> {
&self.extratags
}
pub fn namedetails(&self) -> &HashMap<String, String> {
&self.namedetails
}
pub fn entrances(&self) -> &Vec<Entrance> {
&self.entrances
}
}
fn deserialize_default_if_null<'de, D, T>(deserializer: D) -> std::result::Result<T, D::Error>
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
Ok(Option::<T>::deserialize(deserializer)?.unwrap_or_default())
}
macro_rules! stringmapstruct {
(
$(#[$meta:meta])*
$name:ident {
$( $(#[serde($($serde:tt)+)])? $field:ident),* $(,)?
}
) => {
$( #[$meta] )*
pub struct $name {
$(
#[serde(default $(, $($serde)+)?)]
$field: Option<String>
),*
}
#[allow(missing_docs, reason = "self-explanatory getters")]
impl $name {
$(
pub fn $field(&self) -> Option<&str> {
self.$field.as_deref()
}
)*
}
};
}
stringmapstruct!(
#[derive(Debug, Clone, Deserialize)]
AddressDetails {
continent,
country,
country_code,
region,
state,
state_district,
county,
#[serde(alias = "ISO3166-2-lvl")]
iso3166_2_lvl,
municipality,
city,
town,
village,
city_district,
district,
borough,
suburb,
subdivision,
hamlet,
croft,
isolated_dwelling,
neighbourhood,
allotments,
quarter,
city_block,
residential,
farm,
farmyard,
industrial,
commercial,
retail,
road,
house_number,
house_name,
emergency,
historic,
military,
natural,
landuse,
place,
railway,
man_made,
aerialway,
boundary,
amenity,
aeroway,
club,
craft,
leisure,
office,
mountain_pass,
shop,
tourism,
bridge,
tunnel,
waterway,
postcode,
}
);
#[derive(Debug, Clone, Deserialize)]
pub struct Entrance {
osm_id: OsmId,
r#type: String,
lat: Decimal,
lon: Decimal,
extratags: HashMap<String, String>,
}
#[allow(missing_docs, reason = "self-explanatory getters")]
impl Entrance {
pub fn osm_id(&self) -> OsmId {
self.osm_id
}
pub fn r#type(&self) -> &str {
&self.r#type
}
pub fn lat(&self) -> Decimal {
self.lat
}
pub fn lon(&self) -> Decimal {
self.lon
}
pub fn extratags(&self) -> &HashMap<String, String> {
&self.extratags
}
}