1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::{Colors, Error, Serde, Unit};

impl Serde<Colors, String> for Colors {
    fn de(s: String) -> Result<Colors, Error> {
        if s.contains("inherit") {
            return Ok(Colors::Inherit);
        }

        let vs: Vec<&str> = s[5..s.len() - 1].split(",").collect();
        Ok(Colors::ORGB(
            vs[3].trim().parse().unwrap_or(0.0),
            vs[0].trim().parse().unwrap_or(0),
            vs[1].trim().parse().unwrap_or(0),
            vs[2].trim().parse().unwrap_or(0),
        ))
    }

    fn ser(&self) -> String {
        match self {
            Colors::ORGB(o, r, g, b) => format!("rgba({}, {}, {}, {:.1})", r, g, b, o),
            Colors::Inherit => "inherit".into(),
            _ => {
                if let Colors::ORGB(o, r, g, b) = self.to_orgb() {
                    format!("rgba({}, {}, {}, {:.1})", r, g, b, o)
                } else {
                    "rgba(255, 255, 255, 255)".into()
                }
            }
        }
    }
}

impl Serde<Unit, String> for Unit {
    fn de(s: String) -> Result<Unit, Error> {
        Ok(Unit::from_str(s))
    }

    fn ser(&self) -> String {
        self.to_string()
    }
}