encre-css 0.20.1

A TailwindCSS-compatible utility-first CSS generation library written in Rust
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Define some utility functions used to detect the [CSS type](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Types) of a CSS value.
use super::shadow::ShadowList;

const LENGTH_UNITS: [&str; 16] = [
    "cm", "mm", "Q", "in", "pc", "pt", "px", "em", "ex", "ch", "rem", "lh", "vw", "vh", "vmin",
    "vmax",
];
const LINE_WIDTHS: [&str; 3] = ["thin", "medium", "thick"];
const LINE_STYLES: [&str; 10] = [
    "solid", "dashed", "dotted", "double", "groove", "ridge", "inset", "outset", "hidden", "none",
];
const ANGLES: [&str; 4] = ["deg", "grad", "rad", "turn"];
const GRADIENT_TYPES: [&str; 5] = [
    "linear-gradient",
    "radial-gradient",
    "repeating-linear-gradient",
    "repeating-radial-gradient",
    "conic-gradient",
];
const VALID_POSITIONS: [&str; 5] = ["center", "top", "right", "bottom", "left"];
const ABSOLUTE_SIZES: [&str; 8] = [
    "xx-small",
    "x-small",
    "small",
    "medium",
    "large",
    "x-large",
    "xx-large",
    "xxx-large",
];
const RELATIVE_SIZES: [&str; 2] = ["larger", "smaller"];
const NAMED_COLORS: [&str; 150] = [
    "transparent",
    "currentColor",
    "antiquewhite",
    "aliceblue",
    "aqua",
    "aquamarine",
    "azure",
    "beige",
    "bisque",
    "black",
    "blanchedalmond",
    "blue",
    "blueviolet",
    "brown",
    "burlywood",
    "cadetblue",
    "chartreuse",
    "chocolate",
    "coral",
    "cornflowerblue",
    "cornsilk",
    "crimson",
    "cyan",
    "darkblue",
    "darkcyan",
    "darkgoldenrod",
    "darkgray",
    "darkgreen",
    "darkgrey",
    "darkkhaki",
    "darkmagenta",
    "darkolivegreen",
    "darkorange",
    "darkorchid",
    "darkred",
    "darksalmon",
    "darkseagreen",
    "darkslateblue",
    "darkslategray",
    "darkslategrey",
    "darkturquoise",
    "darkviolet",
    "deeppink",
    "deepskyblue",
    "dimgray",
    "dimgrey",
    "dodgerblue",
    "firebrick",
    "floralwhite",
    "forestgreen",
    "fuchsia",
    "gainsboro",
    "ghostwhite",
    "gold",
    "goldenrod",
    "gray",
    "green",
    "greenyellow",
    "grey",
    "honeydew",
    "hotpink",
    "indianred",
    "indigo",
    "ivory",
    "khaki",
    "lavender",
    "lavenderblush",
    "lawngreen",
    "lemonchiffon",
    "lightblue",
    "lightcoral",
    "lightcyan",
    "lightgoldenrodyellow",
    "lightgray",
    "lightgreen",
    "lightgrey",
    "lightpink",
    "lightsalmon",
    "lightseagreen",
    "lightskyblue",
    "lightslategray",
    "lightslategrey",
    "lightsteelblue",
    "lightyellow",
    "lime",
    "limegreen",
    "linen",
    "magenta",
    "maroon",
    "mediumaquamarine",
    "mediumblue",
    "mediumorchid",
    "mediumpurple",
    "mediumseagreen",
    "mediumslateblue",
    "mediumspringgreen",
    "mediumturquoise",
    "mediumvioletred",
    "midnightblue",
    "mintcream",
    "mistyrose",
    "moccasin",
    "navajowhite",
    "navy",
    "oldlace",
    "olive",
    "olivedrab",
    "orange",
    "orangered",
    "orchid",
    "palegoldenrod",
    "palegreen",
    "paleturquoise",
    "palevioletred",
    "papayawhip",
    "peachpuff",
    "peru",
    "pink",
    "plum",
    "powderblue",
    "purple",
    "rebeccapurple",
    "red",
    "rosybrown",
    "royalblue",
    "saddlebrown",
    "salmon",
    "sandybrown",
    "seagreen",
    "seashell",
    "sienna",
    "silver",
    "skyblue",
    "slateblue",
    "slategray",
    "slategrey",
    "snow",
    "springgreen",
    "steelblue",
    "tan",
    "teal",
    "thistle",
    "tomato",
    "turquoise",
    "violet",
    "wheat",
    "white",
    "whitesmoke",
    "yellow",
    "yellowgreen",
];

