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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::fmt;

use crate::{BoundingBox, LatLon};

pub enum PhotonLayer {
    House,
    Street,
    Locality,
    District,
    City,
    County,
    State,
    Country,
}

impl fmt::Display for PhotonLayer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::House => write!(f, "house"),
            Self::Street => write!(f, "street"),
            Self::Locality => write!(f, "locality"),
            Self::District => write!(f, "district"),
            Self::City => write!(f, "city"),
            Self::County => write!(f, "county"),
            Self::State => write!(f, "state"),
            Self::Country => write!(f, "country"),
        }
    }
}

/// Filtering options for forward searches. This struct implements a builder pattern, so filters
/// can be easily constructed.
pub struct ForwardFilter {
    pub location_bias: Option<LatLon>,
    pub location_bias_zoom: Option<u64>,
    pub location_bias_scale: Option<f64>,
    pub bounding_box: Option<BoundingBox>,
    pub limit: Option<u64>,
    pub lang: Option<String>,
    pub layer: Option<Vec<PhotonLayer>>,
    pub additional_query: Option<Vec<(String, String)>>,
}

impl Default for ForwardFilter {
    fn default() -> Self {
        ForwardFilter {
            location_bias: None,
            location_bias_zoom: None,
            location_bias_scale: None,
            bounding_box: None,
            limit: None,
            lang: None,
            layer: None,
            additional_query: None,
        }
    }
}

impl ForwardFilter {
    /// Construct a new `ForwardFilter`. All fields are set to `None` in the beginning.
    pub fn new() -> Self {
        Self::default()
    }

    /// Concentrate the search around a specific coordinate.
    ///
    /// `zoom` describes the radius around the coordinate to focus on.
    /// `scale` describes how much the prominence of a result should still be taken into account.
    /// See [Photon documentation](https://github.com/komoot/photon#search-with-location-bias) for details
    pub fn location_bias(mut self, coords: LatLon, zoom: Option<u64>, scale: Option<f64>) -> Self {
        self.location_bias = Some(coords);
        self.location_bias_zoom = zoom;
        self.location_bias_scale = scale;
        self
    }

    /// Concentrate the search in a specific rectangular area.
    pub fn bounding_box(mut self, bbox: BoundingBox) -> Self {
        self.bounding_box = Some(bbox);
        self
    }

    /// Limit the number of search results.
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Return results in a specific language. Photon currently supports `DE`, `EN` and `FR`.
    /// Defaults to the local language of a search result.
    pub fn language(mut self, lang: &str) -> Self {
        self.lang = Some(String::from(lang.to_lowercase()));
        self
    }

    /// Filter results by layer. See [Photon documentation](https://github.com/komoot/photon#filter-results-by-layer)
    pub fn layer(mut self, layer: Vec<PhotonLayer>) -> Self {
        self.layer = Some(layer);
        self
    }

    /// Add additional query strings to the request. Example: [Filtering by tags and values](https://github.com/komoot/photon#filter-results-by-tags-and-values)
    pub fn additional_query(mut self, query: Vec<(&str, &str)>) -> Self {
        self.additional_query = Some(
            query
                .iter()
                .map(|(s, t)| (s.to_string(), t.to_string()))
                .collect(),
        );
        self
    }
}

/// Filtering options for reverse searches. This struct implements a builder pattern, so filters
/// can be easily constructed.
pub struct ReverseFilter {
    pub radius: Option<u64>,
    pub limit: Option<u64>,
    pub lang: Option<String>,
    pub layer: Option<Vec<PhotonLayer>>,
    pub additional_query: Option<Vec<(String, String)>>,
}

impl Default for ReverseFilter {
    fn default() -> Self {
        ReverseFilter {
            radius: None,
            limit: None,
            lang: None,
            layer: None,
            additional_query: None,
        }
    }
}

impl ReverseFilter {
    /// Construct a new `ReverseFilter`. All fields are set to `None` in the beginning.
    pub fn new() -> Self {
        Self::default()
    }

    pub fn radius(mut self, radius: u64) -> Self {
        self.radius = Some(radius);
        self
    }

    /// Limit the number of search results.
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Return results in a specific language. Photon currently supports `DE`, `EN` and `FR`.
    /// Defaults to the local language of a search result.
    pub fn language(mut self, lang: &str) -> Self {
        self.lang = Some(String::from(lang.to_lowercase()));
        self
    }

    /// Filter results by layer. See [Photon documentation](https://github.com/komoot/photon#filter-results-by-layer)
    pub fn layer(mut self, layer: Vec<PhotonLayer>) -> Self {
        self.layer = Some(layer);
        self
    }

    /// Add additional query strings to the request. Example: [Filtering by tags and values](https://github.com/komoot/photon#filter-results-by-tags-and-values)
    pub fn additional_query(mut self, query: Vec<(&str, &str)>) -> Self {
        self.additional_query = Some(
            query
                .iter()
                .map(|(s, t)| (s.to_string(), t.to_string()))
                .collect(),
        );
        self
    }
}