1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::geocoding::reverse::ReverseRequest;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};

impl ReverseRequest {

    /// Builds the query string for the Google Maps Geocoding API based on the
    /// input provided by the client.
    ///
    /// ## Arguments:
    ///
    /// This method accepts no arguments.

    pub fn build(&mut self) -> &mut ReverseRequest {

        // This section builds the "required parameters" portion of the query
        // string:

        let mut query = format!(
            "key={}&latlng={}",
            self.key,
            String::from(&self.latlng)
        ); // format!

        // This section builds the "optional parameters" portion of the query
        // string:

        // Language key/value pair:
        if let Some(language) = &self.language {
            query.push_str("&language=");
            query.push_str(&String::from(language))
        } // if

        // Location type(s) key/value pair:
        if let Some(location_type) = &self.location_type {
            query.push_str("&location_type=");
            query.push_str(&*utf8_percent_encode(
                &String::from(location_type.iter().map(|location_type| String::from(location_type) + "|").collect::<String>().trim_end_matches('|')),
                NON_ALPHANUMERIC
            ).to_string())
        } // if

        // Result type(s) key/value pair:
        if let Some(result_type) = &self.result_type {
            query.push_str("&result_type=");
            query.push_str(&*utf8_percent_encode(
                &String::from(result_type.iter().map(|result_type| String::from(result_type) + "|").collect::<String>().trim_end_matches('|')),
                NON_ALPHANUMERIC
            ).to_string())
        } // if

        // Set query string in ReverseRequest struct.
        self.query = Some(query);

        // Return modified ReverseRequest struct to caller.
        self

    } // fn

} // impl