Skip to main content

google_maps/geocoding/response/
mod.rs

1//! Resources (enums, structs) for processing the _Geocoding API_ response
2//! from the Google Maps Platform. Look in here for more information about the
3//! data returned from Google's server and how to parse it with your program.
4
5pub mod geocoding;
6pub mod plus_code;
7pub mod status;
8
9// -----------------------------------------------------------------------------
10
11use crate::geocoding::{Error, response::{geocoding::Geocoding, status::Status}};
12use serde::{Deserialize, Serialize};
13
14// -----------------------------------------------------------------------------
15//
16/// The response from the Google Maps Geolocation API will be stored in this
17/// structure.
18#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
19pub struct Response {
20    /// When the geocoder returns a status code other than `OK`, there may be an
21    /// additional `error_message` field within the Geocoding response object.
22    /// This field contains more detailed information about the reasons behind
23    /// the given status code.
24    ///
25    /// *Note: This field is not guaranteed to be always present, and its
26    /// content is subject to change.*
27    #[serde(default)]
28    pub error_message: Option<String>,
29
30    /// When the geocoder returns results, it places them within a results
31    /// array. Even if the geocoder returns no results (such as if the address
32    /// doesn't exist) it still returns an empty results array.
33    #[serde(default)]
34    pub results: Vec<Geocoding>,
35
36    /// The `status` field within the Geocoding response object contains the
37    /// status of the request, and may contain debugging information to help you
38    /// track down why geocoding is not working.
39    pub status: Status,
40} // struct
41
42// -----------------------------------------------------------------------------
43
44impl std::convert::TryFrom<String> for Response {
45    type Error = serde_json::Error;
46    /// Convert a Google Maps API [JSON](https://en.wikipedia.org/wiki/JSON)
47    /// `String` response into a `Response` struct.
48    fn try_from(s: String) -> Result<Self, Self::Error> {
49        serde_json::from_slice(&s.into_bytes())
50    } // fn
51} // impl
52
53// -----------------------------------------------------------------------------
54
55impl std::str::FromStr for Response {
56    type Err = serde_json::Error;
57    /// Converts a Google Maps API [JSON](https://en.wikipedia.org/wiki/JSON)
58    /// `&str` response into a `Response` struct.
59    ///
60    /// # Notes
61    ///
62    /// * It's recommended to use the implemented `TryFrom` trait instead.
63    ///
64    /// * The [serde_json](https://crates.io/crates/simd-json)'s `from_str`
65    ///   function implementation is unsafe and it's `from_slice` function
66    ///   requires a mutable reference. Therefore this trait clones the `&str`
67    ///   into a `String` to give `from_slice` mutable access to the string.
68    fn from_str(s: &str) -> Result<Self, Self::Err> {
69        let bytes = s.to_string().into_bytes();
70        serde_json::from_slice(&bytes)
71    } // fn
72} // impl
73
74// -----------------------------------------------------------------------------
75
76impl std::convert::From<Response> for Result<Response, crate::geocoding::Error> {
77    /// Converts a Google Maps API `Response` into a `Result<Response, Error>`
78    /// by examining the `status` field inside of the response.
79    ///
80    /// If the status indicates a success, then an `Ok(response)` will be
81    /// returned. If the status indicates an error, then an `Err(error)` will be
82    /// returned.
83    fn from(response: Response) -> Self {
84        match response.status {
85            Status::Ok => Ok(response),
86            Status::InvalidRequest => Err(Error::InvalidRequest),
87            Status::OverDailyLimit => Err(Error::OverDailyLimit),
88            Status::OverQueryLimit => Err(Error::OverQueryLimit),
89            Status::RequestDenied => Err(Error::RequestDenied),
90            Status::UnknownError => Err(Error::UnknownError),
91            Status::ZeroResults => Err(Error::ZeroResults),
92        } // match
93    } // fn
94} // impl