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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
pub struct Declaration {
pub selector: String,
pub properties: Properties,
}
pub struct Properties(pub Vec<Property>);
pub struct Property {
pub property: String,
pub value: String,
}
pub struct Keyframes {
pub name: String,
pub keyframes: Vec<KeyframeInterval>,
}
pub struct KeyframeInterval {
pub value: String,
pub style: Properties,
}
pub struct Media {
pub condition: Vec<Property>,
pub declarations: Vec<Declaration>,
}
impl Declaration {
pub fn as_str(&self) -> String {
format!("{} {}", self.selector, self.properties.as_str())
}
}
impl Property {
pub fn as_str(&self) -> String {
format!("{}: {}", self.property, self.value)
}
}
impl Properties {
pub fn as_str(&self) -> String {
let body = self.0
.iter()
.map(|x| format!("{};", x.as_str()))
.collect::<Vec<_>>()
.join("");
format!("{{{}}}", body)
}
}
impl Keyframes {
pub fn as_str(&self) -> String {
let body = self.keyframes
.iter()
.map(|x| x.as_str())
.collect::<Vec<_>>()
.join("");
format!("@keyframes {} {{{}}}", self.name, body)
}
}
impl KeyframeInterval {
pub fn as_str(&self) -> String {
format!("{} {}", self.value, self.style.as_str())
}
}
impl Media {
pub fn as_str(&self) -> String {
let condition = self.condition
.iter()
.map(|x| format!("({})", x.as_str()))
.collect::<Vec<_>>()
.join(" and ");
let declarations = self.declarations
.iter()
.map(|x| x.as_str())
.collect::<Vec<_>>()
.join("");
format!("@media {} {{{}}}", condition, declarations)
}
}