fn is_matching_base(value: &str) -> bool {
    is_matching_var(value)
        || [
            "inherit",
            "initial",
            "revert",
            "revert-layer",
            "unset",
            "fill",
            "max-content",
            "min-content",
            "fit-content",
        ]
        .contains(&value)
}

/// Match all CSS types.
pub fn is_matching_all(_value: &str) -> bool {
    true
}

/// Returns whether the CSS value is an [`url()`](https://developer.mozilla.org/en-US/docs/Web/CSS/url).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_url;
/// assert!(is_matching_url("url('/hello/world.png')"));
/// ```
pub fn is_matching_url(value: &str) -> bool {
    value.starts_with("url(")
}

/// Returns whether the CSS value is a [`var()`](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_var;
/// assert!(is_matching_var("var(--bg-blue)"));
/// ```
pub fn is_matching_var(value: &str) -> bool {
    value.starts_with("var(")
}

/// Returns whether the CSS value is a [`shadow`](https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow#values).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_shadow;
/// assert!(is_matching_shadow("1px 2rem 10px 10px rgb(12,12,12)"));
/// ```
pub fn is_matching_shadow(value: &str) -> bool {
    ShadowList::parse(value).is_some()
}

/// Returns whether the CSS value is an [`absolute size`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-size#values).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_absolute_size;
/// assert!(is_matching_absolute_size("xx-small"));
/// ```
pub fn is_matching_absolute_size(value: &str) -> bool {
    ABSOLUTE_SIZES.contains(&value)
}

/// Returns whether the CSS value is a [`relative size`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-size#values).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_relative_size;
/// assert!(is_matching_relative_size("larger"));
/// ```
pub fn is_matching_relative_size(value: &str) -> bool {
    RELATIVE_SIZES.contains(&value)
}

/// Returns whether the CSS value is a [`line width`](https://developer.mozilla.org/en-US/docs/Web/CSS/border-width#values).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_line_width;
/// assert!(is_matching_line_width("thin"));
/// ```
pub fn is_matching_line_width(value: &str) -> bool {
    LINE_WIDTHS.contains(&value)
}

/// Returns whether the CSS value is a [`line style`](https://developer.mozilla.org/en-US/docs/Web/CSS/border-style#values).
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_line_style;
/// assert!(is_matching_line_style("solid"));
/// ```
pub fn is_matching_line_style(value: &str) -> bool {
    LINE_STYLES.contains(&value)
}

/// Returns whether the CSS value is a computational CSS function like:
///
/// - [`min()`](https://developer.mozilla.org/en-US/docs/Web/CSS/min)
/// - [`max()`](https://developer.mozilla.org/en-US/docs/Web/CSS/max)
/// - [`clamp()`](https://developer.mozilla.org/en-US/docs/Web/CSS/clamp)
/// - [`calc()`](https://developer.mozilla.org/en-US/docs/Web/CSS/calc)
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_computational_css_function;
/// assert!(is_matching_computational_css_function("min(12px,10%)"));
/// ```
pub fn is_matching_computational_css_function(value: &str) -> bool {
    value.starts_with("min(")
        || value.starts_with("max(")
        || value.starts_with("clamp(")
        || value.starts_with("calc(")
}

