Skip to main content

overpass_lib/query/
set.rs

1use std::{
2    borrow::Cow,
3    fmt::Write,
4    hash::{Hash, Hasher},
5};
6use crate::{FilterSet, Namer, OverpassQLError, OverpassQLNamed, UnionSet};
7#[cfg(doc)]
8use crate::{Element};
9
10/// An abstract collection of [Element]s selected from the full database based on given criteria.
11/// 
12/// [wiki](https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#Sets)
13#[derive(Debug, Clone)]
14pub enum Set<'a> {
15    /// The standard query statement. See [FilterSet].
16    Filter(FilterSet<'a>),
17    /// The union block statement. See [UnionSet].
18    Union(UnionSet<'a>),
19}
20
21impl Default for Set<'_> {
22    fn default() -> Self {
23        Self::Filter(FilterSet::default())
24    }
25}
26
27impl<'a> From<FilterSet<'a>> for Set<'a> {
28    fn from(value: FilterSet<'a>) -> Self {
29        Self::Filter(value)
30    }
31}
32
33impl<'a> From<UnionSet<'a>> for Set<'a> {
34    fn from(value: UnionSet<'a>) -> Self {
35        Self::Union(value)
36    }
37}
38
39impl<'a> OverpassQLNamed<'a> for Set<'a> {
40    fn fmt_oql_named<'b, 'c>(&'b self,
41        f: &mut impl Write,
42        namer: &mut Namer<'a, 'c>,
43    ) -> Result<(), OverpassQLError>
44    where 'b: 'c {
45        match self {
46            Self::Filter(filter) => filter.fmt_oql_named(f, namer),
47            Self::Union(union) => union.fmt_oql_named(f, namer),
48        }?;
49
50        if let Some(name) = namer.get_or_assign(self) {
51            write!(f, "->.{name}")?;
52        }
53
54        Ok(())
55    }
56}
57
58impl<'a> Set<'a> {
59    /// Returns an iterator of sets that must be defined before this one.
60    pub fn dependencies(&self) -> impl ExactSizeIterator<Item=&Set<'a>> {
61        match self {
62            Self::Filter(f) => f.dependencies(),
63            Self::Union(u) => u.dependencies(),
64        }
65    }
66}
67
68impl PartialEq for Set<'_> {
69    fn eq(&self, other: &Self) -> bool {
70        std::ptr::eq(self, other)
71    }
72}
73impl Eq for Set<'_> {}
74
75impl Hash for Set<'_> {
76    fn hash<H: Hasher>(&self, state: &mut H) {
77        std::ptr::hash(self, state)
78    }
79}
80
81impl<'a> Into<Cow<'a, Set<'a>>> for Set<'a> {
82    fn into(self) -> Cow<'a, Set<'a>> {
83        Cow::Owned(self)
84    }
85}
86
87impl<'a> Into<Cow<'a, Set<'a>>> for &'a Set<'a> {
88    fn into(self) -> Cow<'a, Set<'a>> {
89        Cow::Borrowed(self)
90    }
91}
92