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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use serde::{Deserialize, Serialize};
use crate::models::constants::Place;

#[derive(Debug, Serialize, Deserialize)]
pub struct NearbySearchResult {
    pub html_attributions: Vec<String>,
    #[serde(rename = "results")]
    pub places: Vec<Place>,
    pub status: PlaceSearchStatus,
    pub error_message: Option<String>,
    pub info_messages: Option<Vec<String>>,
    pub next_page_token: Option<String>,
    #[serde(skip)]
    pub total_results: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FindPlaceSearchResult {
    #[serde(rename = "candidates")]
    pub results: Vec<Place>,
    pub status: PlaceSearchStatus,
    pub error_message: Option<String>,
    pub info_messages: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TextSearchResult {
    pub html_attributions: Vec<String>,
    #[serde(rename = "results")]
    pub places: Vec<Place>,
    pub status: PlaceSearchStatus,
    pub error_message: Option<String>,
    pub info_messages: Option<Vec<String>>,
    pub next_page_token: Option<String>,
    #[serde(skip)]
    pub total_results: u32,
}

impl NearbySearchResult {
    pub fn calculate_total_results(&mut self) {
        self.total_results = self.places.len() as u32;
    }
    pub fn display(&self) -> String {
        let places = self.places.iter().map(|p| p.display()).collect::<Vec<String>>().join(", ");
        let html_attributions = self.html_attributions.join(", ");
        let info_messages = self.info_messages.as_ref().map(|v| v.join(", ")).unwrap_or_default();
        format!("NearbySearchResult {{ html_attributions: [{}], places: [{}], status: {}, error_message: {}, info_messages: [{}], next_page_token: {}, total_results: {} }}", 
            html_attributions, places, self.status.as_str(), 
            self.error_message.as_ref().unwrap_or(&"".to_string()), 
            info_messages, self.next_page_token.as_ref().unwrap_or(&"".to_string()), self.total_results)
    }
}

impl FindPlaceSearchResult {
    pub fn display(&self) -> String {
        let results = self.results.iter().map(|p| p.display()).collect::<Vec<String>>().join(", ");
        let info_messages = self.info_messages.as_ref().map(|v| v.join(", ")).unwrap_or_default();
        format!("FindPlaceSearchResult {{ results: [{}], status: {}, error_message: {}, info_messages: [{}] }}", 
            results, self.status.as_str(), self.error_message.as_ref().unwrap_or(&"".to_string()), info_messages)
    }
}

impl TextSearchResult {
    pub fn calculate_total_results(&mut self) {
        self.total_results = self.places.len() as u32;
    }
    pub fn display(&self) -> String {
        let places = self.places.iter().map(|p| p.display()).collect::<Vec<String>>().join(", ");
        let html_attributions = self.html_attributions.join(", ");
        let info_messages = self.info_messages.as_ref().map(|v| v.join(", ")).unwrap_or_default();
        format!("TextSearchResult {{ html_attributions: [{}], places: [{}], status: {}, error_message: {}, info_messages: [{}], next_page_token: {} }}, total_results: {}", 
            html_attributions, places, self.status.as_str(),
            self.error_message.as_ref().unwrap_or(&"".to_string()),
            info_messages, self.next_page_token.as_ref().unwrap_or(&"".to_string()), self.total_results)
    }
}


#[derive(Debug, Serialize, Deserialize)]
pub enum PlaceSearchStatus {
    #[serde(rename = "OK")]
    Ok,
    #[serde(rename = "ZERO_RESULTS")]
    ZeroResults,
    #[serde(rename = "INVALID_REQUEST")]
    InvalidRequest,
    #[serde(rename = "OVER_QUERY_LIMIT")]
    OverQueryLimit,
    #[serde(rename = "REQUEST_DENIED")]
    RequestDenied,
    #[serde(rename = "UNKNOWN_ERROR")]
    UnknownError,
}

impl PlaceSearchStatus {
    pub fn as_str(&self) -> &str {
        match self {
            PlaceSearchStatus::Ok => "OK",
            PlaceSearchStatus::ZeroResults => "ZERO_RESULTS",
            PlaceSearchStatus::OverQueryLimit => "OVER_QUERY_LIMIT",
            PlaceSearchStatus::RequestDenied => "REQUEST_DENIED",
            PlaceSearchStatus::InvalidRequest => "INVALID_REQUEST",
            PlaceSearchStatus::UnknownError => "UNKNOWN_ERROR",
        }
    }
}