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
177
178
179
180
181
182
183
184
185
186
// Copyright 2018 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

use ::enum_set::{
    EnumSet,
};

use ::types::{
    ValueType,
};

trait EnumSetExtensions<T: ::enum_set::CLike + Clone> {
    /// Return a set containing both `x` and `y`.
    fn of_both(x: T, y: T) -> EnumSet<T>;

    /// Return a clone of `self` with `y` added.
    fn with(&self, y: T) -> EnumSet<T>;
}

impl<T: ::enum_set::CLike + Clone> EnumSetExtensions<T> for EnumSet<T> {
    /// Return a set containing both `x` and `y`.
    fn of_both(x: T, y: T) -> Self {
        let mut o = EnumSet::new();
        o.insert(x);
        o.insert(y);
        o
    }

    /// Return a clone of `self` with `y` added.
    fn with(&self, y: T) -> EnumSet<T> {
        let mut o = self.clone();
        o.insert(y);
        o
    }
}


#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ValueTypeSet(pub EnumSet<ValueType>);

impl Default for ValueTypeSet {
    fn default() -> ValueTypeSet {
        ValueTypeSet::any()
    }
}

impl ValueTypeSet {
    pub fn any() -> ValueTypeSet {
        ValueTypeSet(ValueType::all_enums())
    }

    pub fn none() -> ValueTypeSet {
        ValueTypeSet(EnumSet::new())
    }

    /// Return a set containing only `t`.
    pub fn of_one(t: ValueType) -> ValueTypeSet {
        let mut s = EnumSet::new();
        s.insert(t);
        ValueTypeSet(s)
    }

    /// Return a set containing `Double` and `Long`.
    pub fn of_numeric_types() -> ValueTypeSet {
        ValueTypeSet(EnumSet::of_both(ValueType::Double, ValueType::Long))
    }

    /// Return a set containing `Double`, `Long`, and `Instant`.
    pub fn of_numeric_and_instant_types() -> ValueTypeSet {
        let mut s = EnumSet::new();
        s.insert(ValueType::Double);
        s.insert(ValueType::Long);
        s.insert(ValueType::Instant);
        ValueTypeSet(s)
    }

    /// Return a set containing `Ref` and `Keyword`.
    pub fn of_keywords() -> ValueTypeSet {
        ValueTypeSet(EnumSet::of_both(ValueType::Ref, ValueType::Keyword))
    }

    /// Return a set containing `Ref` and `Long`.
    pub fn of_longs() -> ValueTypeSet {
        ValueTypeSet(EnumSet::of_both(ValueType::Ref, ValueType::Long))
    }
}

impl ValueTypeSet {
    pub fn insert(&mut self, vt: ValueType) -> bool {
        self.0.insert(vt)
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns a set containing all the types in this set and `other`.
    pub fn union(&self, other: &ValueTypeSet) -> ValueTypeSet {
        ValueTypeSet(self.0.union(other.0))
    }

    pub fn intersection(&self, other: &ValueTypeSet) -> ValueTypeSet {
        ValueTypeSet(self.0.intersection(other.0))
    }

    /// Returns the set difference between `self` and `other`, which is the
    /// set of items in `self` that are not in `other`.
    pub fn difference(&self, other: &ValueTypeSet) -> ValueTypeSet {
        ValueTypeSet(self.0 - other.0)
    }

    /// Return an arbitrary type that's part of this set.
    /// For a set containing a single type, this will be that type.
    pub fn exemplar(&self) -> Option<ValueType> {
        self.0.iter().next()
    }

    pub fn is_subset(&self, other: &ValueTypeSet) -> bool {
        self.0.is_subset(&other.0)
    }

    /// Returns true if `self` and `other` contain no items in common.
    pub fn is_disjoint(&self, other: &ValueTypeSet) -> bool {
        self.0.is_disjoint(&other.0)
    }

    pub fn contains(&self, vt: ValueType) -> bool {
        self.0.contains(&vt)
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn is_unit(&self) -> bool {
        self.0.len() == 1
    }

    pub fn iter(&self) -> ::enum_set::Iter<ValueType> {
        self.0.iter()
    }
}

impl From<ValueType> for ValueTypeSet {
    fn from(t: ValueType) -> Self {
        ValueTypeSet::of_one(t)
    }
}

impl ValueTypeSet {
    pub fn is_only_numeric(&self) -> bool {
        self.is_subset(&ValueTypeSet::of_numeric_types())
    }
}

impl IntoIterator for ValueTypeSet {
    type Item = ValueType;
    type IntoIter = ::enum_set::Iter<ValueType>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl ::std::iter::FromIterator<ValueType> for ValueTypeSet {
    fn from_iter<I: IntoIterator<Item = ValueType>>(iterator: I) -> Self {
        let mut ret = Self::none();
        ret.0.extend(iterator);
        ret
    }
}

impl ::std::iter::Extend<ValueType> for ValueTypeSet {
    fn extend<I: IntoIterator<Item = ValueType>>(&mut self, iter: I) {
        for element in iter {
            self.0.insert(element);
        }
    }
}