alchemy_styles/
spacedlist.rs

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