/// Returns whether the CSS value has the [`<color>`](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_color;
/// assert!(is_matching_color("blue"));
/// assert!(is_matching_color("#333"));
/// ```
pub fn is_matching_color(value: &str) -> bool {
    (value.starts_with('#') && (value.len() == 4 || value.len() == 7))
        || ["rgb(", "rgba(", "hsl(", "hsla(", "hwb(", "lch(", "lab("]
            .iter()
            .any(|e| value.starts_with(e))
        || NAMED_COLORS.iter().any(|c| &value == c)
        || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<length>`](https://developer.mozilla.org/en-US/docs/Web/CSS/length) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_length;
/// assert!(is_matching_length("300px"));
/// ```
pub fn is_matching_length(value: &str) -> bool {
    value.split(' ').all(|v| {
        v == "0"
            || LENGTH_UNITS.iter().any(|u| v.ends_with(u))
            || is_matching_computational_css_function(value)
    }) || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<number>`](https://developer.mozilla.org/en-US/docs/Web/CSS/number) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_number;
/// assert!(is_matching_number("42.12"));
/// ```
pub fn is_matching_number(value: &str) -> bool {
    value.parse::<f32>().is_ok()
        || is_matching_computational_css_function(value)
        || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<percentage>`](https://developer.mozilla.org/en-US/docs/Web/CSS/percentage) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_percentage;
/// assert!(is_matching_percentage("10%"));
/// ```
pub fn is_matching_percentage(value: &str) -> bool {
    value.ends_with('%') || is_matching_computational_css_function(value) || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<time>`](https://developer.mozilla.org/en-US/docs/Web/CSS/time) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_time;
/// assert!(is_matching_time("0.5s"));
/// assert!(is_matching_time("10ms"));
/// ```
pub fn is_matching_time(value: &str) -> bool {
    value.ends_with('s')
        || value.ends_with("ms")
        || is_matching_computational_css_function(value)
        || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<gradient>`](https://developer.mozilla.org/en-US/docs/Web/CSS/gradient) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_gradient;
/// assert!(is_matching_gradient("linear-gradient(45deg, blue, red);"));
/// ```
pub fn is_matching_gradient(value: &str) -> bool {
    GRADIENT_TYPES.iter().any(|t| value.starts_with(t)) || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<position>`](https://developer.mozilla.org/en-US/docs/Web/CSS/position_value) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_position;
/// assert!(is_matching_position("right"));
/// assert!(is_matching_position("12px"));
/// assert!(is_matching_position("42%"));
/// ```
pub fn is_matching_position(value: &str) -> bool {
    value
        .split(' ')
        .all(|v| VALID_POSITIONS.contains(&v) || is_matching_length(v) || is_matching_percentage(v))
        || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<angle>`](https://developer.mozilla.org/en-US/docs/Web/CSS/angle) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_angle;
/// assert!(is_matching_angle("0.2turn"));
/// ```
pub fn is_matching_angle(value: &str) -> bool {
    ANGLES.iter().any(|a| value.ends_with(a))
        || is_matching_computational_css_function(value)
        || is_matching_base(value)
}

/// Returns whether the CSS value has the [`<image>`](https://developer.mozilla.org/en-US/docs/Web/CSS/image) type.
///
/// # Example
///
/// ```
/// use encre_css::utils::value_matchers::is_matching_image;
/// assert!(is_matching_image("linear-gradient(to_right,red,orange,yellow,green,blue,indigo,violet)"));
/// ```
pub fn is_matching_image(value: &str) -> bool {
    is_matching_url(value)
        || is_matching_gradient(value)
        || ["element(", "image(", "cross-fade(", "image-set("]
            .iter()
            .any(|e| value.starts_with(e))
        || is_matching_base(value)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_matching_color_test() {
        assert!(is_matching_color("blue"));
        assert!(is_matching_color("#333"));
        assert!(is_matching_color("#121212"));
        assert!(is_matching_color("rgb(12.12,12,12)"));
        assert!(is_matching_color("rgb(12 12 12)"));
        assert!(is_matching_color("rgb(12 12 12/0.1)"));
        assert!(is_matching_color("rgb(12 12 12 / 0.1)"));
        assert!(is_matching_color("rgb(var(--blue),12,12)"));
        assert!(is_matching_color("rgb(12 12 12 / var(--opacity))"));
        assert!(is_matching_color("rgba(12,12,12,0.12)"));
        assert!(is_matching_color("hsl(360,100%,50%)"));
        assert!(is_matching_color("hsl(3.14rad,100%,50%)"));
        assert!(is_matching_color("hsl(3.14rad 100% 50%/0.42)"));
        assert!(is_matching_color("hsl(var(--hue) 12% 42%/var(--opacity))"));
        assert!(is_matching_color("hsla(360,100%,50%,0.12)"));
    }

    #[test]
    fn is_matching_length_test() {
        assert!(is_matching_length("300px"));
        assert!(!is_matching_length("50%"));
        assert!(is_matching_length("30vw"));
        assert!(is_matching_length("min(10%,10px)"));
        assert!(is_matching_length("0"));
    }

    #[test]
    fn is_matching_shadow_with_functions_test() {
        assert!(is_matching_shadow("10px 10px min(1px,2px) 10px rgb(1,1,1)"));
        assert!(is_matching_shadow("inset 0 -3em 3em rgba(0,0,0,0.1),0 0 0 2px rgb(255,255,255),0.3em 0.3em 1em rgba(0,0,0,0.3)"));
        assert!(is_matching_shadow(
            "var(--a, 0 0 1px rgb(0, 0, 0)), 0 0 1px rgb(0, 0, 0)"
        ));
    }
}