1
//! Rust source-code emitter for parsed CSS.
2
//!
3
//! Produces a `const CSS: Css = ...;` literal plus a minimal `Cargo.toml` and
4
//! `src/main.rs` skeleton suitable for `cargo build` against `azul`.
5

            
6
use alloc::{
7
    format,
8
    string::{String, ToString},
9
    vec,
10
    vec::Vec,
11
};
12
use core::fmt::Write;
13

            
14
use super::{CodegenBackend, GeneratedFile};
15
use crate::{
16
    css::{
17
        AttributeMatchOp, Css, CssAttributeSelector, CssDeclaration, CssNthChildPattern,
18
        CssNthChildSelector, CssPath, CssPathPseudoSelector, CssPathSelector, DynamicCssProperty,
19
        NodeTypeTag,
20
    },
21
    props::property::format_static_css_prop,
22
};
23

            
24
/// Emits Rust source code for a parsed CSS stylesheet.
25
#[derive(Copy, Clone, Debug)]
26
pub struct RustBackend;
27

            
28
impl CodegenBackend for RustBackend {
29
    fn lang(&self) -> &'static str {
30
        "rust"
31
    }
32

            
33
1
    fn emit_css(&self, css: &Css) -> String {
34
1
        css_to_rust_code(css)
35
1
    }
36

            
37
1
    fn emit_project(&self, css: &Css) -> Vec<GeneratedFile> {
38
1
        let css_literal = css_to_rust_code(css);
39
1
        let main_rs = format!(
40
1
            "use azul::prelude::*;\r\n\r\n{css_literal}\r\n\r\nfn main() {{\r\n    \
41
1
             println!(\"Generated stylesheet contains {{}} rule(s)\", \
42
1
             CSS.rules.as_ref().len());\r\n}}\r\n",
43
        );
44
1
        let cargo_toml = "[package]\r\nname = \"azul-generated-app\"\r\nversion = \
45
1
                          \"0.1.0\"\r\nedition = \"2021\"\r\n\r\n[dependencies]\r\nazul = \
46
1
                          \"0.0.7\"\r\n"
47
1
            .to_string();
48
1
        vec![
49
1
            GeneratedFile {
50
1
                path: "Cargo.toml".to_string(),
51
1
                contents: cargo_toml,
52
1
            },
53
1
            GeneratedFile {
54
1
                path: "src/main.rs".to_string(),
55
1
                contents: main_rs,
56
1
            },
57
        ]
58
1
    }
