Skip to main content

overpass_lib/builder/
union.rs

1use std::{borrow::Cow, collections::HashSet};
2use crate::{FilterSetBuilder, Set, SetBuilder, UnionSet, builder::Builder};
3
4/// Trait to daisy-chain [SetBuilder]s together into [UnionSetBuilder]s.
5pub trait UnionWith<'a> {
6    /// Combine this [Set] together with another into a [UnionSet].
7    fn union_with(self, other: impl Into<Cow<'a, Set<'a>>>) -> UnionSetBuilder<'a>;
8}
9
10/// A convenient builder API for [UnionSet].
11pub struct UnionSetBuilder<'a>(
12    /// The set being modified.
13    pub UnionSet<'a>,
14);
15
16impl<'a> Builder<'a> for UnionSetBuilder<'a> {}
17
18impl<'a> Into<Set<'a>> for UnionSetBuilder<'a> {
19    fn into(self) -> Set<'a> {
20        self.0.into()
21    }
22}
23
24impl<'a> Into<Cow<'a, Set<'a>>> for UnionSetBuilder<'a> {
25    fn into(self) -> Cow<'a, Set<'a>> {
26        Cow::Owned(self.into())
27    }
28}
29
30impl<'a> IntoIterator for UnionSetBuilder<'a> {
31    type Item = UnionSetBuilder<'a>;
32    type IntoIter = std::array::IntoIter<Self::Item, 1>;
33    fn into_iter(self) -> Self::IntoIter {
34        [self].into_iter()
35    }
36}
37
38impl<'a> UnionWith<'a> for FilterSetBuilder<'a> {
39    fn union_with(self, other: impl Into<Cow<'a, Set<'a>>>) -> UnionSetBuilder<'a> {
40        UnionSetBuilder(UnionSet(HashSet::from([self.into(), other.into()])))
41    }
42}
43
44impl<'a> UnionWith<'a> for UnionSetBuilder<'a> {
45    fn union_with(mut self, other: impl Into<Cow<'a, Set<'a>>>) -> Self {
46        self.0.0.insert(other.into());
47        self
48    }
49}
50
51impl SetBuilder {
52    /// Collect the provided sets into a new [UnionSet]
53    pub fn union<'a, T>(sets: impl IntoIterator<Item=T>) -> UnionSetBuilder<'a>
54    where T: Into<Cow<'a, Set<'a>>> {
55        UnionSetBuilder(sets.into_iter().collect())
56    }
57}
58