1use std::borrow::Cow;
2use crate::{Bbox, FilterSet, FilterType, RecurseFilter, SaniStr, Set, SetBuilder, TagFilter, Builder};
3#[cfg(doc)]
4use crate::{Node, Way, Relation};
5
6pub struct FilterSetBuilder<'a>(
8 pub FilterSet<'a>,
10);
11
12impl<'a> Builder<'a> for FilterSetBuilder<'a> {}
13
14impl<'a> Into<Set<'a>> for FilterSetBuilder<'a> {
15 fn into(self) -> Set<'a> {
16 self.0.into()
17 }
18}
19
20impl<'a> Into<Cow<'a, Set<'a>>> for FilterSetBuilder<'a> {
21 fn into(self) -> Cow<'a, Set<'a>> {
22 Cow::Owned(self.into())
23 }
24}
25
26impl<'a> IntoIterator for FilterSetBuilder<'a> {
27 type Item = FilterSetBuilder<'a>;
28 type IntoIter = std::array::IntoIter<Self::Item, 1>;
29 fn into_iter(self) -> Self::IntoIter {
30 [self].into_iter()
31 }
32}
33
34impl SetBuilder {
36 pub fn all_nodes<'a>() -> FilterSetBuilder<'a> {
38 FilterSetBuilder(FilterSet {
39 filter_type: FilterType::Node,
40 ..Default::default()
41 })
42 }
43
44 pub fn nodes_from<'a, T>(sets: impl IntoIterator<Item=T>)
46 -> FilterSetBuilder<'a>
47 where T: Into<Cow<'a, Set<'a>>> {
48 FilterSetBuilder(FilterSet {
49 filter_type: FilterType::Node,
50 inputs: sets.into_iter().map(|i| i.into()).collect(),
51 ..Default::default()
52 })
53 }
54
55 pub fn all_ways<'a>() -> FilterSetBuilder<'a> {
57 FilterSetBuilder(FilterSet {
58 filter_type: FilterType::Way,
59 ..Default::default()
60 })
61 }
62
63 pub fn ways_from<'a, T>(sets: impl IntoIterator<Item=T>)
65 -> FilterSetBuilder<'a>
66 where T: Into<Cow<'a, Set<'a>>> {
67 FilterSetBuilder(FilterSet {
68 filter_type: FilterType::Way,
69 inputs: sets.into_iter().map(|i| i.into()).collect(),
70 ..Default::default()
71 })
72 }
73
74 pub fn all_relations<'a>() -> FilterSetBuilder<'a> {
76 FilterSetBuilder(FilterSet {
77 filter_type: FilterType::Relation,
78 ..Default::default()
79 })
80 }
81
82 pub fn relations_from<'a, T>(sets: impl IntoIterator<Item=T>)
84 -> FilterSetBuilder<'a>
85 where T: Into<Cow<'a, Set<'a>>> {
86 FilterSetBuilder(FilterSet {
87 filter_type: FilterType::Relation,
88 inputs: sets.into_iter().map(|i| i.into()).collect(),
89 ..Default::default()
90 })
91 }
92
93 pub fn any_type<'a>() -> FilterSetBuilder<'a> {
95 FilterSetBuilder(FilterSet {
96 filter_type: FilterType::Any,
97 ..Default::default()
98 })
99 }
100
101 pub fn any_from<'a, T>(sets: impl IntoIterator<Item=T>)
103 -> FilterSetBuilder<'a>
104 where T: Into<Cow<'a, Set<'a>>> {
105 FilterSetBuilder(FilterSet {
106 filter_type: FilterType::Any,
107 inputs: sets.into_iter().map(|i| i.into()).collect(),
108 ..Default::default()
109 })
110 }
111
112 pub fn all_nodes_or_ways<'a>() -> FilterSetBuilder<'a> {
114 FilterSetBuilder(FilterSet {
115 filter_type: FilterType::NodeOrWay,
116 ..Default::default()
117 })
118 }
119
120 pub fn nodes_or_ways_from<'a, T>(sets: impl IntoIterator<Item=T>)
122 -> FilterSetBuilder<'a>
123 where T: Into<Cow<'a, Set<'a>>> {
124 FilterSetBuilder(FilterSet {
125 filter_type: FilterType::NodeOrWay,
126 inputs: sets.into_iter().map(|i| i.into()).collect(),
127 ..Default::default()
128 })
129 }
130
131 pub fn all_nodes_or_relations<'a>() -> FilterSetBuilder<'a> {
133 FilterSetBuilder(FilterSet {
134 filter_type: FilterType::NodeOrRelation,
135 ..Default::default()
136 })
137 }
138
139 pub fn nodes_or_relations_from<'a, T>(sets: impl IntoIterator<Item=T>)
141 -> FilterSetBuilder<'a>
142 where T: Into<Cow<'a, Set<'a>>> {
143 FilterSetBuilder(FilterSet {
144 filter_type: FilterType::NodeOrRelation,
145 inputs: sets.into_iter().map(|i| i.into()).collect(),
146 ..Default::default()
147 })
148 }
149
150 pub fn all_ways_or_relations<'a>() -> FilterSetBuilder<'a> {
152 FilterSetBuilder(FilterSet {
153 filter_type: FilterType::WayOrRelation,
154 ..Default::default()
155 })
156 }
157
158 pub fn ways_or_relations_from<'a, T>(sets: impl IntoIterator<Item=T>)
160 -> FilterSetBuilder<'a>
161 where T: Into<Cow<'a, Set<'a>>> {
162 FilterSetBuilder(FilterSet {
163 filter_type: FilterType::WayOrRelation,
164 inputs: sets.into_iter().map(|i| i.into()).collect(),
165 ..Default::default()
166 })
167 }
168 pub fn all_areas<'a>() -> FilterSetBuilder<'a> {
188 FilterSetBuilder(FilterSet {
189 filter_type: FilterType::Area,
190 ..Default::default()
191 })
192 }
193
194 pub fn areas_from<'a, T>(sets: impl IntoIterator<Item=T>)
196 -> FilterSetBuilder<'a>
197 where T: Into<Cow<'a, Set<'a>>> {
198 FilterSetBuilder(FilterSet {
199 filter_type: FilterType::Area,
200 inputs: sets.into_iter().map(|i| i.into()).collect(),
201 ..Default::default()
202 })
203 }
204}
205
206impl<'a> FilterSetBuilder<'a> {
207 pub fn with_id(mut self, id: i64) -> Self {
209 self.0.id_filters.clear();
210 self.0.id_filters.insert(id);
211 self
212 }
213
214 pub fn with_ids(mut self, ids: impl IntoIterator<Item=i64>) -> Self {
216 self.0.id_filters.clear();
217 for id in ids {
218 self.0.id_filters.insert(id);
219 }
220 self
221 }
222
223 pub fn within_bounds(mut self, bbox: impl Into<Bbox>) -> Self {
225 self.0.bbox_filter = Some(bbox.into());
226 self
227 }
228
229 pub fn with_tag(mut self, tag: &'a str) -> Self {
231 self.0.tag_filters.insert(TagFilter::exists(tag));
232 self
233 }
234
235 pub fn without_tag(mut self, tag: &'a str) -> Self {
237 self.0.tag_filters.insert(TagFilter::not_exists(tag));
238 self
239 }
240
241 pub fn with_tag_value(mut self, tag: &'a str, value: &'a str) -> Self {
243 self.0.tag_filters.insert(TagFilter::equals(tag, value));
244 self
245 }
246
247 pub fn without_tag_value(mut self, tag: &'a str, value: &'a str) -> Self {
250 self.0.tag_filters.insert(TagFilter::not_equals(tag, value));
251 self
252 }
253
254 pub fn with_tag_value_matching(mut self, tag: &'a str, value_pat: &'a str) -> Self {
257 self.0.tag_filters.insert(TagFilter::matches(tag, value_pat));
258 self
259 }
260
261 pub fn without_tag_value_matching(mut self, tag: &'a str, value_pat: &'a str) -> Self {
265 self.0.tag_filters.insert(TagFilter::not_matches(tag, value_pat));
266 self
267 }
268
269 pub fn with_tag_name_and_value_matching(mut self, tag_pat: &'a str, value_pat: &'a str) -> Self {
273 self.0.tag_filters.insert(TagFilter::name_value_matches(tag_pat, value_pat));
274 self
275 }
276
277 pub fn with_tags(mut self, tags: impl IntoIterator<Item=&'a str>) -> Self {
279 for i in tags {
280 self.0.tag_filters.insert(TagFilter::Exists(SaniStr(i)));
281 }
282 self
283 }
284
285 pub fn without_tags(mut self, tags: impl IntoIterator<Item=&'a str>) -> Self {
287 for i in tags {
288 self.0.tag_filters.insert(TagFilter::NotExists(SaniStr(i)));
289 }
290 self
291 }
292
293 pub fn with_tag_values(
295 mut self,
296 tags: impl IntoIterator<Item=(&'a str, &'a str)>,
297 ) -> Self {
298 for (n, v) in tags {
299 self.0.tag_filters.insert(TagFilter::Equals(SaniStr(n), SaniStr(v)));
300 }
301 self
302 }
303
304 pub fn without_tag_values(
307 mut self,
308 tags: impl IntoIterator<Item=(&'a str, &'a str)>,
309 ) -> Self {
310 for (n, v) in tags {
311 self.0.tag_filters.insert(TagFilter::NotEquals(SaniStr(n), SaniStr(v)));
312 }
313 self
314 }
315
316 pub fn with_tag_values_matching(
319 mut self,
320 tags: impl IntoIterator<Item=(&'a str, &'a str)>,
321 ) -> Self {
322 for (n, v) in tags {
323 self.0.tag_filters.insert(TagFilter::Matches(SaniStr(n), SaniStr(v)));
324 }
325 self
326 }
327
328 pub fn without_tag_values_matching(
332 mut self,
333 tags: impl IntoIterator<Item=(&'a str, &'a str)>,
334 ) -> Self {
335 for (n, v) in tags {
336 self.0.tag_filters.insert(TagFilter::NotMatches(SaniStr(n), SaniStr(v)));
337 }
338 self
339 }
340
341 pub fn with_tag_names_and_values_matching(
345 mut self,
346 tags: impl IntoIterator<Item=(&'a str, &'a str)>,
347 ) -> Self {
348 for (n, v) in tags {
349 self.0.tag_filters.insert(TagFilter::NameValueMatches(SaniStr(n), SaniStr(v)));
350 }
351 self
352 }
353
354 pub fn within_ways(mut self, set: impl Into<Cow<'a, Set<'a>>>) -> Self {
357 self.0.recurse_filters.insert(RecurseFilter::WithinWays { input: set.into() });
358 self
359 }
360
361 pub fn within_relations(mut self, set: impl Into<Cow<'a, Set<'a>>>) -> Self {
364 self.0.recurse_filters.insert(RecurseFilter::WithinRelations { input: set.into(), role: None });
365 self
366 }
367
368 pub fn within_relations_with_role(
372 mut self,
373 role: &'a str,
374 set: impl Into<Cow<'a, Set<'a>>>,
375 ) -> Self {
376 self.0.recurse_filters.insert(RecurseFilter::WithinRelations { input: set.into(), role: Some(SaniStr(role)) });
377 self
378 }
379
380 pub fn containing_nodes(mut self, set: impl Into<Cow<'a, Set<'a>>>) -> Self {
383 self.0.recurse_filters.insert(RecurseFilter::ContainingNodes { input: set.into(), role: None });
384 self
385 }
386
387 pub fn containing_nodes_with_role(
391 mut self,
392 role: &'a str,
393 set: impl Into<Cow<'a, Set<'a>>>,
394 ) -> Self {
395 self.0.recurse_filters.insert(RecurseFilter::ContainingNodes { input: set.into(), role: Some(SaniStr(role)) });
396 self
397 }
398
399 pub fn containing_ways(mut self, set: impl Into<Cow<'a, Set<'a>>>) -> Self {
402 self.0.recurse_filters.insert(RecurseFilter::ContainingWays { input: set.into(), role: None });
403 self
404 }
405
406 pub fn containing_ways_with_role(
410 mut self,
411 role: &'a str,
412 set: impl Into<Cow<'a, Set<'a>>>,
413 ) -> Self {
414 self.0.recurse_filters.insert(RecurseFilter::ContainingWays { input: set.into(), role: Some(SaniStr(role)) });
415 self
416 }
417
418 pub fn containing_relations(mut self, set: impl Into<Cow<'a, Set<'a>>>) -> Self {
421 self.0.recurse_filters.insert(RecurseFilter::ContainingRelations { input: set.into(), role: None });
422 self
423 }
424
425 pub fn containing_relations_with_role(
429 mut self,
430 role: &'a str,
431 set: impl Into<Cow<'a, Set<'a>>>,
432 ) -> Self {
433 self.0.recurse_filters.insert(RecurseFilter::ContainingRelations { input: set.into(), role: Some(SaniStr(role)) });
434 self
435 }
436}
437
438#[cfg(test)]
439mod test {
440 use super::*;
441
442 fn _all_nodes_from() {
443 let _ = SetBuilder::nodes_from(SetBuilder::all_nodes());
444
445 let set: Set = SetBuilder::all_ways().into();
446 let _ = SetBuilder::nodes_from([&set]);
447 }
448}