use rust_decimal::Decimal;
use serde::Deserialize;
#[cfg(doc)]
use crate::SearchQueryBuilder;
pub type PlaceId = u64;
pub type OsmId = u64;
macro_rules! strenum {
(
$(#[ $meta:meta ])*
$typname:ident {
$(
$( #[$fieldmeta:meta] )*
$code:ident
),* $(,)?
}
) => {
$(#[ $meta ])*
pub enum $typname {
$(
$(#[$fieldmeta])*
$code
),*
}
impl std::str::FromStr for $typname {
type Err = $crate::errors::Error;
fn from_str(s: &str) -> $crate::errors::Result<Self> {
let uppers = s.to_lowercase();
$( if uppers == stringify!($code).to_lowercase() {
Ok(Self::$code)
} else )* {
Err($crate::errors::Error::Iso3166_1Alpha2Parse(s.to_string()))
}
}
}
impl std::fmt::Display for $typname {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", match self {
$(
Self::$code => stringify!($code),
)*
}.to_lowercase())
}
}
};
}
strenum!(
#[derive(Debug, Clone, Copy, serde::Serialize)]
#[serde(rename_all = "lowercase")]
CountryCodeIso3166_1Alpha2 {
AD,
AE,
AF,
AG,
AI,
AL,
AM,
AO,
AQ,
AR,
AS,
AT,
AU,
AW,
AX,
AZ,
BA,
BB,
BD,
BE,
BF,
BG,
BH,
BI,
BJ,
BL,
BM,
BN,
BO,
BQ,
BR,
BS,
BT,
BV,
BW,
BY,
BZ,
CA,
CC,
CD,
CF,
CG,
CH,
CI,
CK,
CL,
CM,
CN,
CO,
CR,
CU,
CV,
CW,
CX,
CY,
CZ,
DE,
DJ,
DK,
DM,
DO,
DZ,
EC,
EE,
EG,
EH,
ER,
ES,
ET,
FI,
FJ,
FK,
FM,
FO,
FR,
GA,
GB,
GD,
GE,
GF,
GG,
GH,
GI,
GL,
GM,
GN,
GP,
GQ,
GR,
GS,
GT,
GU,
GW,
GY,
HK,
HM,
HN,
HR,
HT,
HU,
ID,
IE,
IL,
IM,
IN,
IO,
IQ,
IR,
IS,
IT,
JE,
JM,
JO,
JP,
KE,
KG,
KH,
KI,
KM,
KN,
KP,
KR,
KW,
KY,
KZ,
LA,
LB,
LC,
LI,
LK,
LR,
LS,
LT,
LU,
LV,
LY,
MA,
MC,
MD,
ME,
MF,
MG,
MH,
MK,
ML,
MM,
MN,
MO,
MP,
MQ,
MR,
MS,
MT,
MU,
MV,
MW,
MX,
MY,
MZ,
NA,
NC,
NE,
NF,
NG,
NI,
NL,
NO,
NP,
NR,
NU,
NZ,
OM,
PA,
PE,
PF,
PG,
PH,
PK,
PL,
PM,
PN,
PR,
PS,
PT,
PW,
PY,
QA,
RE,
RO,
RS,
RU,
RW,
SA,
SB,
SC,
SD,
SE,
SG,
SH,
SI,
SJ,
SK,
SL,
SM,
SN,
SO,
SR,
SS,
ST,
SV,
SX,
SY,
SZ,
TC,
TD,
TF,
TG,
TH,
TJ,
TK,
TL,
TM,
TN,
TO,
TR,
TT,
TV,
TW,
TZ,
UA,
UG,
UM,
US,
UY,
UZ,
VA,
VC,
VE,
VG,
VI,
VN,
VU,
WF,
WS,
YE,
YT,
ZA,
ZM,
ZW,
}
);
pub type CountryCode = CountryCodeIso3166_1Alpha2;
strenum!(
#[derive(Debug, Clone, Copy, serde::Serialize)]
#[serde(rename_all = "lowercase")]
#[allow(missing_docs, reason="clear naming")]
Layer {
Address,
Poi,
Railway,
Natural,
Manmade,
}
);
strenum!(
#[derive(Debug, Clone, Copy, serde::Serialize)]
#[serde(rename_all = "lowercase")]
#[allow(missing_docs, reason="clear naming")]
FeatureType {
Country,
State,
City,
Settlement
}
);
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(from = "[Decimal; 4]")]
pub struct BoundingBox {
min: Point,
max: Point,
}
impl BoundingBox {
pub fn new(min: impl Into<Point>, max: impl Into<Point>) -> Self {
Self {
min: min.into(),
max: max.into(),
}
}
pub fn min(&self) -> Point {
self.min
}
pub fn max(&self) -> Point {
self.max
}
}
impl From<[Decimal; 4]> for BoundingBox {
fn from(value: [Decimal; 4]) -> Self {
BoundingBox {
min: Point {
latitude: value[0],
longitude: value[2],
},
max: Point {
latitude: value[1],
longitude: value[3],
},
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Point {
latitude: Decimal,
longitude: Decimal,
}
impl Point {
pub fn new(latitude: impl Into<Decimal>, longitude: impl Into<Decimal>) -> Self {
Self {
latitude: latitude.into(),
longitude: longitude.into(),
}
}
pub fn latitude(&self) -> Decimal {
self.latitude
}
pub fn longitude(&self) -> Decimal {
self.longitude
}
}
#[derive(Debug, Clone, Copy)]
pub struct ViewBox(Point, Point);
impl ViewBox {
pub fn new(a: impl Into<Point>, b: impl Into<Point>) -> Self {
Self(a.into(), b.into())
}
}
impl From<ViewBox> for [Decimal; 4] {
fn from(value: ViewBox) -> Self {
[
value.0.longitude(),
value.0.latitude(),
value.1.longitude(),
value.1.latitude(),
]
}
}
impl<A: Into<Point>, B: Into<Point>> From<(A, B)> for ViewBox {
fn from(value: (A, B)) -> Self {
Self(value.0.into(), value.1.into())
}
}
impl From<ViewBox> for (Point, Point) {
fn from(value: ViewBox) -> Self {
(value.0, value.1)
}
}