photon_geocoding/data/
filter.rs

1use std::fmt;
2
3use crate::{BoundingBox, LatLon};
4
5pub enum PhotonLayer {
6    House,
7    Street,
8    Locality,
9    District,
10    City,
11    County,
12    State,
13    Country,
14}
15
16impl fmt::Display for PhotonLayer {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::House => write!(f, "house"),
20            Self::Street => write!(f, "street"),
21            Self::Locality => write!(f, "locality"),
22            Self::District => write!(f, "district"),
23            Self::City => write!(f, "city"),
24            Self::County => write!(f, "county"),
25            Self::State => write!(f, "state"),
26            Self::Country => write!(f, "country"),
27        }
28    }
29}
30
31/// Filtering options for forward searches. This struct implements a builder pattern, so filters
32/// can be easily constructed.
33pub struct ForwardFilter {
34    pub location_bias: Option<LatLon>,
35    pub location_bias_zoom: Option<u64>,
36    pub location_bias_scale: Option<f64>,
37    pub bounding_box: Option<BoundingBox>,
38    pub limit: Option<u64>,
39    pub lang: Option<String>,
40    pub layer: Option<Vec<PhotonLayer>>,
41    pub additional_query: Option<Vec<(String, String)>>,
42}
43
44impl Default for ForwardFilter {
45    fn default() -> Self {
46        ForwardFilter {
47            location_bias: None,
48            location_bias_zoom: None,
49            location_bias_scale: None,
50            bounding_box: None,
51            limit: None,
52            lang: None,
53            layer: None,
54            additional_query: None,
55        }
56    }
57}
58
59impl ForwardFilter {
60    /// Construct a new `ForwardFilter`. All fields are set to `None` in the beginning.
61    pub fn new() -> Self {
62        Self::default()
63    }
64
65    /// Concentrate the search around a specific coordinate.
66    ///
67    /// `zoom` describes the radius around the coordinate to focus on.
68    /// `scale` describes how much the prominence of a result should still be taken into account.
69    /// See [Photon documentation](https://github.com/komoot/photon#search-with-location-bias) for details
70    pub fn location_bias(mut self, coords: LatLon, zoom: Option<u64>, scale: Option<f64>) -> Self {
71        self.location_bias = Some(coords);
72        self.location_bias_zoom = zoom;
73        self.location_bias_scale = scale;
74        self
75    }
76
77    /// Concentrate the search in a specific rectangular area.
78    pub fn bounding_box(mut self, bbox: BoundingBox) -> Self {
79        self.bounding_box = Some(bbox);
80        self
81    }
82
83    /// Limit the number of search results.
84    pub fn limit(mut self, limit: u64) -> Self {
85        self.limit = Some(limit);
86        self
87    }
88
89    /// Return results in a specific language. Photon currently supports `DE`, `EN` and `FR`.
90    /// Defaults to the local language of a search result.
91    pub fn language(mut self, lang: &str) -> Self {
92        self.lang = Some(String::from(lang.to_lowercase()));
93        self
94    }
95
96    /// Filter results by layer. See [Photon documentation](https://github.com/komoot/photon#filter-results-by-layer)
97    pub fn layer(mut self, layer: Vec<PhotonLayer>) -> Self {
98        self.layer = Some(layer);
99        self
100    }
101
102    /// Add additional query strings to the request. Example: [Filtering by tags and values](https://github.com/komoot/photon#filter-results-by-tags-and-values)
103    pub fn additional_query(mut self, query: Vec<(&str, &str)>) -> Self {
104        self.additional_query = Some(
105            query
106                .iter()
107                .map(|(s, t)| (s.to_string(), t.to_string()))
108                .collect(),
109        );
110        self
111    }
112}
113
114/// Filtering options for reverse searches. This struct implements a builder pattern, so filters
115/// can be easily constructed.
116pub struct ReverseFilter {
117    pub radius: Option<u64>,
118    pub limit: Option<u64>,
119    pub lang: Option<String>,
120    pub layer: Option<Vec<PhotonLayer>>,
121    pub additional_query: Option<Vec<(String, String)>>,
122}
123
124impl Default for ReverseFilter {
125    fn default() -> Self {
126        ReverseFilter {
127            radius: None,
128            limit: None,
129            lang: None,
130            layer: None,
131            additional_query: None,
132        }
133    }
134}
135
136impl ReverseFilter {
137    /// Construct a new `ReverseFilter`. All fields are set to `None` in the beginning.
138    pub fn new() -> Self {
139        Self::default()
140    }
141
142    pub fn radius(mut self, radius: u64) -> Self {
143        self.radius = Some(radius);
144        self
145    }
146
147    /// Limit the number of search results.
148    pub fn limit(mut self, limit: u64) -> Self {
149        self.limit = Some(limit);
150        self
151    }
152
153    /// Return results in a specific language. Photon currently supports `DE`, `EN` and `FR`.
154    /// Defaults to the local language of a search result.
155    pub fn language(mut self, lang: &str) -> Self {
156        self.lang = Some(String::from(lang.to_lowercase()));
157        self
158    }
159
160    /// Filter results by layer. See [Photon documentation](https://github.com/komoot/photon#filter-results-by-layer)
161    pub fn layer(mut self, layer: Vec<PhotonLayer>) -> Self {
162        self.layer = Some(layer);
163        self
164    }
165
166    /// Add additional query strings to the request. Example: [Filtering by tags and values](https://github.com/komoot/photon#filter-results-by-tags-and-values)
167    pub fn additional_query(mut self, query: Vec<(&str, &str)>) -> Self {
168        self.additional_query = Some(
169            query
170                .iter()
171                .map(|(s, t)| (s.to_string(), t.to_string()))
172                .collect(),
173        );
174        self
175    }
176}