#[cfg(feature = "geo")]
mod geo;
use crate::types::LatLng;
use crate::GoogleMapsError;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use rust_decimal::Decimal;
#[cfg(not(feature = "geo"))]
#[derive(
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub enum Location {
Address(String),
LatLng(LatLng),
PlaceId(String),
}
impl std::convert::From<&Self> for Location {
fn from(location: &Self) -> Self {
location.clone()
} }
#[cfg(not(feature = "geo"))]
impl std::convert::From<&Location> for String {
fn from(location: &Location) -> Self {
match location {
Location::Address(address) => {
utf8_percent_encode(address, NON_ALPHANUMERIC).to_string()
}
Location::LatLng(latlng) => {
utf8_percent_encode(&Self::from(latlng), NON_ALPHANUMERIC).to_string()
}
Location::PlaceId(place_id) => {
utf8_percent_encode(&format!("place_id:{place_id}"), NON_ALPHANUMERIC).to_string()
}
} } }
#[cfg(feature = "geo")]
#[derive(Clone, Debug, PartialEq)]
pub enum Location {
Address(String),
LatLng(LatLng),
PlaceId(String),
Coord(geo_types::geometry::Coord),
Point(geo_types::geometry::Point),
}
#[cfg(feature = "geo")]
impl std::convert::From<&Location> for String {
fn from(location: &Location) -> Self {
match location {
Location::Address(address) => {
utf8_percent_encode(address, NON_ALPHANUMERIC).to_string()
}
Location::LatLng(latlng) => {
utf8_percent_encode(&Self::from(latlng), NON_ALPHANUMERIC).to_string()
}
Location::PlaceId(place_id) => {
utf8_percent_encode(&format!("place_id:{place_id}"), NON_ALPHANUMERIC).to_string()
}
Location::Coord(coordinate) => utf8_percent_encode(
&format!(
"{latitude},{longitude}",
latitude = coordinate.y,
longitude = coordinate.x,
),
NON_ALPHANUMERIC,
)
.to_string(),
Location::Point(point) => utf8_percent_encode(
&format!(
"{latitude},{longitude}",
latitude = point.y(),
longitude = point.x()
),
NON_ALPHANUMERIC,
)
.to_string(),
} } }
impl Location {
pub fn from_address(address: impl Into<String>) -> Self {
Self::Address(address.into())
} }
impl Location {
pub fn from_place_id(place_id: impl Into<String>) -> Self {
Self::PlaceId(place_id.into())
} }
impl Location {
pub fn try_from_dec(latitude: Decimal, longitude: Decimal) -> Result<Self, GoogleMapsError> {
let latlng = LatLng::try_from_dec(latitude, longitude)?;
Ok(Self::LatLng(latlng))
} }
impl Location {
pub fn try_from_f32(latitude: f32, longitude: f32) -> Result<Self, GoogleMapsError> {
let latlng = LatLng::try_from_f32(latitude, longitude)?;
Ok(Self::LatLng(latlng))
} }
impl Location {
pub fn try_from_f64(latitude: f64, longitude: f64) -> Result<Self, GoogleMapsError> {
let latlng = LatLng::try_from_f64(latitude, longitude)?;
Ok(Self::LatLng(latlng))
} }
impl From<LatLng> for Location {
fn from(latlng: LatLng) -> Self {
Self::LatLng(latlng)
} }
impl From<&LatLng> for Location {
fn from(latlng: &LatLng) -> Self {
Self::LatLng(*latlng)
} }