59
}
60

            
61
/// Render a parsed [`Css`] as Rust source code (a `const CSS: Css = ...;`).
62
#[must_use]
63
2
pub fn css_to_rust_code(css: &Css) -> String {
64
2
    let mut output = String::new();
65

            
66
2
    output.push_str("const CSS: Css = Css {\r\n");
67
2
    output.push_str("\trules: [\r\n");
68

            
69
4
    for block in &css.rules {
70
2
        output.push_str("\t\tCssRuleBlock {\r\n");
71
2
        let _ = write!(
72
2
            output,
73
2
            "\t\t\tpath: {},\r\n",
74
2
            print_block_path(&block.path, 3)
75
        );
76
2
        let _ = write!(output, "\t\t\tpriority: {},\r\n", block.priority,);
77

            
78
2
        output.push_str("\t\t\tdeclarations: [\r\n");
79
2
        for declaration in &block.declarations {
80
            let _ = write!(output, "\t\t\t\t{},\r\n", print_declaration(declaration, 4));
81
        }
82
2
        output.push_str("\t\t\t]\r\n");
83

            
84
2
        output.push_str("\t\t},\r\n");
85
    }
86

            
87
2
    output.push_str("\t]\r\n");
88
2
    output.push_str("};");
89

            
90
2
    output.replace('\t', "    ")
91
2
}
92

            
93
#[allow(clippy::too_many_lines)]
94
// large but cohesive: single-purpose CSS parser/formatter/dispatch table (one branch per
95
// property/variant)
96
#[must_use]
97
2
pub const fn format_node_type(n: &NodeTypeTag) -> &'static str {
98
2
    match n {
99
        // Document structure
100
        NodeTypeTag::Html => "NodeTypeTag::Html",
101
        NodeTypeTag::Head => "NodeTypeTag::Head",
102
        NodeTypeTag::Body => "NodeTypeTag::Body",
103

            
104
        // Block elements
105
2
        NodeTypeTag::Div => "NodeTypeTag::Div",
106
        NodeTypeTag::P => "NodeTypeTag::P",
107
        NodeTypeTag::Article => "NodeTypeTag::Article",
108
        NodeTypeTag::Section => "NodeTypeTag::Section",
109
        NodeTypeTag::Nav => "NodeTypeTag::Nav",
110
        NodeTypeTag::Aside => "NodeTypeTag::Aside",
111
        NodeTypeTag::Header => "NodeTypeTag::Header",
112
        NodeTypeTag::Footer => "NodeTypeTag::Footer",
113
        NodeTypeTag::Main => "NodeTypeTag::Main",
114
        NodeTypeTag::Figure => "NodeTypeTag::Figure",
115
        NodeTypeTag::FigCaption => "NodeTypeTag::FigCaption",
116

            
117
        // Headings
118
        NodeTypeTag::H1 => "NodeTypeTag::H1",
119
        NodeTypeTag::H2 => "NodeTypeTag::H2",
120
        NodeTypeTag::H3 => "NodeTypeTag::H3",
121
        NodeTypeTag::H4 => "NodeTypeTag::H4",
122
        NodeTypeTag::H5 => "NodeTypeTag::H5",
123
        NodeTypeTag::H6 => "NodeTypeTag::H6",
124

            
125
        // Text formatting
126
        NodeTypeTag::Br => "NodeTypeTag::Br",
127
        NodeTypeTag::Hr => "NodeTypeTag::Hr",
128
        NodeTypeTag::Pre => "NodeTypeTag::Pre",
129
        NodeTypeTag::BlockQuote => "NodeTypeTag::BlockQuote",
130
        NodeTypeTag::Address => "NodeTypeTag::Address",
131
        NodeTypeTag::Details => "NodeTypeTag::Details",
132
        NodeTypeTag::Summary => "NodeTypeTag::Summary",
133
        NodeTypeTag::Dialog => "NodeTypeTag::Dialog",
134

            
135
        // List elements
136
        NodeTypeTag::Ul => "NodeTypeTag::Ul",
137
        NodeTypeTag::Ol => "NodeTypeTag::Ol",
138
        NodeTypeTag::Li => "NodeTypeTag::Li",
139
        NodeTypeTag::Dl => "NodeTypeTag::Dl",
140
        NodeTypeTag::Dt => "NodeTypeTag::Dt",
141
        NodeTypeTag::Dd => "NodeTypeTag::Dd",
142
        NodeTypeTag::Menu => "NodeTypeTag::Menu",
143
        NodeTypeTag::MenuItem => "NodeTypeTag::MenuItem",
144
        NodeTypeTag::Dir => "NodeTypeTag::Dir",
145

            
146
        // Table elements
147
        NodeTypeTag::Table => "NodeTypeTag::Table",
148
        NodeTypeTag::Caption => "NodeTypeTag::Caption",
149
        NodeTypeTag::THead => "NodeTypeTag::THead",
150
        NodeTypeTag::TBody => "NodeTypeTag::TBody",
151
        NodeTypeTag::TFoot => "NodeTypeTag::TFoot",
152
        NodeTypeTag::Tr => "NodeTypeTag::Tr",
153
        NodeTypeTag::Th => "NodeTypeTag::Th",
154
        NodeTypeTag::Td => "NodeTypeTag::Td",
155
        NodeTypeTag::ColGroup => "NodeTypeTag::ColGroup",
156
        NodeTypeTag::Col => "NodeTypeTag::Col",
157

            
158
        // Form elements
159
        NodeTypeTag::Form => "NodeTypeTag::Form",
160
        NodeTypeTag::FieldSet => "NodeTypeTag::FieldSet",
161
        NodeTypeTag::Legend => "NodeTypeTag::Legend",
162
        NodeTypeTag::Label => "NodeTypeTag::Label",
163
        NodeTypeTag::Input => "NodeTypeTag::Input",
164
        NodeTypeTag::Button => "NodeTypeTag::Button",
165
        NodeTypeTag::Select => "NodeTypeTag::Select",
166
        NodeTypeTag::OptGroup => "NodeTypeTag::OptGroup",
167
        NodeTypeTag::SelectOption => "NodeTypeTag::SelectOption",
168
        NodeTypeTag::TextArea => "NodeTypeTag::TextArea",
169
        NodeTypeTag::Output => "NodeTypeTag::Output",
170
        NodeTypeTag::Progress => "NodeTypeTag::Progress",
171
        NodeTypeTag::Meter => "NodeTypeTag::Meter",
172
        NodeTypeTag::DataList => "NodeTypeTag::DataList",
173

            
174
        // Inline elements
175
        NodeTypeTag::Span => "NodeTypeTag::Span",
176
        NodeTypeTag::A => "NodeTypeTag::A",
177
        NodeTypeTag::Em => "NodeTypeTag::Em",
178
        NodeTypeTag::Strong => "NodeTypeTag::Strong",
179
        NodeTypeTag::B => "NodeTypeTag::B",
180
        NodeTypeTag::I => "NodeTypeTag::I",
181
        NodeTypeTag::U => "NodeTypeTag::U",
182
        NodeTypeTag::S => "NodeTypeTag::S",
183
        NodeTypeTag::Mark => "NodeTypeTag::Mark",
184
        NodeTypeTag::Del => "NodeTypeTag::Del",
185
        NodeTypeTag::Ins => "NodeTypeTag::Ins",
186
        NodeTypeTag::Code => "NodeTypeTag::Code",
187
        NodeTypeTag::Samp => "NodeTypeTag::Samp",
188
        NodeTypeTag::Kbd => "NodeTypeTag::Kbd",
189
        NodeTypeTag::Var => "NodeTypeTag::Var",
190
        NodeTypeTag::Cite => "NodeTypeTag::Cite",
191
        NodeTypeTag::Dfn => "NodeTypeTag::Dfn",
192
        NodeTypeTag::Abbr => "NodeTypeTag::Abbr",
193
        NodeTypeTag::Acronym => "NodeTypeTag::Acronym",
194
        NodeTypeTag::Q => "NodeTypeTag::Q",
195
        NodeTypeTag::Time => "NodeTypeTag::Time",
196
        NodeTypeTag::Sub => "NodeTypeTag::Sub",
197
        NodeTypeTag::Sup => "NodeTypeTag::Sup",
198
        NodeTypeTag::Small => "NodeTypeTag::Small",
199
        NodeTypeTag::Big => "NodeTypeTag::Big",
200
        NodeTypeTag::Bdo => "NodeTypeTag::Bdo",
201
        NodeTypeTag::Bdi => "NodeTypeTag::Bdi",
202
        NodeTypeTag::Wbr => "NodeTypeTag::Wbr",
203
        NodeTypeTag::Ruby => "NodeTypeTag::Ruby",
204
        NodeTypeTag::Rt => "NodeTypeTag::Rt",
205
        NodeTypeTag::Rtc => "NodeTypeTag::Rtc",
206
        NodeTypeTag::Rp => "NodeTypeTag::Rp",
207
        NodeTypeTag::Data => "NodeTypeTag::Data",
208

            
209
        // Embedded content
210
        NodeTypeTag::Canvas => "NodeTypeTag::Canvas",
211
        NodeTypeTag::Object => "NodeTypeTag::Object",
212
        NodeTypeTag::Param => "NodeTypeTag::Param",
213
        NodeTypeTag::Embed => "NodeTypeTag::Embed",
214
        NodeTypeTag::Audio => "NodeTypeTag::Audio",
215
        NodeTypeTag::Video => "NodeTypeTag::Video",
216
        NodeTypeTag::Source => "NodeTypeTag::Source",
217
        NodeTypeTag::Track => "NodeTypeTag::Track",
218
        NodeTypeTag::Map => "NodeTypeTag::Map",
219
        NodeTypeTag::Area => "NodeTypeTag::Area",
220
        NodeTypeTag::Svg => "NodeTypeTag::Svg",
221
        NodeTypeTag::SvgPath => "NodeTypeTag::SvgPath",
222
        NodeTypeTag::SvgCircle => "NodeTypeTag::SvgCircle",
223
        NodeTypeTag::SvgRect => "NodeTypeTag::SvgRect",
224
        NodeTypeTag::SvgEllipse => "NodeTypeTag::SvgEllipse",
225
        NodeTypeTag::SvgLine => "NodeTypeTag::SvgLine",
226
        NodeTypeTag::SvgPolygon => "NodeTypeTag::SvgPolygon",
227
        NodeTypeTag::SvgPolyline => "NodeTypeTag::SvgPolyline",
228
        NodeTypeTag::SvgG => "NodeTypeTag::SvgG",
229

            
230
        // SVG container elements
231
        NodeTypeTag::SvgDefs => "NodeTypeTag::SvgDefs",
232
        NodeTypeTag::SvgSymbol => "NodeTypeTag::SvgSymbol",
233
        NodeTypeTag::SvgUse => "NodeTypeTag::SvgUse",
234
        NodeTypeTag::SvgSwitch => "NodeTypeTag::SvgSwitch",
235

            
236
        // SVG text elements
237
        NodeTypeTag::SvgText => "NodeTypeTag::SvgText",
238
        NodeTypeTag::SvgTspan => "NodeTypeTag::SvgTspan",
239
        NodeTypeTag::SvgTextPath => "NodeTypeTag::SvgTextPath",
240

            
241
        // SVG paint server elements
242
        NodeTypeTag::SvgLinearGradient => "NodeTypeTag::SvgLinearGradient",
243
        NodeTypeTag::SvgRadialGradient => "NodeTypeTag::SvgRadialGradient",
244
        NodeTypeTag::SvgStop => "NodeTypeTag::SvgStop",
245
        NodeTypeTag::SvgPattern => "NodeTypeTag::SvgPattern",
246

            
247
        // SVG clipping/masking elements
248
        NodeTypeTag::SvgClipPathElement => "NodeTypeTag::SvgClipPathElement",
249
        NodeTypeTag::SvgMask => "NodeTypeTag::SvgMask",
250

            
251
        // SVG filter elements
252
        NodeTypeTag::SvgFilter => "NodeTypeTag::SvgFilter",
253
        NodeTypeTag::SvgFeBlend => "NodeTypeTag::SvgFeBlend",
254
        NodeTypeTag::SvgFeColorMatrix => "NodeTypeTag::SvgFeColorMatrix",
255
        NodeTypeTag::SvgFeComponentTransfer => "NodeTypeTag::SvgFeComponentTransfer",
256
        NodeTypeTag::SvgFeComposite => "NodeTypeTag::SvgFeComposite",
257
        NodeTypeTag::SvgFeConvolveMatrix => "NodeTypeTag::SvgFeConvolveMatrix",
258
        NodeTypeTag::SvgFeDiffuseLighting => "NodeTypeTag::SvgFeDiffuseLighting",
259
        NodeTypeTag::SvgFeDisplacementMap => "NodeTypeTag::SvgFeDisplacementMap",
260
        NodeTypeTag::SvgFeDistantLight => "NodeTypeTag::SvgFeDistantLight",
261
        NodeTypeTag::SvgFeDropShadow => "NodeTypeTag::SvgFeDropShadow",
262
        NodeTypeTag::SvgFeFlood => "NodeTypeTag::SvgFeFlood",
263
        NodeTypeTag::SvgFeFuncR => "NodeTypeTag::SvgFeFuncR",
264
        NodeTypeTag::SvgFeFuncG => "NodeTypeTag::SvgFeFuncG",
265
        NodeTypeTag::SvgFeFuncB => "NodeTypeTag::SvgFeFuncB",
266
        NodeTypeTag::SvgFeFuncA => "NodeTypeTag::SvgFeFuncA",
267
        NodeTypeTag::SvgFeGaussianBlur => "NodeTypeTag::SvgFeGaussianBlur",
268
        NodeTypeTag::SvgFeImage => "NodeTypeTag::SvgFeImage",
269
        NodeTypeTag::SvgFeMerge => "NodeTypeTag::SvgFeMerge",
270
        NodeTypeTag::SvgFeMergeNode => "NodeTypeTag::SvgFeMergeNode",
271
        NodeTypeTag::SvgFeMorphology => "NodeTypeTag::SvgFeMorphology",
272
        NodeTypeTag::SvgFeOffset => "NodeTypeTag::SvgFeOffset",
273
        NodeTypeTag::SvgFePointLight => "NodeTypeTag::SvgFePointLight",
274
        NodeTypeTag::SvgFeSpecularLighting => "NodeTypeTag::SvgFeSpecularLighting",
275
        NodeTypeTag::SvgFeSpotLight => "NodeTypeTag::SvgFeSpotLight",
276
        NodeTypeTag::SvgFeTile => "NodeTypeTag::SvgFeTile",
277
        NodeTypeTag::SvgFeTurbulence => "NodeTypeTag::SvgFeTurbulence",
278

            
279
        // SVG marker/image elements
280
        NodeTypeTag::SvgMarker => "NodeTypeTag::SvgMarker",
281
        NodeTypeTag::SvgImage => "NodeTypeTag::SvgImage",
282
        NodeTypeTag::SvgForeignObject => "NodeTypeTag::SvgForeignObject",
283

            
284
        // SVG descriptive elements
285
        NodeTypeTag::SvgTitle => "NodeTypeTag::SvgTitle",
286
        NodeTypeTag::SvgDesc => "NodeTypeTag::SvgDesc",
287
        NodeTypeTag::SvgMetadata => "NodeTypeTag::SvgMetadata",
288
        NodeTypeTag::SvgA => "NodeTypeTag::SvgA",
289
        NodeTypeTag::SvgView => "NodeTypeTag::SvgView",
290
        NodeTypeTag::SvgStyle => "NodeTypeTag::SvgStyle",
291
        NodeTypeTag::SvgScript => "NodeTypeTag::SvgScript",
292

            
293
        // SVG animation elements
294
        NodeTypeTag::SvgAnimate => "NodeTypeTag::SvgAnimate",
295
        NodeTypeTag::SvgAnimateMotion => "NodeTypeTag::SvgAnimateMotion",
296
        NodeTypeTag::SvgAnimateTransform => "NodeTypeTag::SvgAnimateTransform",
297
        NodeTypeTag::SvgSet => "NodeTypeTag::SvgSet",
298
        NodeTypeTag::SvgMpath => "NodeTypeTag::SvgMpath",
299

            
300
        // Metadata
301
        NodeTypeTag::Title => "NodeTypeTag::Title",
302
        NodeTypeTag::Meta => "NodeTypeTag::Meta",
303
        NodeTypeTag::Link => "NodeTypeTag::Link",
304
        NodeTypeTag::Script => "NodeTypeTag::Script",
305
        NodeTypeTag::Style => "NodeTypeTag::Style",
306
        NodeTypeTag::Base => "NodeTypeTag::Base",
307

            
308
        // Content elements
309
        NodeTypeTag::Text => "NodeTypeTag::Text",
310
        NodeTypeTag::Img => "NodeTypeTag::Img",
311
        NodeTypeTag::VirtualView => "NodeTypeTag::VirtualView",
312
        NodeTypeTag::TransientWindow => "NodeTypeTag::TransientWindow",
313
        NodeTypeTag::Icon => "NodeTypeTag::Icon",
314
        NodeTypeTag::GeolocationProbe => "NodeTypeTag::GeolocationProbe",
315
        NodeTypeTag::PageBreak => "NodeTypeTag::PageBreak",
316

            
317
        // Pseudo-elements
318
        NodeTypeTag::Before => "NodeTypeTag::Before",
319
        NodeTypeTag::After => "NodeTypeTag::After",
320
        NodeTypeTag::Marker => "NodeTypeTag::Marker",
321
        NodeTypeTag::Placeholder => "NodeTypeTag::Placeholder",
322
    }
