alchemy_styles/
spacedset.rs

1//! A space separated set of unique values.
2//!
3//! This type represents a set of unique values represented as a string of
4//! values separated by spaces in HTML attributes.
5
6use std::collections::BTreeSet;
7use std::fmt::{Debug, Display, Error, Formatter};
8use std::iter::FromIterator;
9use std::ops::{Deref, DerefMut};
10use std::str::FromStr;
11
12/// A space separated set of unique values.
13///
14/// This type represents a set of unique values represented as a string of
15/// values separated by spaces in HTML attributes.
16#[derive(Clone, PartialEq, Eq, Hash)]
17pub struct SpacedSet<A: Ord>(pub BTreeSet<A>);
18
19impl<A: Ord> SpacedSet<A> {
20    /// Construct an empty `SpacedSet`.
21    pub fn new() -> Self {
22        SpacedSet(BTreeSet::new())
23    }
24
25    /// Add a value to the `SpacedSet`.
26    pub fn add<T: Into<A>>(&mut self, value: T) -> bool {
27        self.0.insert(value.into())
28    }
29}
30
31impl<A: Ord> Default for SpacedSet<A> {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl<A: Ord> FromIterator<A> for SpacedSet<A> {
38    fn from_iter<I>(iter: I) -> Self
39    where
40        I: IntoIterator<Item = A>,
41    {
42        SpacedSet(iter.into_iter().collect())
43    }
44}
45
46impl<'a, A: 'a + Ord + Clone> FromIterator<&'a A> for SpacedSet<A> {
47    fn from_iter<I>(iter: I) -> Self
48    where
49        I: IntoIterator<Item = &'a A>,
50    {
51        SpacedSet(iter.into_iter().cloned().collect())
52    }
53}
54
55impl<'a, A: Ord + FromStr> FromStr for SpacedSet<A>
56where
57    <A as FromStr>::Err: Debug,
58{
59    type Err = <A as FromStr>::Err;
60
61    fn from_str(s: &str) -> Result<Self, Self::Err> {
62        let result: Result<Vec<A>, Self::Err> =
63            s.split_whitespace().map(|s| FromStr::from_str(s)).collect();
64        result.map(Self::from_iter)
65    }
66}
67
68impl<'a, A: Ord + FromStr> From<&'a str> for SpacedSet<A>
69where
70    <A as FromStr>::Err: Debug,
71{
72    fn from(s: &'a str) -> Self {
73        Self::from_iter(s.split_whitespace().map(|s| FromStr::from_str(s).unwrap()))
74    }
75}
76
77impl<A: Ord> Deref for SpacedSet<A> {
78    type Target = BTreeSet<A>;
79    fn deref(&self) -> &Self::Target {
80        &self.0
81    }
82}
83
84impl<A: Ord> DerefMut for SpacedSet<A> {
85    fn deref_mut(&mut self) -> &mut Self::Target {
86        &mut self.0
87    }
88}
89
90impl<A: Ord + Display> Display for SpacedSet<A> {
91    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
92        let mut it = self.0.iter().peekable();
93        while let Some(class) = it.next() {
94            Display::fmt(class, f)?;
95            if it.peek().is_some() {
96                Display::fmt(" ", f)?;
97            }
98        }
99        Ok(())
100    }
101}
102
103impl<A: Ord + Debug> Debug for SpacedSet<A> {
104    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
105        f.debug_list().entries(self.0.iter()).finish()
106    }
107}
108
109impl<'a, A: Ord + FromStr> From<Vec<&'a str>> for SpacedSet<A>
110where
111    <A as FromStr>::Err: Debug,
112{
113    fn from(s: Vec<&'a str>) -> Self {
114        let mut list = Self::new();
115
116        for key in s {
117            list.insert(FromStr::from_str(key).unwrap());
118        }
119        
120        list
121    }
122}
123
124impl<'a, A: Ord + FromStr> From<&Vec<&'a str>> for SpacedSet<A>
125where
126    <A as FromStr>::Err: Debug,
127{
128    fn from(s: &Vec<&'a str>) -> Self {
129        let mut list = Self::new();
130
131        for key in s {
132            list.insert(FromStr::from_str(key).unwrap());
133        }
134        
135        list
136    }
137}
138
139impl<'a, 'b, A: Ord + FromStr> From<(&'a str, &'b str)> for SpacedSet<A>
140where
141    <A as FromStr>::Err: Debug,
142{
143    fn from(s: (&str, &str)) -> Self {
144        let mut list = Self::new();
145        list.insert(FromStr::from_str(s.0).unwrap());
146        list.insert(FromStr::from_str(s.1).unwrap());
147        list
148    }
149}
150
151impl<'a, 'b, 'c, A: Ord + FromStr> From<(&'a str, &'b str, &'c str)> for SpacedSet<A>
152where
153    <A as FromStr>::Err: Debug,
154{
155    fn from(s: (&str, &str, &str)) -> Self {
156        let mut list = Self::new();
157        list.insert(FromStr::from_str(s.0).unwrap());
158        list.insert(FromStr::from_str(s.1).unwrap());
159        list.insert(FromStr::from_str(s.2).unwrap());
160        list
161    }
162}
163
164impl<'a, 'b, 'c, 'd, A: Ord + FromStr> From<(&'a str, &'b str, &'c str, &'d str)> for SpacedSet<A>
165where
166    <A as FromStr>::Err: Debug,
167{
168    fn from(s: (&str, &str, &str, &str)) -> Self {
169        let mut list = Self::new();
170        list.insert(FromStr::from_str(s.0).unwrap());
171        list.insert(FromStr::from_str(s.1).unwrap());
172        list.insert(FromStr::from_str(s.2).unwrap());
173        list.insert(FromStr::from_str(s.3).unwrap());
174        list
175    }
176}
177
178impl<'a, 'b, 'c, 'd, 'e, A: Ord + FromStr> From<(&'a str, &'b str, &'c str, &'d str, &'e str)>
179    for SpacedSet<A>
180where
181    <A as FromStr>::Err: Debug,
182{
183    fn from(s: (&str, &str, &str, &str, &str)) -> Self {
184        let mut list = Self::new();
185        list.insert(FromStr::from_str(s.0).unwrap());
186        list.insert(FromStr::from_str(s.1).unwrap());
187        list.insert(FromStr::from_str(s.2).unwrap());
188        list.insert(FromStr::from_str(s.3).unwrap());
189        list.insert(FromStr::from_str(s.4).unwrap());
190        list
191    }
192}
193
194impl<'a, 'b, 'c, 'd, 'e, 'f, A: Ord + FromStr>
195    From<(&'a str, &'b str, &'c str, &'d str, &'e str, &'f str)> for SpacedSet<A>
196where
197    <A as FromStr>::Err: Debug,
198{
199    fn from(s: (&str, &str, &str, &str, &str, &str)) -> Self {
200        let mut list = Self::new();
201        list.insert(FromStr::from_str(s.0).unwrap());
202        list.insert(FromStr::from_str(s.1).unwrap());
203        list.insert(FromStr::from_str(s.2).unwrap());
204        list.insert(FromStr::from_str(s.3).unwrap());
205        list.insert(FromStr::from_str(s.4).unwrap());
206        list.insert(FromStr::from_str(s.5).unwrap());
207        list
208    }
209}
210
211impl<'a, 'b, 'c, 'd, 'e, 'f, 'g, A: Ord + FromStr>
212    From<(
213        &'a str,
214        &'b str,
215        &'c str,
216        &'d str,
217        &'e str,
218        &'f str,
219        &'g str,
220    )> for SpacedSet<A>
221where
222    <A as FromStr>::Err: Debug,
223{
224    fn from(s: (&str, &str, &str, &str, &str, &str, &str)) -> Self {
225        let mut list = Self::new();
226        list.insert(FromStr::from_str(s.0).unwrap());
227        list.insert(FromStr::from_str(s.1).unwrap());
228        list.insert(FromStr::from_str(s.2).unwrap());
229        list.insert(FromStr::from_str(s.3).unwrap());
230        list.insert(FromStr::from_str(s.4).unwrap());
231        list.insert(FromStr::from_str(s.5).unwrap());
232        list.insert(FromStr::from_str(s.6).unwrap());
233        list
234    }
235}
236
237impl<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, A: Ord + FromStr>
238    From<(
239        &'a str,
240        &'b str,
241        &'c str,
242        &'d str,
243        &'e str,
244        &'f str,
245        &'g str,
246        &'h str,
247    )> for SpacedSet<A>
248where
249    <A as FromStr>::Err: Debug,
250{
251    fn from(s: (&str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
252        let mut list = Self::new();
253        list.insert(FromStr::from_str(s.0).unwrap());
254        list.insert(FromStr::from_str(s.1).unwrap());
255        list.insert(FromStr::from_str(s.2).unwrap());
256        list.insert(FromStr::from_str(s.3).unwrap());
257        list.insert(FromStr::from_str(s.4).unwrap());
258        list.insert(FromStr::from_str(s.5).unwrap());
259        list.insert(FromStr::from_str(s.6).unwrap());
260        list.insert(FromStr::from_str(s.7).unwrap());
261        list
262    }
263}
264
265macro_rules! spacedlist_from_array {
266    ($num:tt) => {
267        impl<'a, A: Ord + FromStr> From<[&'a str; $num]> for SpacedSet<A>
268        where
269            <A as FromStr>::Err: Debug,
270        {
271            fn from(s: [&str; $num]) -> Self {
272                Self::from_iter(s.into_iter().map(|s| FromStr::from_str(*s).unwrap()))
273            }
274        }
275    };
276}
277spacedlist_from_array!(1);
278spacedlist_from_array!(2);
279spacedlist_from_array!(3);
280spacedlist_from_array!(4);
281spacedlist_from_array!(5);
282spacedlist_from_array!(6);
283spacedlist_from_array!(7);
284spacedlist_from_array!(8);
285spacedlist_from_array!(9);
286spacedlist_from_array!(10);
287spacedlist_from_array!(11);
288spacedlist_from_array!(12);
289spacedlist_from_array!(13);
290spacedlist_from_array!(14);
291spacedlist_from_array!(15);
292spacedlist_from_array!(16);
293spacedlist_from_array!(17);
294spacedlist_from_array!(18);
295spacedlist_from_array!(19);
296spacedlist_from_array!(20);
297spacedlist_from_array!(21);
298spacedlist_from_array!(22);
299spacedlist_from_array!(23);
300spacedlist_from_array!(24);
301spacedlist_from_array!(25);
302spacedlist_from_array!(26);
303spacedlist_from_array!(27);
304spacedlist_from_array!(28);
305spacedlist_from_array!(29);
306spacedlist_from_array!(30);
307spacedlist_from_array!(31);
308spacedlist_from_array!(32);