html_types/semantic/
class.rs

1use crate::attributes::Value;
2use std::convert::TryFrom;
3
4pub struct Class(String);
5
6#[derive(Debug)]
7pub struct InvalidClass(String);
8
9impl Class {
10    pub fn create<S>(class: S) -> Result<Class, InvalidClass>
11    where
12        S: Into<String>,
13    {
14        let string = class.into();
15        let is_value = Value::is_valid(&string);
16        if is_value {
17            Ok(Class(string))
18        } else {
19            Err(InvalidClass(string))
20        }
21    }
22}
23
24pub struct EmptyVector;
25impl<'a> TryFrom<Vec<Class>> for Value<'a> {
26    type Error = EmptyVector;
27    fn try_from(value: Vec<Class>) -> Result<Self, Self::Error> {
28        match value.len() {
29            0 => Err(EmptyVector),
30            _ => {
31                let whitespace = " ";
32                let to_str = |x: Class| -> String { x.0 };
33                let strings = value
34                    .into_iter()
35                    .map(to_str)
36                    .collect::<Vec<String>>()
37                    .join(whitespace);
38                // If all classes are valid values, and the seperator is allowed
39                // then joining them should never panics
40                let value = Value::owned(strings).unwrap();
41                Ok(value)
42            }
43        }
44    }
45}