xref: /aosp_15_r20/external/bazelbuild-rules_rust/crate_universe/src/select.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan use std::collections::{BTreeMap, BTreeSet};
2*d4726bddSHONG Yifan use std::fmt::Debug;
3*d4726bddSHONG Yifan 
4*d4726bddSHONG Yifan use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
5*d4726bddSHONG Yifan 
6*d4726bddSHONG Yifan /// A wrapper around values where some values may be conditionally included (e.g. only on a certain platform), and others are unconditional.
7*d4726bddSHONG Yifan #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
8*d4726bddSHONG Yifan pub struct Select<T>
9*d4726bddSHONG Yifan where
10*d4726bddSHONG Yifan     T: Selectable,
11*d4726bddSHONG Yifan {
12*d4726bddSHONG Yifan     common: T::CommonType,
13*d4726bddSHONG Yifan     selects: BTreeMap<String, T::SelectsType>,
14*d4726bddSHONG Yifan }
15*d4726bddSHONG Yifan 
16*d4726bddSHONG Yifan pub trait Selectable
17*d4726bddSHONG Yifan where
18*d4726bddSHONG Yifan     Self: SelectableValue,
19*d4726bddSHONG Yifan {
20*d4726bddSHONG Yifan     type ItemType: SelectableValue;
21*d4726bddSHONG Yifan     type CommonType: SelectableValue + Default;
22*d4726bddSHONG Yifan     type SelectsType: SelectableValue;
23*d4726bddSHONG Yifan 
is_empty(this: &Select<Self>) -> bool24*d4726bddSHONG Yifan     fn is_empty(this: &Select<Self>) -> bool;
insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>)25*d4726bddSHONG Yifan     fn insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>);
26*d4726bddSHONG Yifan 
items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>27*d4726bddSHONG Yifan     fn items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>;
values(this: &Select<Self>) -> Vec<Self::ItemType>28*d4726bddSHONG Yifan     fn values(this: &Select<Self>) -> Vec<Self::ItemType>;
29*d4726bddSHONG Yifan 
merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>30*d4726bddSHONG Yifan     fn merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>;
31*d4726bddSHONG Yifan }
32*d4726bddSHONG Yifan 
33*d4726bddSHONG Yifan // Replace with `trait_alias` once stabilized.
34*d4726bddSHONG Yifan // https://github.com/rust-lang/rust/issues/41517
35*d4726bddSHONG Yifan pub trait SelectableValue
36*d4726bddSHONG Yifan where
37*d4726bddSHONG Yifan     Self: Debug + Clone + PartialEq + Eq + Serialize + DeserializeOwned,
38*d4726bddSHONG Yifan {
39*d4726bddSHONG Yifan }
40*d4726bddSHONG Yifan 
41*d4726bddSHONG Yifan impl<T> SelectableValue for T where T: Debug + Clone + PartialEq + Eq + Serialize + DeserializeOwned {}
42*d4726bddSHONG Yifan 
43*d4726bddSHONG Yifan // Replace with `trait_alias` once stabilized.
44*d4726bddSHONG Yifan // https://github.com/rust-lang/rust/issues/41517
45*d4726bddSHONG Yifan pub trait SelectableOrderedValue
46*d4726bddSHONG Yifan where
47*d4726bddSHONG Yifan     Self: SelectableValue + PartialOrd + Ord,
48*d4726bddSHONG Yifan {
49*d4726bddSHONG Yifan }
50*d4726bddSHONG Yifan 
51*d4726bddSHONG Yifan impl<T> SelectableOrderedValue for T where T: SelectableValue + PartialOrd + Ord {}
52*d4726bddSHONG Yifan 
53*d4726bddSHONG Yifan pub(crate) trait SelectableScalar
54*d4726bddSHONG Yifan where
55*d4726bddSHONG Yifan     Self: SelectableValue,
56*d4726bddSHONG Yifan {
57*d4726bddSHONG Yifan }
58*d4726bddSHONG Yifan 
59*d4726bddSHONG Yifan impl SelectableScalar for String {}
60*d4726bddSHONG Yifan impl SelectableScalar for bool {}
61*d4726bddSHONG Yifan impl SelectableScalar for i64 {}
62*d4726bddSHONG Yifan 
63*d4726bddSHONG Yifan // General
64*d4726bddSHONG Yifan impl<T> Select<T>
65*d4726bddSHONG Yifan where
66*d4726bddSHONG Yifan     T: Selectable,
67*d4726bddSHONG Yifan {
new() -> Self68*d4726bddSHONG Yifan     pub(crate) fn new() -> Self {
69*d4726bddSHONG Yifan         Self {
70*d4726bddSHONG Yifan             common: T::CommonType::default(),
71*d4726bddSHONG Yifan             selects: BTreeMap::new(),
72*d4726bddSHONG Yifan         }
73*d4726bddSHONG Yifan     }
74*d4726bddSHONG Yifan 
from_value(value: T::CommonType) -> Self75*d4726bddSHONG Yifan     pub(crate) fn from_value(value: T::CommonType) -> Self {
76*d4726bddSHONG Yifan         Self {
77*d4726bddSHONG Yifan             common: value,
78*d4726bddSHONG Yifan             selects: BTreeMap::new(),
79*d4726bddSHONG Yifan         }
80*d4726bddSHONG Yifan     }
81*d4726bddSHONG Yifan 
82*d4726bddSHONG Yifan     /// Whether there zero values in this collection, common or configuration-specific.
is_empty(&self) -> bool83*d4726bddSHONG Yifan     pub fn is_empty(&self) -> bool {
84*d4726bddSHONG Yifan         T::is_empty(self)
85*d4726bddSHONG Yifan     }
86*d4726bddSHONG Yifan 
87*d4726bddSHONG Yifan     /// A list of the configurations which have some configuration-specific value associated.
configurations(&self) -> BTreeSet<String>88*d4726bddSHONG Yifan     pub fn configurations(&self) -> BTreeSet<String> {
89*d4726bddSHONG Yifan         self.selects.keys().cloned().collect()
90*d4726bddSHONG Yifan     }
91*d4726bddSHONG Yifan 
92*d4726bddSHONG Yifan     /// All values and their associated configurations, if any.
items(&self) -> Vec<(Option<String>, T::ItemType)>93*d4726bddSHONG Yifan     pub fn items(&self) -> Vec<(Option<String>, T::ItemType)> {
94*d4726bddSHONG Yifan         T::items(self)
95*d4726bddSHONG Yifan     }
96*d4726bddSHONG Yifan 
97*d4726bddSHONG Yifan     /// All values, whether common or configured.
values(&self) -> Vec<T::ItemType>98*d4726bddSHONG Yifan     pub fn values(&self) -> Vec<T::ItemType> {
99*d4726bddSHONG Yifan         T::values(self)
100*d4726bddSHONG Yifan     }
101*d4726bddSHONG Yifan 
insert(&mut self, value: T::ItemType, configuration: Option<String>)102*d4726bddSHONG Yifan     pub(crate) fn insert(&mut self, value: T::ItemType, configuration: Option<String>) {
103*d4726bddSHONG Yifan         T::insert(self, value, configuration);
104*d4726bddSHONG Yifan     }
105*d4726bddSHONG Yifan 
into_parts(self) -> (T::CommonType, BTreeMap<String, T::SelectsType>)106*d4726bddSHONG Yifan     pub(crate) fn into_parts(self) -> (T::CommonType, BTreeMap<String, T::SelectsType>) {
107*d4726bddSHONG Yifan         (self.common, self.selects)
108*d4726bddSHONG Yifan     }
109*d4726bddSHONG Yifan 
merge(lhs: Self, rhs: Self) -> Self110*d4726bddSHONG Yifan     pub(crate) fn merge(lhs: Self, rhs: Self) -> Self {
111*d4726bddSHONG Yifan         T::merge(lhs, rhs)
112*d4726bddSHONG Yifan     }
113*d4726bddSHONG Yifan }
114*d4726bddSHONG Yifan 
115*d4726bddSHONG Yifan impl<T> Default for Select<T>
116*d4726bddSHONG Yifan where
117*d4726bddSHONG Yifan     T: Selectable,
118*d4726bddSHONG Yifan {
default() -> Self119*d4726bddSHONG Yifan     fn default() -> Self {
120*d4726bddSHONG Yifan         Self::new()
121*d4726bddSHONG Yifan     }
122*d4726bddSHONG Yifan }
123*d4726bddSHONG Yifan 
124*d4726bddSHONG Yifan impl<'de, T> Deserialize<'de> for Select<T>
125*d4726bddSHONG Yifan where
126*d4726bddSHONG Yifan     T: Selectable,
127*d4726bddSHONG Yifan {
deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>,128*d4726bddSHONG Yifan     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
129*d4726bddSHONG Yifan     where
130*d4726bddSHONG Yifan         D: Deserializer<'de>,
131*d4726bddSHONG Yifan     {
132*d4726bddSHONG Yifan         #[derive(Debug, Deserialize)]
133*d4726bddSHONG Yifan         #[serde(untagged)]
134*d4726bddSHONG Yifan         enum Either<T>
135*d4726bddSHONG Yifan         where
136*d4726bddSHONG Yifan             T: Selectable,
137*d4726bddSHONG Yifan         {
138*d4726bddSHONG Yifan             Select {
139*d4726bddSHONG Yifan                 common: T::CommonType,
140*d4726bddSHONG Yifan                 selects: BTreeMap<String, T::SelectsType>,
141*d4726bddSHONG Yifan             },
142*d4726bddSHONG Yifan             Value(T::CommonType),
143*d4726bddSHONG Yifan         }
144*d4726bddSHONG Yifan 
145*d4726bddSHONG Yifan         let either = Either::<T>::deserialize(deserializer)?;
146*d4726bddSHONG Yifan         match either {
147*d4726bddSHONG Yifan             Either::Select { common, selects } => Ok(Self { common, selects }),
148*d4726bddSHONG Yifan             Either::Value(common) => Ok(Self {
149*d4726bddSHONG Yifan                 common,
150*d4726bddSHONG Yifan                 selects: BTreeMap::new(),
151*d4726bddSHONG Yifan             }),
152*d4726bddSHONG Yifan         }
153*d4726bddSHONG Yifan     }
154*d4726bddSHONG Yifan }
155*d4726bddSHONG Yifan 
156*d4726bddSHONG Yifan // Scalar
157*d4726bddSHONG Yifan impl<T> Selectable for T
158*d4726bddSHONG Yifan where
159*d4726bddSHONG Yifan     T: SelectableScalar,
160*d4726bddSHONG Yifan {
161*d4726bddSHONG Yifan     type ItemType = T;
162*d4726bddSHONG Yifan     type CommonType = Option<Self::ItemType>;
163*d4726bddSHONG Yifan     type SelectsType = Self::ItemType;
164*d4726bddSHONG Yifan 
is_empty(this: &Select<Self>) -> bool165*d4726bddSHONG Yifan     fn is_empty(this: &Select<Self>) -> bool {
166*d4726bddSHONG Yifan         this.common.is_none() && this.selects.is_empty()
167*d4726bddSHONG Yifan     }
168*d4726bddSHONG Yifan 
items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>169*d4726bddSHONG Yifan     fn items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)> {
170*d4726bddSHONG Yifan         let mut result = Vec::new();
171*d4726bddSHONG Yifan         if let Some(value) = this.common.as_ref() {
172*d4726bddSHONG Yifan             result.push((None, value.clone()));
173*d4726bddSHONG Yifan         }
174*d4726bddSHONG Yifan         result.extend(
175*d4726bddSHONG Yifan             this.selects
176*d4726bddSHONG Yifan                 .iter()
177*d4726bddSHONG Yifan                 .map(|(configuration, value)| (Some(configuration.clone()), value.clone())),
178*d4726bddSHONG Yifan         );
179*d4726bddSHONG Yifan         result
180*d4726bddSHONG Yifan     }
181*d4726bddSHONG Yifan 
values(this: &Select<Self>) -> Vec<Self::ItemType>182*d4726bddSHONG Yifan     fn values(this: &Select<Self>) -> Vec<Self::ItemType> {
183*d4726bddSHONG Yifan         let mut result = Vec::new();
184*d4726bddSHONG Yifan         if let Some(value) = this.common.as_ref() {
185*d4726bddSHONG Yifan             result.push(value.clone());
186*d4726bddSHONG Yifan         }
187*d4726bddSHONG Yifan         result.extend(this.selects.values().cloned());
188*d4726bddSHONG Yifan         result
189*d4726bddSHONG Yifan     }
190*d4726bddSHONG Yifan 
insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>)191*d4726bddSHONG Yifan     fn insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>) {
192*d4726bddSHONG Yifan         match configuration {
193*d4726bddSHONG Yifan             None => {
194*d4726bddSHONG Yifan                 this.selects
195*d4726bddSHONG Yifan                     .retain(|_, existing_value| existing_value != &value);
196*d4726bddSHONG Yifan                 this.common = Some(value);
197*d4726bddSHONG Yifan             }
198*d4726bddSHONG Yifan             Some(configuration) => {
199*d4726bddSHONG Yifan                 if Some(&value) != this.common.as_ref() {
200*d4726bddSHONG Yifan                     this.selects.insert(configuration, value);
201*d4726bddSHONG Yifan                 }
202*d4726bddSHONG Yifan             }
203*d4726bddSHONG Yifan         }
204*d4726bddSHONG Yifan     }
205*d4726bddSHONG Yifan 
merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>206*d4726bddSHONG Yifan     fn merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self> {
207*d4726bddSHONG Yifan         let mut result: Select<Self> = Select::new();
208*d4726bddSHONG Yifan 
209*d4726bddSHONG Yifan         if let Some(value) = lhs.common {
210*d4726bddSHONG Yifan             result.insert(value, None);
211*d4726bddSHONG Yifan         }
212*d4726bddSHONG Yifan         if let Some(value) = rhs.common {
213*d4726bddSHONG Yifan             result.insert(value, None);
214*d4726bddSHONG Yifan         }
215*d4726bddSHONG Yifan 
216*d4726bddSHONG Yifan         for (configuration, value) in lhs.selects.into_iter() {
217*d4726bddSHONG Yifan             result.insert(value, Some(configuration));
218*d4726bddSHONG Yifan         }
219*d4726bddSHONG Yifan         for (configuration, value) in rhs.selects.into_iter() {
220*d4726bddSHONG Yifan             result.insert(value, Some(configuration));
221*d4726bddSHONG Yifan         }
222*d4726bddSHONG Yifan 
223*d4726bddSHONG Yifan         result
224*d4726bddSHONG Yifan     }
225*d4726bddSHONG Yifan }
226*d4726bddSHONG Yifan 
227*d4726bddSHONG Yifan // Vec<T>
228*d4726bddSHONG Yifan impl<T> Selectable for Vec<T>
229*d4726bddSHONG Yifan where
230*d4726bddSHONG Yifan     T: SelectableValue,
231*d4726bddSHONG Yifan {
232*d4726bddSHONG Yifan     type ItemType = T;
233*d4726bddSHONG Yifan     type CommonType = Vec<T>;
234*d4726bddSHONG Yifan     type SelectsType = Vec<T>;
235*d4726bddSHONG Yifan 
is_empty(this: &Select<Self>) -> bool236*d4726bddSHONG Yifan     fn is_empty(this: &Select<Self>) -> bool {
237*d4726bddSHONG Yifan         this.common.is_empty() && this.selects.is_empty()
238*d4726bddSHONG Yifan     }
239*d4726bddSHONG Yifan 
items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>240*d4726bddSHONG Yifan     fn items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)> {
241*d4726bddSHONG Yifan         let mut result = Vec::new();
242*d4726bddSHONG Yifan         result.extend(this.common.iter().map(|value| (None, value.clone())));
243*d4726bddSHONG Yifan         result.extend(this.selects.iter().flat_map(|(configuration, values)| {
244*d4726bddSHONG Yifan             values
245*d4726bddSHONG Yifan                 .iter()
246*d4726bddSHONG Yifan                 .map(|value| (Some(configuration.clone()), value.clone()))
247*d4726bddSHONG Yifan         }));
248*d4726bddSHONG Yifan         result
249*d4726bddSHONG Yifan     }
250*d4726bddSHONG Yifan 
values(this: &Select<Self>) -> Vec<Self::ItemType>251*d4726bddSHONG Yifan     fn values(this: &Select<Self>) -> Vec<Self::ItemType> {
252*d4726bddSHONG Yifan         let mut result = Vec::new();
253*d4726bddSHONG Yifan         result.extend(this.common.iter().cloned());
254*d4726bddSHONG Yifan         result.extend(
255*d4726bddSHONG Yifan             this.selects
256*d4726bddSHONG Yifan                 .values()
257*d4726bddSHONG Yifan                 .flat_map(|values| values.iter().cloned()),
258*d4726bddSHONG Yifan         );
259*d4726bddSHONG Yifan         result
260*d4726bddSHONG Yifan     }
261*d4726bddSHONG Yifan 
insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>)262*d4726bddSHONG Yifan     fn insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>) {
263*d4726bddSHONG Yifan         match configuration {
264*d4726bddSHONG Yifan             None => this.common.push(value),
265*d4726bddSHONG Yifan             Some(configuration) => this.selects.entry(configuration).or_default().push(value),
266*d4726bddSHONG Yifan         }
267*d4726bddSHONG Yifan     }
268*d4726bddSHONG Yifan 
merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>269*d4726bddSHONG Yifan     fn merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self> {
270*d4726bddSHONG Yifan         let mut result: Select<Self> = Select::new();
271*d4726bddSHONG Yifan 
272*d4726bddSHONG Yifan         for value in lhs.common.into_iter() {
273*d4726bddSHONG Yifan             result.insert(value, None);
274*d4726bddSHONG Yifan         }
275*d4726bddSHONG Yifan         for value in rhs.common.into_iter() {
276*d4726bddSHONG Yifan             result.insert(value, None);
277*d4726bddSHONG Yifan         }
278*d4726bddSHONG Yifan 
279*d4726bddSHONG Yifan         for (configuration, values) in lhs.selects.into_iter() {
280*d4726bddSHONG Yifan             for value in values.into_iter() {
281*d4726bddSHONG Yifan                 result.insert(value, Some(configuration.clone()));
282*d4726bddSHONG Yifan             }
283*d4726bddSHONG Yifan         }
284*d4726bddSHONG Yifan         for (configuration, values) in rhs.selects.into_iter() {
285*d4726bddSHONG Yifan             for value in values.into_iter() {
286*d4726bddSHONG Yifan                 result.insert(value, Some(configuration.clone()));
287*d4726bddSHONG Yifan             }
288*d4726bddSHONG Yifan         }
289*d4726bddSHONG Yifan 
290*d4726bddSHONG Yifan         result
291*d4726bddSHONG Yifan     }
292*d4726bddSHONG Yifan }
293*d4726bddSHONG Yifan 
294*d4726bddSHONG Yifan // BTreeSet<T>
295*d4726bddSHONG Yifan impl<T> Selectable for BTreeSet<T>
296*d4726bddSHONG Yifan where
297*d4726bddSHONG Yifan     T: SelectableOrderedValue,
298*d4726bddSHONG Yifan {
299*d4726bddSHONG Yifan     type ItemType = T;
300*d4726bddSHONG Yifan     type CommonType = BTreeSet<T>;
301*d4726bddSHONG Yifan     type SelectsType = BTreeSet<T>;
302*d4726bddSHONG Yifan 
is_empty(this: &Select<Self>) -> bool303*d4726bddSHONG Yifan     fn is_empty(this: &Select<Self>) -> bool {
304*d4726bddSHONG Yifan         this.common.is_empty() && this.selects.is_empty()
305*d4726bddSHONG Yifan     }
306*d4726bddSHONG Yifan 
items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>307*d4726bddSHONG Yifan     fn items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)> {
308*d4726bddSHONG Yifan         let mut result = Vec::new();
309*d4726bddSHONG Yifan         result.extend(this.common.iter().map(|value| (None, value.clone())));
310*d4726bddSHONG Yifan         result.extend(this.selects.iter().flat_map(|(configuration, values)| {
311*d4726bddSHONG Yifan             values
312*d4726bddSHONG Yifan                 .iter()
313*d4726bddSHONG Yifan                 .map(|value| (Some(configuration.clone()), value.clone()))
314*d4726bddSHONG Yifan         }));
315*d4726bddSHONG Yifan         result
316*d4726bddSHONG Yifan     }
317*d4726bddSHONG Yifan 
values(this: &Select<Self>) -> Vec<Self::ItemType>318*d4726bddSHONG Yifan     fn values(this: &Select<Self>) -> Vec<Self::ItemType> {
319*d4726bddSHONG Yifan         let mut result = Vec::new();
320*d4726bddSHONG Yifan         result.extend(this.common.iter().cloned());
321*d4726bddSHONG Yifan         result.extend(
322*d4726bddSHONG Yifan             this.selects
323*d4726bddSHONG Yifan                 .values()
324*d4726bddSHONG Yifan                 .flat_map(|values| values.iter().cloned()),
325*d4726bddSHONG Yifan         );
326*d4726bddSHONG Yifan         result
327*d4726bddSHONG Yifan     }
328*d4726bddSHONG Yifan 
insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>)329*d4726bddSHONG Yifan     fn insert(this: &mut Select<Self>, value: Self::ItemType, configuration: Option<String>) {
330*d4726bddSHONG Yifan         match configuration {
331*d4726bddSHONG Yifan             None => {
332*d4726bddSHONG Yifan                 this.selects.retain(|_, set| {
333*d4726bddSHONG Yifan                     set.remove(&value);
334*d4726bddSHONG Yifan                     !set.is_empty()
335*d4726bddSHONG Yifan                 });
336*d4726bddSHONG Yifan                 this.common.insert(value);
337*d4726bddSHONG Yifan             }
338*d4726bddSHONG Yifan             Some(configuration) => {
339*d4726bddSHONG Yifan                 if !this.common.contains(&value) {
340*d4726bddSHONG Yifan                     this.selects.entry(configuration).or_default().insert(value);
341*d4726bddSHONG Yifan                 }
342*d4726bddSHONG Yifan             }
343*d4726bddSHONG Yifan         }
344*d4726bddSHONG Yifan     }
345*d4726bddSHONG Yifan 
merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>346*d4726bddSHONG Yifan     fn merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self> {
347*d4726bddSHONG Yifan         let mut result: Select<Self> = Select::new();
348*d4726bddSHONG Yifan 
349*d4726bddSHONG Yifan         for value in lhs.common.into_iter() {
350*d4726bddSHONG Yifan             result.insert(value, None);
351*d4726bddSHONG Yifan         }
352*d4726bddSHONG Yifan         for value in rhs.common.into_iter() {
353*d4726bddSHONG Yifan             result.insert(value, None);
354*d4726bddSHONG Yifan         }
355*d4726bddSHONG Yifan 
356*d4726bddSHONG Yifan         for (configuration, values) in lhs.selects.into_iter() {
357*d4726bddSHONG Yifan             for value in values {
358*d4726bddSHONG Yifan                 result.insert(value, Some(configuration.clone()));
359*d4726bddSHONG Yifan             }
360*d4726bddSHONG Yifan         }
361*d4726bddSHONG Yifan         for (configuration, values) in rhs.selects.into_iter() {
362*d4726bddSHONG Yifan             for value in values {
363*d4726bddSHONG Yifan                 result.insert(value, Some(configuration.clone()));
364*d4726bddSHONG Yifan             }
365*d4726bddSHONG Yifan         }
366*d4726bddSHONG Yifan 
367*d4726bddSHONG Yifan         result
368*d4726bddSHONG Yifan     }
369*d4726bddSHONG Yifan }
370*d4726bddSHONG Yifan 
371*d4726bddSHONG Yifan impl<T> Select<BTreeSet<T>>
372*d4726bddSHONG Yifan where
373*d4726bddSHONG Yifan     T: SelectableOrderedValue,
374*d4726bddSHONG Yifan {
map<U, F>(self, func: F) -> Select<BTreeSet<U>> where U: SelectableOrderedValue, F: Copy + FnMut(T) -> U,375*d4726bddSHONG Yifan     pub(crate) fn map<U, F>(self, func: F) -> Select<BTreeSet<U>>
376*d4726bddSHONG Yifan     where
377*d4726bddSHONG Yifan         U: SelectableOrderedValue,
378*d4726bddSHONG Yifan         F: Copy + FnMut(T) -> U,
379*d4726bddSHONG Yifan     {
380*d4726bddSHONG Yifan         Select {
381*d4726bddSHONG Yifan             common: self.common.into_iter().map(func).collect(),
382*d4726bddSHONG Yifan             selects: self
383*d4726bddSHONG Yifan                 .selects
384*d4726bddSHONG Yifan                 .into_iter()
385*d4726bddSHONG Yifan                 .map(|(configuration, values)| {
386*d4726bddSHONG Yifan                     (configuration, values.into_iter().map(func).collect())
387*d4726bddSHONG Yifan                 })
388*d4726bddSHONG Yifan                 .collect(),
389*d4726bddSHONG Yifan         }
390*d4726bddSHONG Yifan     }
391*d4726bddSHONG Yifan }
392*d4726bddSHONG Yifan 
393*d4726bddSHONG Yifan // BTreeMap<U, T>
394*d4726bddSHONG Yifan impl<U, T> Selectable for BTreeMap<U, T>
395*d4726bddSHONG Yifan where
396*d4726bddSHONG Yifan     U: SelectableOrderedValue,
397*d4726bddSHONG Yifan     T: SelectableValue,
398*d4726bddSHONG Yifan {
399*d4726bddSHONG Yifan     type ItemType = (U, T);
400*d4726bddSHONG Yifan     type CommonType = BTreeMap<U, T>;
401*d4726bddSHONG Yifan     type SelectsType = BTreeMap<U, T>;
402*d4726bddSHONG Yifan 
is_empty(this: &Select<Self>) -> bool403*d4726bddSHONG Yifan     fn is_empty(this: &Select<Self>) -> bool {
404*d4726bddSHONG Yifan         this.common.is_empty() && this.selects.is_empty()
405*d4726bddSHONG Yifan     }
406*d4726bddSHONG Yifan 
items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)>407*d4726bddSHONG Yifan     fn items(this: &Select<Self>) -> Vec<(Option<String>, Self::ItemType)> {
408*d4726bddSHONG Yifan         let mut result = Vec::new();
409*d4726bddSHONG Yifan         result.extend(
410*d4726bddSHONG Yifan             this.common
411*d4726bddSHONG Yifan                 .iter()
412*d4726bddSHONG Yifan                 .map(|(key, value)| (None, (key.clone(), value.clone()))),
413*d4726bddSHONG Yifan         );
414*d4726bddSHONG Yifan         result.extend(this.selects.iter().flat_map(|(configuration, values)| {
415*d4726bddSHONG Yifan             values
416*d4726bddSHONG Yifan                 .iter()
417*d4726bddSHONG Yifan                 .map(|(key, value)| (Some(configuration.clone()), (key.clone(), value.clone())))
418*d4726bddSHONG Yifan         }));
419*d4726bddSHONG Yifan         result
420*d4726bddSHONG Yifan     }
421*d4726bddSHONG Yifan 
values(this: &Select<Self>) -> Vec<Self::ItemType>422*d4726bddSHONG Yifan     fn values(this: &Select<Self>) -> Vec<Self::ItemType> {
423*d4726bddSHONG Yifan         let mut result = Vec::new();
424*d4726bddSHONG Yifan         result.extend(
425*d4726bddSHONG Yifan             this.common
426*d4726bddSHONG Yifan                 .iter()
427*d4726bddSHONG Yifan                 .map(|(key, value)| (key.clone(), value.clone())),
428*d4726bddSHONG Yifan         );
429*d4726bddSHONG Yifan         result.extend(this.selects.values().flat_map(|values| {
430*d4726bddSHONG Yifan             values
431*d4726bddSHONG Yifan                 .iter()
432*d4726bddSHONG Yifan                 .map(|(key, value)| (key.clone(), value.clone()))
433*d4726bddSHONG Yifan         }));
434*d4726bddSHONG Yifan         result
435*d4726bddSHONG Yifan     }
436*d4726bddSHONG Yifan 
insert( this: &mut Select<Self>, (key, value): Self::ItemType, configuration: Option<String>, )437*d4726bddSHONG Yifan     fn insert(
438*d4726bddSHONG Yifan         this: &mut Select<Self>,
439*d4726bddSHONG Yifan         (key, value): Self::ItemType,
440*d4726bddSHONG Yifan         configuration: Option<String>,
441*d4726bddSHONG Yifan     ) {
442*d4726bddSHONG Yifan         match configuration {
443*d4726bddSHONG Yifan             None => {
444*d4726bddSHONG Yifan                 this.selects.retain(|_, map| {
445*d4726bddSHONG Yifan                     map.remove(&key);
446*d4726bddSHONG Yifan                     !map.is_empty()
447*d4726bddSHONG Yifan                 });
448*d4726bddSHONG Yifan                 this.common.insert(key, value);
449*d4726bddSHONG Yifan             }
450*d4726bddSHONG Yifan             Some(configuration) => {
451*d4726bddSHONG Yifan                 if !this.common.contains_key(&key) {
452*d4726bddSHONG Yifan                     this.selects
453*d4726bddSHONG Yifan                         .entry(configuration)
454*d4726bddSHONG Yifan                         .or_default()
455*d4726bddSHONG Yifan                         .insert(key, value);
456*d4726bddSHONG Yifan                 }
457*d4726bddSHONG Yifan             }
458*d4726bddSHONG Yifan         }
459*d4726bddSHONG Yifan     }
460*d4726bddSHONG Yifan 
merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self>461*d4726bddSHONG Yifan     fn merge(lhs: Select<Self>, rhs: Select<Self>) -> Select<Self> {
462*d4726bddSHONG Yifan         let mut result: Select<Self> = Select::new();
463*d4726bddSHONG Yifan 
464*d4726bddSHONG Yifan         for (key, value) in lhs.common.into_iter() {
465*d4726bddSHONG Yifan             result.insert((key, value), None);
466*d4726bddSHONG Yifan         }
467*d4726bddSHONG Yifan         for (key, value) in rhs.common.into_iter() {
468*d4726bddSHONG Yifan             result.insert((key, value), None);
469*d4726bddSHONG Yifan         }
470*d4726bddSHONG Yifan 
471*d4726bddSHONG Yifan         for (configuration, entries) in lhs.selects.into_iter() {
472*d4726bddSHONG Yifan             for (key, value) in entries {
473*d4726bddSHONG Yifan                 result.insert((key, value), Some(configuration.clone()));
474*d4726bddSHONG Yifan             }
475*d4726bddSHONG Yifan         }
476*d4726bddSHONG Yifan         for (configuration, entries) in rhs.selects.into_iter() {
477*d4726bddSHONG Yifan             for (key, value) in entries {
478*d4726bddSHONG Yifan                 result.insert((key, value), Some(configuration.clone()));
479*d4726bddSHONG Yifan             }
480*d4726bddSHONG Yifan         }
481*d4726bddSHONG Yifan 
482*d4726bddSHONG Yifan         result
483*d4726bddSHONG Yifan     }
484*d4726bddSHONG Yifan }
485