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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::{bumpalo, Align, Background, Color, Length};
use std::collections::BTreeMap;
#[derive(Debug)]
pub enum Rule {
Column,
Row,
Padding(u16),
Spacing(u16),
}
impl Rule {
pub fn class<'a>(&self) -> String {
match self {
Rule::Column => String::from("c"),
Rule::Row => String::from("r"),
Rule::Padding(padding) => format!("p-{}", padding),
Rule::Spacing(spacing) => format!("s-{}", spacing),
}
}
pub fn declaration<'a>(&self, bump: &'a bumpalo::Bump) -> &'a str {
let class = self.class();
match self {
Rule::Column => {
let body = "{ display: flex; flex-direction: column; }";
bumpalo::format!(in bump, ".{} {}", class, body).into_bump_str()
}
Rule::Row => {
let body = "{ display: flex; flex-direction: row; }";
bumpalo::format!(in bump, ".{} {}", class, body).into_bump_str()
}
Rule::Padding(padding) => bumpalo::format!(
in bump,
".{} {{ box-sizing: border-box; padding: {}px }}",
class,
padding
)
.into_bump_str(),
Rule::Spacing(spacing) => bumpalo::format!(
in bump,
".c.{} > * {{ margin-bottom: {}px }} \
.r.{} > * {{ margin-right: {}px }} \
.c.{} > *:last-child {{ margin-bottom: 0 }} \
.r.{} > *:last-child {{ margin-right: 0 }}",
class,
spacing,
class,
spacing,
class,
class
)
.into_bump_str(),
}
}
}
#[derive(Debug)]
pub struct Css<'a> {
rules: BTreeMap<String, &'a str>,
}
impl<'a> Css<'a> {
pub fn new() -> Self {
Css {
rules: BTreeMap::new(),
}
}
pub fn insert(&mut self, bump: &'a bumpalo::Bump, rule: Rule) -> String {
let class = rule.class();
if !self.rules.contains_key(&class) {
let _ = self.rules.insert(class.clone(), rule.declaration(bump));
}
class
}
pub fn node(self, bump: &'a bumpalo::Bump) -> dodrio::Node<'a> {
use dodrio::builder::*;
let mut declarations = bumpalo::collections::Vec::new_in(bump);
declarations.push(text("html { height: 100% }"));
declarations.push(text(
"body { height: 100%; margin: 0; padding: 0; font-family: sans-serif }",
));
declarations.push(text("* { margin: 0; padding: 0 }"));
declarations.push(text(
"button { border: none; cursor: pointer; outline: none }",
));
for declaration in self.rules.values() {
declarations.push(text(*declaration));
}
style(bump).children(declarations).finish()
}
}
pub fn length(length: Length) -> String {
match length {
Length::Shrink => String::from("auto"),
Length::Units(px) => format!("{}px", px),
Length::Fill | Length::FillPortion(_) => String::from("100%"),
}
}
pub fn max_length(units: u32) -> String {
use std::u32;
if units == u32::MAX {
String::from("initial")
} else {
format!("{}px", units)
}
}
pub fn min_length(units: u32) -> String {
if units == 0 {
String::from("initial")
} else {
format!("{}px", units)
}
}
pub fn color(Color { r, g, b, a }: Color) -> String {
format!("rgba({}, {}, {}, {})", 255.0 * r, 255.0 * g, 255.0 * b, a)
}
pub fn background(background: Background) -> String {
match background {
Background::Color(c) => color(c),
}
}
pub fn align(align: Align) -> &'static str {
match align {
Align::Start => "flex-start",
Align::Center => "center",
Align::End => "flex-end",
}
}