//! A space separated list of values. //! //! This type represents a list of non-unique values represented as a string of //! values separated by spaces in HTML attributes. This is rarely used; a //! SpacedSet of unique values is much more common. use std::fmt::{Debug, Display, Error, Formatter}; use std::iter::FromIterator; use std::ops::{Deref, DerefMut}; use std::str::FromStr; /// A space separated list of values. /// /// This type represents a list of non-unique values represented as a string of /// values separated by spaces in HTML attributes. This is rarely used; a /// SpacedSet of unique values is much more common. #[derive(Clone, PartialEq, Eq, Hash)] pub struct SpacedList(Vec); impl SpacedList { /// Construct an empty `SpacedList`. pub fn new() -> Self { SpacedList(Vec::new()) } } impl Default for SpacedList { fn default() -> Self { Self::new() } } impl FromIterator for SpacedList { fn from_iter(iter: I) -> Self where I: IntoIterator, { SpacedList(iter.into_iter().collect()) } } impl<'a, A: 'a + Clone> FromIterator<&'a A> for SpacedList { fn from_iter(iter: I) -> Self where I: IntoIterator, { SpacedList(iter.into_iter().cloned().collect()) } } impl<'a, A: FromStr> From<&'a str> for SpacedList where ::Err: Debug, { fn from(s: &'a str) -> Self { Self::from_iter(s.split_whitespace().map(|s| FromStr::from_str(s).unwrap())) } } impl Deref for SpacedList { type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for SpacedList { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Display for SpacedList { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { let mut it = self.0.iter().peekable(); while let Some(class) = it.next() { Display::fmt(class, f)?; if it.peek().is_some() { Display::fmt(" ", f)?; } } Ok(()) } } impl Debug for SpacedList { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { f.debug_list().entries(self.0.iter()).finish() } } impl<'a, 'b, A: FromStr> From<(&'a str, &'b str)> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list } } impl<'a, 'b, 'c, A: FromStr> From<(&'a str, &'b str, &'c str)> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list } } impl<'a, 'b, 'c, 'd, A: FromStr> From<(&'a str, &'b str, &'c str, &'d str)> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list.push(FromStr::from_str(s.3).unwrap()); list } } impl<'a, 'b, 'c, 'd, 'e, A: FromStr> From<(&'a str, &'b str, &'c str, &'d str, &'e str)> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list.push(FromStr::from_str(s.3).unwrap()); list.push(FromStr::from_str(s.4).unwrap()); list } } impl<'a, 'b, 'c, 'd, 'e, 'f, A: FromStr> From<(&'a str, &'b str, &'c str, &'d str, &'e str, &'f str)> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str, &str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list.push(FromStr::from_str(s.3).unwrap()); list.push(FromStr::from_str(s.4).unwrap()); list.push(FromStr::from_str(s.5).unwrap()); list } } impl<'a, 'b, 'c, 'd, 'e, 'f, 'g, A: FromStr> From<( &'a str, &'b str, &'c str, &'d str, &'e str, &'f str, &'g str, )> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str, &str, &str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list.push(FromStr::from_str(s.3).unwrap()); list.push(FromStr::from_str(s.4).unwrap()); list.push(FromStr::from_str(s.5).unwrap()); list.push(FromStr::from_str(s.6).unwrap()); list } } impl<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, A: FromStr> From<( &'a str, &'b str, &'c str, &'d str, &'e str, &'f str, &'g str, &'h str, )> for SpacedList where ::Err: Debug, { fn from(s: (&str, &str, &str, &str, &str, &str, &str, &str)) -> Self { let mut list = Self::new(); list.push(FromStr::from_str(s.0).unwrap()); list.push(FromStr::from_str(s.1).unwrap()); list.push(FromStr::from_str(s.2).unwrap()); list.push(FromStr::from_str(s.3).unwrap()); list.push(FromStr::from_str(s.4).unwrap()); list.push(FromStr::from_str(s.5).unwrap()); list.push(FromStr::from_str(s.6).unwrap()); list.push(FromStr::from_str(s.7).unwrap()); list } } macro_rules! spacedlist_from_array { ($num:tt) => { impl<'a, A: FromStr> From<[&'a str; $num]> for SpacedList where ::Err: Debug, { fn from(s: [&str; $num]) -> Self { Self::from_iter(s.into_iter().map(|s| FromStr::from_str(*s).unwrap())) } } }; } spacedlist_from_array!(1); spacedlist_from_array!(2); spacedlist_from_array!(3); spacedlist_from_array!(4); spacedlist_from_array!(5); spacedlist_from_array!(6); spacedlist_from_array!(7); spacedlist_from_array!(8); spacedlist_from_array!(9); spacedlist_from_array!(10); spacedlist_from_array!(11); spacedlist_from_array!(12); spacedlist_from_array!(13); spacedlist_from_array!(14); spacedlist_from_array!(15); spacedlist_from_array!(16); spacedlist_from_array!(17); spacedlist_from_array!(18); spacedlist_from_array!(19); spacedlist_from_array!(20); spacedlist_from_array!(21); spacedlist_from_array!(22); spacedlist_from_array!(23); spacedlist_from_array!(24); spacedlist_from_array!(25); spacedlist_from_array!(26); spacedlist_from_array!(27); spacedlist_from_array!(28); spacedlist_from_array!(29); spacedlist_from_array!(30); spacedlist_from_array!(31); spacedlist_from_array!(32);