323
2
}
324

            
325
#[must_use]
326
2
pub fn print_block_path(path: &CssPath, tabs: usize) -> String {
327
2
    let t = String::from("    ").repeat(tabs);
328
2
    let t1 = String::from("    ").repeat(tabs + 1);
329

            
330
2
    format!(
331
2
        "CssPath {{\r\n{}selectors: {}\r\n{}}}",
332
        t1,
333
2
        format_selectors(path.selectors.as_ref(), tabs + 1),
334
        t
335
    )
336
2
}
337

            
338
#[must_use]
339
2
pub fn format_selectors(selectors: &[CssPathSelector], tabs: usize) -> String {
340
2
    let t = String::from("    ").repeat(tabs);
341
2
    let t1 = String::from("    ").repeat(tabs + 1);
342

            
343
2
    let selectors_formatted = selectors
344
2
        .iter()
345
2
        .map(|s| format!("{}{},", t1, format_single_selector(s, tabs + 1)))
346
2
        .collect::<Vec<String>>()
347
2
        .join("\r\n");
348

            
349
2
    format!("vec![\r\n{selectors_formatted}\r\n{t}].into()")
350
2
}
351

            
352
#[must_use]
353
2
pub fn format_single_selector(p: &CssPathSelector, _tabs: usize) -> String {
354
2
    match p {
355
        CssPathSelector::Global => "CssPathSelector::Global".to_string(),
356
        CssPathSelector::Root(r) => format!(
357
            "CssPathSelector::Root(CssScopeRange {{ start: {}, end: {} }})",
358
            r.start, r.end
359
        ),
360
2
        CssPathSelector::Type(ntp) => format!("CssPathSelector::Type({})", format_node_type(ntp)),
361
        CssPathSelector::Class(class) => {
362
            format!("CssPathSelector::Class(String::from({class:?}))")
363
        }
364
        CssPathSelector::Id(id) => format!("CssPathSelector::Id(String::from({id:?}))"),
365
        CssPathSelector::PseudoSelector(cps) => format!(
366
            "CssPathSelector::PseudoSelector({})",
367
            format_pseudo_selector_type(cps)
368
        ),
369
        CssPathSelector::Attribute(a) => format!(
370
            "CssPathSelector::Attribute({})",
371
            format_attribute_selector(a)
372
        ),
373
        CssPathSelector::DirectChildren => "CssPathSelector::DirectChildren".to_string(),
374
        CssPathSelector::Children => "CssPathSelector::Children".to_string(),
375
        CssPathSelector::AdjacentSibling => "CssPathSelector::AdjacentSibling".to_string(),
376
        CssPathSelector::GeneralSibling => "CssPathSelector::GeneralSibling".to_string(),
377
    }
378
2
}
379

            
380
#[must_use]
381
pub fn format_pseudo_selector_type(p: &CssPathPseudoSelector) -> String {
382
    match p {
383
        CssPathPseudoSelector::First => "CssPathPseudoSelector::First".to_string(),
384
        CssPathPseudoSelector::Last => "CssPathPseudoSelector::Last".to_string(),
385
        CssPathPseudoSelector::NthChild(n) => format!(
386
            "CssPathPseudoSelector::NthChild({})",
387
            format_nth_child_selector(n)
388
        ),
389
        CssPathPseudoSelector::Placeholder => "CssPathPseudoSelector::Placeholder".to_string(),
390
        CssPathPseudoSelector::Hover => "CssPathPseudoSelector::Hover".to_string(),
391
        CssPathPseudoSelector::Active => "CssPathPseudoSelector::Active".to_string(),
392
        CssPathPseudoSelector::Focus => "CssPathPseudoSelector::Focus".to_string(),
393
        CssPathPseudoSelector::SeatFocus => "CssPathPseudoSelector::SeatFocus".to_string(),
394
        CssPathPseudoSelector::Backdrop => "CssPathPseudoSelector::Backdrop".to_string(),
395
        CssPathPseudoSelector::Lang(lang) => format!(
396
            "CssPathPseudoSelector::Lang(AzString::from_const_str(\"{}\"))",
397
            lang.as_str()
398
        ),
399
        CssPathPseudoSelector::Dragging => "CssPathPseudoSelector::Dragging".to_string(),
400
        CssPathPseudoSelector::DragOver => "CssPathPseudoSelector::DragOver".to_string(),
401
        CssPathPseudoSelector::Root => "CssPathPseudoSelector::Root".to_string(),
402
    }
403
}
404

            
405
#[must_use]
406
pub fn format_attribute_selector(a: &CssAttributeSelector) -> String {
407
    let value = a.value.as_ref().map_or_else(
408
        || "OptionString::None".to_string(),
409
        |v| {
410
            format!(
411
                "OptionString::Some(AzString::from_const_str({:?}))",
412
                v.as_str()
413
            )
414
        },
415
    );
416
    format!(
417
        "CssAttributeSelector {{ name: AzString::from_const_str({:?}), op: {}, value: {} }}",
418
        a.name.as_str(),
419
        format_attribute_match_op(&a.op),
420
        value
421
    )
422
}
423

            
424
#[must_use]
425
pub fn format_attribute_match_op(op: &AttributeMatchOp) -> String {
426
    match op {
427
        AttributeMatchOp::Exists => "AttributeMatchOp::Exists".to_string(),
428
        AttributeMatchOp::Eq => "AttributeMatchOp::Eq".to_string(),
429
        AttributeMatchOp::Includes => "AttributeMatchOp::Includes".to_string(),
430
        AttributeMatchOp::DashMatch => "AttributeMatchOp::DashMatch".to_string(),
431
        AttributeMatchOp::Prefix => "AttributeMatchOp::Prefix".to_string(),
432
        AttributeMatchOp::Suffix => "AttributeMatchOp::Suffix".to_string(),
433
        AttributeMatchOp::Substring => "AttributeMatchOp::Substring".to_string(),
434
    }
435
}
436

            
437
#[must_use]
438
pub fn format_nth_child_selector(n: &CssNthChildSelector) -> String {
439
    match n {
440
        CssNthChildSelector::Number(num) => format!("CssNthChildSelector::Number({num})"),
441
        CssNthChildSelector::Even => "CssNthChildSelector::Even".to_string(),
442
        CssNthChildSelector::Odd => "CssNthChildSelector::Odd".to_string(),
443
        CssNthChildSelector::Pattern(CssNthChildPattern {
444
            pattern_repeat,
445
            offset,
446
        }) => format!(
447
            "CssNthChildSelector::Pattern(CssNthChildPattern {{ pattern_repeat: {pattern_repeat}, \
448
             offset: {offset} }})"
