gmaps_static/
location.rs

1use std::fmt;
2
3#[derive(Clone)]
4pub enum Location {
5    Address(String),
6    LatLng(f64, f64),
7}
8
9impl Location {
10    pub fn from_address(address: String) -> Self {
11        Location::Address(address)
12    }
13
14    pub fn from_lat_lng(lat: f64, lng: f64) -> Self {
15        let clamp_lat = if lat < -90.0 {
16            -90.0
17        } else if lat > 90.0 {
18            90.0
19        } else {
20            lat
21        };
22
23        let clamp_lng = if lng < -180.0 {
24            -180.0
25        } else if lng > 180.0 {
26            180.0
27        } else {
28            lng
29        };
30
31        Location::LatLng(clamp_lat, clamp_lng)
32    }
33}
34
35impl From<(f32, f32)> for Location {
36    fn from(lat_lng: (f32, f32)) -> Self {
37        let (lat, lng) = lat_lng;
38        Location::from_lat_lng(lat as f64, lng as f64)
39    }
40}
41
42impl From<(f64, f64)> for Location {
43    fn from(lat_lng: (f64, f64)) -> Self {
44        let (lat, lng) = lat_lng;
45        Location::from_lat_lng(lat, lng)
46    }
47}
48
49impl From<String> for Location {
50    fn from(address: String) -> Self {
51        Location::from_address(address)
52    }
53}
54
55impl From<&'static str> for Location {
56    fn from(address: &'static str) -> Self {
57        Location::from_address(String::from(address))
58    }
59}
60
61impl fmt::Display for Location {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        use Location::*;
64        write!(
65            f,
66            "{}",
67            match self {
68                Address(s) => s.clone(),
69                LatLng(lat, lng) => format!("{:.6},{:.6}", lat, lng),
70            }
71        )
72    }
73}