1mod fetch;
2mod models;
3
4pub mod nearby;
5pub mod place;
6pub use fetch::fetch;
7
8use async_trait::async_trait;
9use models::LatLng;
10
11pub trait SearchParams {
12 fn get_params(&self) -> Vec<(String, String)>;
13}
14
15#[async_trait]
16pub trait Send<Response, Error> {
17 async fn send(&self) -> Result<Response, Error>;
18}
19
20pub struct Client {
21 token: String,
22}
23
24impl Client {
25 pub fn new(token: impl Into<String>) -> Client {
26 Client {
27 token: token.into(),
28 }
29 }
30
31 pub fn find(&self, input: impl Into<String>, input_type: impl Into<String>) -> place::Request {
32 place::Request {
33 token: self.token.clone(),
34 input: input.into(),
35 input_type: input_type.into(),
36 ..Default::default()
37 }
38 }
39
40 pub fn nearby(&self, latitude: f64, longitude: f64) -> nearby::Request {
41 nearby::Request {
42 token: self.token.clone(),
43 location: LatLng {
44 lat: latitude,
45 lng: longitude,
46 },
47 ..Default::default()
48 }
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use std::ops::Deref;
55
56 use super::{Client, SearchParams};
57 use crate::models::LatLng;
58
59 #[test]
60 fn test_nearby_request() {
61 let request = Client::new("hello kirby").nearby(0.0, 0.0);
62
63 assert_eq!(request.token, "hello kirby");
64 assert_eq!(request.location, LatLng { lat: 0.0, lng: 0.0 });
65 assert_eq!(request.keyword, None);
66 assert_eq!(request.language, None);
67 assert_eq!(request.maxprice, None);
68 assert_eq!(request.minprice, None);
69 assert_eq!(request.opennow, None);
70 assert_eq!(request.pagetoken, None);
71 assert_eq!(request.request_type, None);
72 }
73
74 fn format_search_params<T>(params: &[(T, T)]) -> String
75 where
76 T: Deref,
77 T: ToString,
78 {
79 params
80 .into_iter()
81 .map(|(a, b)| format!("({},{})", a.to_string(), b.to_string()))
82 .collect::<Vec<_>>()
83 .join(",")
84 }
85
86 #[test]
87 fn test_nearby_prominence() {
88 let request = Client::new("hello kirby").nearby(0.0, 0.0).prominence(1000);
89
90 assert_eq!(request.radius, 1000);
91
92 let left = vec![
93 ("key", "hello kirby"),
94 ("location", "0,0"),
95 ("rankby", "prominence"),
96 ("radius", "1000"),
97 ];
98
99 let right = request.get_params();
100
101 assert_eq!(
102 format_search_params(&left),
104 format_search_params(&right)
105 )
106 }
107
108 #[test]
109 fn test_nearby_distance() {
110 let request = Client::new("hello kirby")
111 .nearby(0.0, 0.0)
112 .distance()
113 .set_type("restaurant")
114 .set_keyword("food");
115
116 let left = vec![
117 ("key", "hello kirby"),
118 ("location", "0,0"),
119 ("keyword", "food"),
120 ("type", "restaurant"),
121 ("rankby", "distance"),
122 ];
123 let right = request.get_params();
124
125 assert_eq!(
126 format_search_params(&left),
128 format_search_params(&right)
129 )
130 }
131}