449
        ),
450
    }
451
}
452

            
453
#[must_use]
454
pub fn print_declaration(decl: &CssDeclaration, tabs: usize) -> String {
455
    match decl {
456
        CssDeclaration::Static(s) => format!(
457
            "CssDeclaration::Static({})",
458
            format_static_css_prop(s, tabs)
459
        ),
460
        CssDeclaration::Dynamic(d) => format!(
461
            "CssDeclaration::Dynamic({})",
462
            format_dynamic_css_prop(d, tabs)
463
        ),
464
    }
465
}
466

            
467
#[must_use]
468
pub fn format_dynamic_css_prop(decl: &DynamicCssProperty, tabs: usize) -> String {
469
    let t = String::from("    ").repeat(tabs);
470
    format!(
471
        "DynamicCssProperty {{\r\n{}    dynamic_id: {:?},\r\n{}    default_value: {},\r\n{}}}",
472
        t,
473
        decl.dynamic_id,
474
        t,
475
        format_static_css_prop(&decl.default_value, tabs + 1),
476
        t
477
    )
478
}
479

            
480
#[cfg(test)]
481
mod tests {
482
    use alloc::vec;
483

            
484
    use super::*;
485
    use crate::css::CssRuleBlock;
486

            
487
2
    fn sample_css() -> Css {
488
2
        let path = CssPath::new(vec![CssPathSelector::Type(NodeTypeTag::Div)]);
489
2
        let block = CssRuleBlock::new(path, vec![]);
490
2
        Css {
491
2
            rules: vec![block].into(),
492
2
            keyframes: crate::css::KeyframesVec::from_const_slice(&[]),
493
2
        }
494
2
    }
495

            
496
    #[test]
497
1
    fn rust_backend_emits_const_literal() {
498
1
        let css = sample_css();
499
1
        let rust = RustBackend.emit_css(&css);
500
1
        assert!(rust.contains("const CSS: Css"));
501
1
        assert!(rust.contains("NodeTypeTag::Div"));
502
1
    }
503

            
504
    #[test]
505
1
    fn rust_backend_emits_project_files() {
506
1
        let css = sample_css();
507
1
        let files = RustBackend.emit_project(&css);
508
2
        let paths: Vec<_> = files.iter().map(|f| f.path.as_str()).collect();
509
1
        assert!(paths.contains(&"Cargo.toml"));
510
1
        assert!(paths.contains(&"src/main.rs"));
511
1
        let main_rs = files
512
1
            .iter()
513
2
            .find(|f| f.path == "src/main.rs")
514
1
            .expect("main.rs missing");
515
1
        assert!(main_rs.contains_const_literal());
516
1
    }
517

            
518
    impl GeneratedFile {
519
1
        fn contains_const_literal(&self) -> bool {
520
1
            self.contents.contains("const CSS: Css")
521
1
        }
522
    }
523
}