use serde::{Deserialize, Serialize};
use super::MOD_PATH;
use crate::{HasId, HasName, Uri};
use tmflib_derive::{HasId, HasName};
const CLASS_PATH: &str = "geographicAddress";
#[derive(Clone, Debug, Default, Deserialize, HasId, HasName, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GeographicSubAddress {
#[serde(skip_serializing_if = "Option::is_none")]
pub building_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<Uri>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub level_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub level_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub private_street_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub private_street_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sub_address_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sub_unit_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sub_unit: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct GeographicLocationRefOrValue {
pub bbox: Vec<f64>,
href: Option<String>,
id: Option<String>,
name: Option<String>,
}
impl From<(f64, f64)> for GeographicLocationRefOrValue {
fn from(value: (f64, f64)) -> Self {
GeographicLocationRefOrValue {
bbox: vec![value.0, value.1],
..Default::default()
}
}
}
#[derive(Clone, Debug, Default, Deserialize, HasId, HasName, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GeographicAddress {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
locality: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
street_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
street_nr: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
state_or_province: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
street_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
geographic_sub_address: Option<Vec<GeographicSubAddress>>,
#[serde(skip_serializing_if = "Option::is_none")]
geographic_location: Option<GeographicLocationRefOrValue>,
}
impl GeographicAddress {
pub fn new(name: impl Into<String>) -> GeographicAddress {
let mut address = GeographicAddress::create();
address.name = Some(name.into());
address
}
pub fn street(mut self, street: &str) -> GeographicAddress {
if street.split(' ').count() > 1 {
}
self.street_name = Some(street.to_string());
self
}
pub fn street_type(mut self, street_type: &str) -> GeographicAddress {
self.street_type = Some(street_type.to_string());
self
}
pub fn number(mut self, number: &str) -> GeographicAddress {
self.street_nr = Some(number.to_string());
self
}
pub fn suburb(mut self, suburb: &str) -> GeographicAddress {
self.locality = Some(suburb.to_string());
self
}
pub fn state(mut self, state: &str) -> GeographicAddress {
self.state_or_province = Some(state.to_string());
self
}
}
#[cfg(test)]
mod test {
use super::*;
const NUMBER: &str = "14";
const STREET: &str = "Mayfair";
const STREET_TYPE: &str = "Parade";
const SUBURB: &str = "Bayview";
const STATE: &str = "Victoria";
#[test]
fn test_address_new_name() {
let address = GeographicAddress::new("AnAddress");
assert_eq!(address.name, Some("AnAddress".into()));
}
#[test]
fn test_address_new_number() {
let address = GeographicAddress::new("AnAddress").number(NUMBER);
assert_eq!(address.street_nr, Some(NUMBER.into()));
}
#[test]
fn test_address_new_street() {
let address = GeographicAddress::new("AnAddress").street(STREET);
assert_eq!(address.street_name, Some(STREET.into()));
}
#[test]
fn test_address_new_streettype() {
let address = GeographicAddress::new("AnAddress").street_type(STREET_TYPE);
assert_eq!(address.street_type, Some(STREET_TYPE.into()));
}
#[test]
fn test_address_new_suburb() {
let address = GeographicAddress::new("AnAddress").suburb(SUBURB);
assert_eq!(address.locality, Some(SUBURB.into()));
}
#[test]
fn test_address_new_state() {
let address = GeographicAddress::new("AnAddress").state(STATE);
assert_eq!(address.state_or_province, Some(STATE.into()));
}
}