azul-css 0.0.7

Common datatypes used for styling applications using the Azul desktop GUI framework
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
//! Types and methods used to describe the style of an application
use alloc::{string::String, vec::Vec};
use core::fmt;

use crate::{
    dynamic_selector::DynamicSelectorVec,
    props::property::{format_static_css_prop, CssProperty, CssPropertyType},
    AzString,
};

/// Css stylesheet - contains a parsed CSS stylesheet in "rule blocks",
/// i.e. blocks of key-value pairs associated with a selector path.
#[derive(Debug, Default, PartialEq, PartialOrd, Clone)]
#[repr(C)]
pub struct Css {
    /// One CSS stylesheet can hold more than one sub-stylesheet:
    /// For example, when overriding native styles, the `.sort_by_specificy()` function
    /// should not mix the two stylesheets during sorting.
    pub stylesheets: StylesheetVec,
}

impl_vec!(Stylesheet, StylesheetVec, StylesheetVecDestructor, StylesheetVecDestructorType, StylesheetVecSlice, OptionStylesheet);
impl_vec_mut!(Stylesheet, StylesheetVec);
impl_vec_debug!(Stylesheet, StylesheetVec);
impl_vec_partialord!(Stylesheet, StylesheetVec);
impl_vec_clone!(Stylesheet, StylesheetVec, StylesheetVecDestructor);
impl_vec_partialeq!(Stylesheet, StylesheetVec);

impl Css {
    pub fn is_empty(&self) -> bool {
        self.stylesheets.iter().all(|s| s.rules.as_ref().is_empty())
    }

    pub fn new(stylesheets: Vec<Stylesheet>) -> Self {
        Self {
            stylesheets: stylesheets.into(),
        }
    }

    #[cfg(feature = "parser")]
    pub fn from_string(s: crate::AzString) -> Self {
        crate::parser2::new_from_str(s.as_str()).0
    }

    #[cfg(feature = "parser")]
    pub fn from_string_with_warnings(
        s: crate::AzString,
    ) -> (Self, Vec<crate::parser2::CssParseWarnMsgOwned>) {
        let (css, warnings) = crate::parser2::new_from_str(s.as_str());
        (
            css,
            warnings
                .into_iter()
                .map(|w| crate::parser2::CssParseWarnMsgOwned {
                    warning: w.warning.to_contained(),
                    location: w.location,
                })
                .collect(),
        )
    }
}

#[derive(Debug, Default, PartialEq, PartialOrd, Clone)]
#[repr(C)]
pub struct Stylesheet {
    /// The style rules making up the document - for example, de-duplicated CSS rules
    pub rules: CssRuleBlockVec,
}

impl_option!(
    Stylesheet,
    OptionStylesheet,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl_vec!(CssRuleBlock, CssRuleBlockVec, CssRuleBlockVecDestructor, CssRuleBlockVecDestructorType, CssRuleBlockVecSlice, OptionCssRuleBlock);
impl_vec_mut!(CssRuleBlock, CssRuleBlockVec);
impl_vec_debug!(CssRuleBlock, CssRuleBlockVec);
impl_vec_partialord!(CssRuleBlock, CssRuleBlockVec);
impl_vec_clone!(CssRuleBlock, CssRuleBlockVec, CssRuleBlockVecDestructor);
impl_vec_partialeq!(CssRuleBlock, CssRuleBlockVec);

impl Stylesheet {
    pub fn new(rules: Vec<CssRuleBlock>) -> Self {
        Self {
            rules: rules.into(),
        }
    }
}

impl From<Vec<CssRuleBlock>> for Stylesheet {
    fn from(rules: Vec<CssRuleBlock>) -> Self {
        Self {
            rules: rules.into(),
        }
    }
}

/// Contains one parsed `key: value` pair, static or dynamic
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C, u8)]
pub enum CssDeclaration {
    /// Static key-value pair, such as `width: 500px`
    Static(CssProperty),
    /// Dynamic key-value pair with default value, such as `width: [[ my_id | 500px ]]`
    Dynamic(DynamicCssProperty),
}

impl_option!(
    CssDeclaration,
    OptionCssDeclaration,
    copy = false,
    [Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
);

impl CssDeclaration {
    pub const fn new_static(prop: CssProperty) -> Self {
        CssDeclaration::Static(prop)
    }

    pub const fn new_dynamic(prop: DynamicCssProperty) -> Self {
        CssDeclaration::Dynamic(prop)
    }

    /// Returns the type of the property (i.e. the CSS key as a typed enum)
    pub fn get_type(&self) -> CssPropertyType {
        use self::CssDeclaration::*;
        match self {
            Static(s) => s.get_type(),
            Dynamic(d) => d.default_value.get_type(),
        }
    }

    /// Determines if the property will be inherited (applied to the children)
    /// during the recursive application of the style on the DOM tree
    pub fn is_inheritable(&self) -> bool {
        use self::CssDeclaration::*;
        match self {
            Static(s) => s.get_type().is_inheritable(),
            Dynamic(d) => d.is_inheritable(),
        }
    }

    /// Returns whether this rule affects only styling properties or layout
    /// properties (that could trigger a re-layout)
    pub fn can_trigger_relayout(&self) -> bool {
        use self::CssDeclaration::*;
        match self {
            Static(s) => s.get_type().can_trigger_relayout(),
            Dynamic(d) => d.can_trigger_relayout(),
        }
    }

    pub fn to_str(&self) -> String {
        use self::CssDeclaration::*;
        match self {
            Static(s) => format!("{:?}", s),
            Dynamic(d) => format!("var(--{}, {:?})", d.dynamic_id, d.default_value),
        }
    }
}

/// A `DynamicCssProperty` is a type of css property that can be changed on possibly
/// every frame by the Rust code - for example to implement an `On::Hover` behaviour.
///
/// The syntax for such a property looks like this:
///
/// ```no_run,ignore
/// #my_div {
///    padding: var(--my_dynamic_property_id, 400px);
/// }
/// ```
///
/// Azul will register a dynamic property with the key "my_dynamic_property_id"
/// and the default value of 400px. If the property gets overridden during one frame,
/// the overridden property takes precedence.
///
/// At runtime the style is immutable (which is a performance optimization - if we
/// can assume that the property never changes at runtime), we can do some optimizations on it.
/// Dynamic style properties can also be used for animations and conditional styles
/// (i.e. `hover`, `focus`, etc.), thereby leading to cleaner code, since all of these
/// special cases now use one single API.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C)]
pub struct DynamicCssProperty {
    /// The stringified ID of this property, i.e. the `"my_id"` in `width: var(--my_id, 500px)`.
    pub dynamic_id: AzString,
    /// Default values for this properties - one single value can control multiple properties!
    pub default_value: CssProperty,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C, u8)] // necessary for ABI stability
pub enum CssPropertyValue<T> {
    Auto,
    None,
    Initial,
    Inherit,
    Revert,
    Unset,
    Exact(T),
}

pub trait PrintAsCssValue {
    fn print_as_css_value(&self) -> String;
}

impl<T: PrintAsCssValue> CssPropertyValue<T> {
    pub fn get_css_value_fmt(&self) -> String {
        match self {
            CssPropertyValue::Auto => format!("auto"),
            CssPropertyValue::None => format!("none"),
            CssPropertyValue::Initial => format!("initial"),
            CssPropertyValue::Inherit => format!("inherit"),
            CssPropertyValue::Revert => format!("revert"),
            CssPropertyValue::Unset => format!("unset"),
            CssPropertyValue::Exact(e) => e.print_as_css_value(),
        }
    }
}

impl<T: fmt::Display> fmt::Display for CssPropertyValue<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::CssPropertyValue::*;
        match self {
            Auto => write!(f, "auto"),
            None => write!(f, "none"),
            Initial => write!(f, "initial"),
            Inherit => write!(f, "inherit"),
            Revert => write!(f, "revert"),
            Unset => write!(f, "unset"),
            Exact(e) => write!(f, "{}", e),
        }
    }
}

impl<T> From<T> for CssPropertyValue<T> {
    fn from(c: T) -> Self {
        CssPropertyValue::Exact(c)
    }
}

impl<T> CssPropertyValue<T> {
    /// Transforms a `CssPropertyValue<T>` into a `CssPropertyValue<U>` by applying a mapping
    /// function
    #[inline]
    pub fn map_property<F: Fn(T) -> U, U>(self, map_fn: F) -> CssPropertyValue<U> {
        match self {
            CssPropertyValue::Exact(c) => CssPropertyValue::Exact(map_fn(c)),
            CssPropertyValue::Auto => CssPropertyValue::Auto,
            CssPropertyValue::None => CssPropertyValue::None,
            CssPropertyValue::Initial => CssPropertyValue::Initial,
            CssPropertyValue::Inherit => CssPropertyValue::Inherit,
            CssPropertyValue::Revert => CssPropertyValue::Revert,
            CssPropertyValue::Unset => CssPropertyValue::Unset,
        }
    }

    #[inline]
    pub fn get_property(&self) -> Option<&T> {
        match self {
            CssPropertyValue::Exact(c) => Some(c),
            _ => None,
        }
    }

    #[inline]
    pub fn get_property_owned(self) -> Option<T> {
        match self {
            CssPropertyValue::Exact(c) => Some(c),
            _ => None,
        }
    }

    #[inline]
    pub fn is_auto(&self) -> bool {
        match self {
            CssPropertyValue::Auto => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_none(&self) -> bool {
        match self {
            CssPropertyValue::None => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_initial(&self) -> bool {
        match self {
            CssPropertyValue::Initial => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_inherit(&self) -> bool {
        match self {
            CssPropertyValue::Inherit => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_revert(&self) -> bool {
        match self {
            CssPropertyValue::Revert => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_unset(&self) -> bool {
        match self {
            CssPropertyValue::Unset => true,
            _ => false,
        }
    }
}

impl<T: Default> CssPropertyValue<T> {
    #[inline]
    pub fn get_property_or_default(self) -> Option<T> {
        match self {
            CssPropertyValue::Auto | CssPropertyValue::Initial => Some(T::default()),
            CssPropertyValue::Exact(c) => Some(c),
            CssPropertyValue::None
            | CssPropertyValue::Inherit
            | CssPropertyValue::Revert
            | CssPropertyValue::Unset => None,
        }
    }
}

impl<T: Default> Default for CssPropertyValue<T> {
    #[inline]
    fn default() -> Self {
        CssPropertyValue::Exact(T::default())
    }
}

impl DynamicCssProperty {
    pub fn is_inheritable(&self) -> bool {
        // Dynamic style properties should not be inheritable,
        // since that could lead to bugs - you set a property in Rust, suddenly
        // the wrong UI component starts to react because it was inherited.
        false
    }

    pub fn can_trigger_relayout(&self) -> bool {
        self.default_value.get_type().can_trigger_relayout()
    }
}

/// One block of rules that applies a bunch of rules to a "path" in the style, i.e.
/// `div#myid.myclass -> { ("justify-content", "center") }`
///
/// The `conditions` field contains @media/@lang/etc. conditions that must ALL be
/// satisfied for this rule block to apply (from enclosing @-rule blocks).
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct CssRuleBlock {
    /// The css path (full selector) of the style ruleset
    pub path: CssPath,
    /// `"justify-content: center"` =>
    /// `CssDeclaration::Static(CssProperty::JustifyContent(LayoutJustifyContent::Center))`
    pub declarations: CssDeclarationVec,
    /// Conditions from enclosing @-rules (@media, @lang, etc.) that must ALL be
    /// satisfied for this rule block to apply. Empty = unconditional.
    pub conditions: DynamicSelectorVec,
}

impl_option!(
    CssRuleBlock,
    OptionCssRuleBlock,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl PartialOrd for CssRuleBlock {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        // Compare by path and declarations only, conditions are not ordered
        match self.path.partial_cmp(&other.path) {
            Some(core::cmp::Ordering::Equal) => self.declarations.partial_cmp(&other.declarations),
            ord => ord,
        }
    }
}

impl_vec!(CssDeclaration, CssDeclarationVec, CssDeclarationVecDestructor, CssDeclarationVecDestructorType, CssDeclarationVecSlice, OptionCssDeclaration);
impl_vec_mut!(CssDeclaration, CssDeclarationVec);
impl_vec_debug!(CssDeclaration, CssDeclarationVec);
impl_vec_partialord!(CssDeclaration, CssDeclarationVec);
impl_vec_ord!(CssDeclaration, CssDeclarationVec);
impl_vec_clone!(
    CssDeclaration,
    CssDeclarationVec,
    CssDeclarationVecDestructor
);
impl_vec_partialeq!(CssDeclaration, CssDeclarationVec);
impl_vec_eq!(CssDeclaration, CssDeclarationVec);
impl_vec_hash!(CssDeclaration, CssDeclarationVec);

impl CssRuleBlock {
    pub fn new(path: CssPath, declarations: Vec<CssDeclaration>) -> Self {
        Self {
            path,
            declarations: declarations.into(),
            conditions: DynamicSelectorVec::from_const_slice(&[]),
        }
    }

    pub fn with_conditions(
        path: CssPath,
        declarations: Vec<CssDeclaration>,
        conditions: Vec<crate::dynamic_selector::DynamicSelector>,
    ) -> Self {
        Self {
            path,
            declarations: declarations.into(),
            conditions: conditions.into(),
        }
    }
}

pub type CssContentGroup<'a> = Vec<&'a CssPathSelector>;

/// Signifies the type of a DOM node without carrying any associated data
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum NodeTypeTag {
    // Document structure
    Html,
    Head,
    Body,

    // Block-level elements
    Div,
    P,
    Article,
    Section,
    Nav,
    Aside,
    Header,
    Footer,
    Main,
    Figure,
    FigCaption,

    // Headings
    H1,
    H2,
    H3,
    H4,
    H5,
    H6,

    // Inline text
    Br,
    Hr,
    Pre,
    BlockQuote,
    Address,
    Details,
    Summary,
    Dialog,

    // Lists
    Ul,
    Ol,
    Li,
    Dl,
    Dt,
    Dd,
    Menu,
    MenuItem,
    Dir,

    // Tables
    Table,
    Caption,
    THead,
    TBody,
    TFoot,
    Tr,
    Th,
    Td,
    ColGroup,
    Col,

    // Forms
    Form,
    FieldSet,
    Legend,
    Label,
    Input,
    Button,
    Select,
    OptGroup,
    SelectOption,
    TextArea,
    Output,
    Progress,
    Meter,
    DataList,

    // Inline elements
    Span,
    A,
    Em,
    Strong,
    B,
    I,
    U,
    S,
    Mark,
    Del,
    Ins,
    Code,
    Samp,
    Kbd,
    Var,
    Cite,
    Dfn,
    Abbr,
    Acronym,
    Q,
    Time,
    Sub,
    Sup,
    Small,
    Big,
    Bdo,
    Bdi,
    Wbr,
    Ruby,
    Rt,
    Rtc,
    Rp,
    Data,

    // Embedded content
    Canvas,
    Object,
    Param,
    Embed,
    Audio,
    Video,
    Source,
    Track,
    Map,
    Area,
    Svg,

    // Metadata
    Title,
    Meta,
    Link,
    Script,
    Style,
    Base,

    // Special
    Text,
    Img,
    IFrame,
    /// Icon element - resolved to actual content by IconProvider
    Icon,

    // Pseudo-elements
    Before,
    After,
    Marker,
    Placeholder,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NodeTypeTagParseError<'a> {
    Invalid(&'a str),
}

impl<'a> fmt::Display for NodeTypeTagParseError<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            NodeTypeTagParseError::Invalid(e) => write!(f, "Invalid node type: {}", e),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NodeTypeTagParseErrorOwned {
    Invalid(String),
}

impl<'a> NodeTypeTagParseError<'a> {
    pub fn to_contained(&self) -> NodeTypeTagParseErrorOwned {
        match self {
            NodeTypeTagParseError::Invalid(s) => NodeTypeTagParseErrorOwned::Invalid(s.to_string()),
        }
    }
}

impl NodeTypeTagParseErrorOwned {
    pub fn to_shared<'a>(&'a self) -> NodeTypeTagParseError<'a> {
        match self {
            NodeTypeTagParseErrorOwned::Invalid(s) => NodeTypeTagParseError::Invalid(s),
        }
    }
}

/// Parses the node type from a CSS string such as `"div"` => `NodeTypeTag::Div`
impl NodeTypeTag {
    pub fn from_str(css_key: &str) -> Result<Self, NodeTypeTagParseError> {
        match css_key {
            // Document structure
            "html" => Ok(NodeTypeTag::Html),
            "head" => Ok(NodeTypeTag::Head),
            "body" => Ok(NodeTypeTag::Body),

            // Block-level elements
            "div" => Ok(NodeTypeTag::Div),
            "p" => Ok(NodeTypeTag::P),
            "article" => Ok(NodeTypeTag::Article),
            "section" => Ok(NodeTypeTag::Section),
            "nav" => Ok(NodeTypeTag::Nav),
            "aside" => Ok(NodeTypeTag::Aside),
            "header" => Ok(NodeTypeTag::Header),
            "footer" => Ok(NodeTypeTag::Footer),
            "main" => Ok(NodeTypeTag::Main),
            "figure" => Ok(NodeTypeTag::Figure),
            "figcaption" => Ok(NodeTypeTag::FigCaption),

            // Headings
            "h1" => Ok(NodeTypeTag::H1),
            "h2" => Ok(NodeTypeTag::H2),
            "h3" => Ok(NodeTypeTag::H3),
            "h4" => Ok(NodeTypeTag::H4),
            "h5" => Ok(NodeTypeTag::H5),
            "h6" => Ok(NodeTypeTag::H6),

            // Inline text
            "br" => Ok(NodeTypeTag::Br),
            "hr" => Ok(NodeTypeTag::Hr),
            "pre" => Ok(NodeTypeTag::Pre),
            "blockquote" => Ok(NodeTypeTag::BlockQuote),
            "address" => Ok(NodeTypeTag::Address),
            "details" => Ok(NodeTypeTag::Details),
            "summary" => Ok(NodeTypeTag::Summary),
            "dialog" => Ok(NodeTypeTag::Dialog),

            // Lists
            "ul" => Ok(NodeTypeTag::Ul),
            "ol" => Ok(NodeTypeTag::Ol),
            "li" => Ok(NodeTypeTag::Li),
            "dl" => Ok(NodeTypeTag::Dl),
            "dt" => Ok(NodeTypeTag::Dt),
            "dd" => Ok(NodeTypeTag::Dd),
            "menu" => Ok(NodeTypeTag::Menu),
            "menuitem" => Ok(NodeTypeTag::MenuItem),
            "dir" => Ok(NodeTypeTag::Dir),

            // Tables
            "table" => Ok(NodeTypeTag::Table),
            "caption" => Ok(NodeTypeTag::Caption),
            "thead" => Ok(NodeTypeTag::THead),
            "tbody" => Ok(NodeTypeTag::TBody),
            "tfoot" => Ok(NodeTypeTag::TFoot),
            "tr" => Ok(NodeTypeTag::Tr),
            "th" => Ok(NodeTypeTag::Th),
            "td" => Ok(NodeTypeTag::Td),
            "colgroup" => Ok(NodeTypeTag::ColGroup),
            "col" => Ok(NodeTypeTag::Col),

            // Forms
            "form" => Ok(NodeTypeTag::Form),
            "fieldset" => Ok(NodeTypeTag::FieldSet),
            "legend" => Ok(NodeTypeTag::Legend),
            "label" => Ok(NodeTypeTag::Label),
            "input" => Ok(NodeTypeTag::Input),
            "button" => Ok(NodeTypeTag::Button),
            "select" => Ok(NodeTypeTag::Select),
            "optgroup" => Ok(NodeTypeTag::OptGroup),
            "option" => Ok(NodeTypeTag::SelectOption),
            "textarea" => Ok(NodeTypeTag::TextArea),
            "output" => Ok(NodeTypeTag::Output),
            "progress" => Ok(NodeTypeTag::Progress),
            "meter" => Ok(NodeTypeTag::Meter),
            "datalist" => Ok(NodeTypeTag::DataList),

            // Inline elements
            "span" => Ok(NodeTypeTag::Span),
            "a" => Ok(NodeTypeTag::A),
            "em" => Ok(NodeTypeTag::Em),
            "strong" => Ok(NodeTypeTag::Strong),
            "b" => Ok(NodeTypeTag::B),
            "i" => Ok(NodeTypeTag::I),
            "u" => Ok(NodeTypeTag::U),
            "s" => Ok(NodeTypeTag::S),
            "mark" => Ok(NodeTypeTag::Mark),
            "del" => Ok(NodeTypeTag::Del),
            "ins" => Ok(NodeTypeTag::Ins),
            "code" => Ok(NodeTypeTag::Code),
            "samp" => Ok(NodeTypeTag::Samp),
            "kbd" => Ok(NodeTypeTag::Kbd),
            "var" => Ok(NodeTypeTag::Var),
            "cite" => Ok(NodeTypeTag::Cite),
            "dfn" => Ok(NodeTypeTag::Dfn),
            "abbr" => Ok(NodeTypeTag::Abbr),
            "acronym" => Ok(NodeTypeTag::Acronym),
            "q" => Ok(NodeTypeTag::Q),
            "time" => Ok(NodeTypeTag::Time),
            "sub" => Ok(NodeTypeTag::Sub),
            "sup" => Ok(NodeTypeTag::Sup),
            "small" => Ok(NodeTypeTag::Small),
            "big" => Ok(NodeTypeTag::Big),
            "bdo" => Ok(NodeTypeTag::Bdo),
            "bdi" => Ok(NodeTypeTag::Bdi),
            "wbr" => Ok(NodeTypeTag::Wbr),
            "ruby" => Ok(NodeTypeTag::Ruby),
            "rt" => Ok(NodeTypeTag::Rt),
            "rtc" => Ok(NodeTypeTag::Rtc),
            "rp" => Ok(NodeTypeTag::Rp),
            "data" => Ok(NodeTypeTag::Data),

            // Embedded content
            "canvas" => Ok(NodeTypeTag::Canvas),
            "object" => Ok(NodeTypeTag::Object),
            "param" => Ok(NodeTypeTag::Param),
            "embed" => Ok(NodeTypeTag::Embed),
            "audio" => Ok(NodeTypeTag::Audio),
            "video" => Ok(NodeTypeTag::Video),
            "source" => Ok(NodeTypeTag::Source),
            "track" => Ok(NodeTypeTag::Track),
            "map" => Ok(NodeTypeTag::Map),
            "area" => Ok(NodeTypeTag::Area),
            "svg" => Ok(NodeTypeTag::Svg),

            // Metadata
            "title" => Ok(NodeTypeTag::Title),
            "meta" => Ok(NodeTypeTag::Meta),
            "link" => Ok(NodeTypeTag::Link),
            "script" => Ok(NodeTypeTag::Script),
            "style" => Ok(NodeTypeTag::Style),
            "base" => Ok(NodeTypeTag::Base),

            // Special
            "img" => Ok(NodeTypeTag::Img),
            "iframe" => Ok(NodeTypeTag::IFrame),
            "icon" => Ok(NodeTypeTag::Icon),

            // Pseudo-elements (usually prefixed with ::)
            "before" | "::before" => Ok(NodeTypeTag::Before),
            "after" | "::after" => Ok(NodeTypeTag::After),
            "marker" | "::marker" => Ok(NodeTypeTag::Marker),
            "placeholder" | "::placeholder" => Ok(NodeTypeTag::Placeholder),

            other => Err(NodeTypeTagParseError::Invalid(other)),
        }
    }
}

impl fmt::Display for NodeTypeTag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            // Document structure
            NodeTypeTag::Html => write!(f, "html"),
            NodeTypeTag::Head => write!(f, "head"),
            NodeTypeTag::Body => write!(f, "body"),

            // Block elements
            NodeTypeTag::Div => write!(f, "div"),
            NodeTypeTag::P => write!(f, "p"),
            NodeTypeTag::Article => write!(f, "article"),
            NodeTypeTag::Section => write!(f, "section"),
            NodeTypeTag::Nav => write!(f, "nav"),
            NodeTypeTag::Aside => write!(f, "aside"),
            NodeTypeTag::Header => write!(f, "header"),
            NodeTypeTag::Footer => write!(f, "footer"),
            NodeTypeTag::Main => write!(f, "main"),
            NodeTypeTag::Figure => write!(f, "figure"),
            NodeTypeTag::FigCaption => write!(f, "figcaption"),

            // Headings
            NodeTypeTag::H1 => write!(f, "h1"),
            NodeTypeTag::H2 => write!(f, "h2"),
            NodeTypeTag::H3 => write!(f, "h3"),
            NodeTypeTag::H4 => write!(f, "h4"),
            NodeTypeTag::H5 => write!(f, "h5"),
            NodeTypeTag::H6 => write!(f, "h6"),

            // Text formatting
            NodeTypeTag::Br => write!(f, "br"),
            NodeTypeTag::Hr => write!(f, "hr"),
            NodeTypeTag::Pre => write!(f, "pre"),
            NodeTypeTag::BlockQuote => write!(f, "blockquote"),
            NodeTypeTag::Address => write!(f, "address"),
            NodeTypeTag::Details => write!(f, "details"),
            NodeTypeTag::Summary => write!(f, "summary"),
            NodeTypeTag::Dialog => write!(f, "dialog"),

            // List elements
            NodeTypeTag::Ul => write!(f, "ul"),
            NodeTypeTag::Ol => write!(f, "ol"),
            NodeTypeTag::Li => write!(f, "li"),
            NodeTypeTag::Dl => write!(f, "dl"),
            NodeTypeTag::Dt => write!(f, "dt"),
            NodeTypeTag::Dd => write!(f, "dd"),
            NodeTypeTag::Menu => write!(f, "menu"),
            NodeTypeTag::MenuItem => write!(f, "menuitem"),
            NodeTypeTag::Dir => write!(f, "dir"),

            // Table elements
            NodeTypeTag::Table => write!(f, "table"),
            NodeTypeTag::Caption => write!(f, "caption"),
            NodeTypeTag::THead => write!(f, "thead"),
            NodeTypeTag::TBody => write!(f, "tbody"),
            NodeTypeTag::TFoot => write!(f, "tfoot"),
            NodeTypeTag::Tr => write!(f, "tr"),
            NodeTypeTag::Th => write!(f, "th"),
            NodeTypeTag::Td => write!(f, "td"),
            NodeTypeTag::ColGroup => write!(f, "colgroup"),
            NodeTypeTag::Col => write!(f, "col"),

            // Form elements
            NodeTypeTag::Form => write!(f, "form"),
            NodeTypeTag::FieldSet => write!(f, "fieldset"),
            NodeTypeTag::Legend => write!(f, "legend"),
            NodeTypeTag::Label => write!(f, "label"),
            NodeTypeTag::Input => write!(f, "input"),
            NodeTypeTag::Button => write!(f, "button"),
            NodeTypeTag::Select => write!(f, "select"),
            NodeTypeTag::OptGroup => write!(f, "optgroup"),
            NodeTypeTag::SelectOption => write!(f, "option"),
            NodeTypeTag::TextArea => write!(f, "textarea"),
            NodeTypeTag::Output => write!(f, "output"),
            NodeTypeTag::Progress => write!(f, "progress"),
            NodeTypeTag::Meter => write!(f, "meter"),
            NodeTypeTag::DataList => write!(f, "datalist"),

            // Inline elements
            NodeTypeTag::Span => write!(f, "span"),
            NodeTypeTag::A => write!(f, "a"),
            NodeTypeTag::Em => write!(f, "em"),
            NodeTypeTag::Strong => write!(f, "strong"),
            NodeTypeTag::B => write!(f, "b"),
            NodeTypeTag::I => write!(f, "i"),
            NodeTypeTag::U => write!(f, "u"),
            NodeTypeTag::S => write!(f, "s"),
            NodeTypeTag::Mark => write!(f, "mark"),
            NodeTypeTag::Del => write!(f, "del"),
            NodeTypeTag::Ins => write!(f, "ins"),
            NodeTypeTag::Code => write!(f, "code"),
            NodeTypeTag::Samp => write!(f, "samp"),
            NodeTypeTag::Kbd => write!(f, "kbd"),
            NodeTypeTag::Var => write!(f, "var"),
            NodeTypeTag::Cite => write!(f, "cite"),
            NodeTypeTag::Dfn => write!(f, "dfn"),
            NodeTypeTag::Abbr => write!(f, "abbr"),
            NodeTypeTag::Acronym => write!(f, "acronym"),
            NodeTypeTag::Q => write!(f, "q"),
            NodeTypeTag::Time => write!(f, "time"),
            NodeTypeTag::Sub => write!(f, "sub"),
            NodeTypeTag::Sup => write!(f, "sup"),
            NodeTypeTag::Small => write!(f, "small"),
            NodeTypeTag::Big => write!(f, "big"),
            NodeTypeTag::Bdo => write!(f, "bdo"),
            NodeTypeTag::Bdi => write!(f, "bdi"),
            NodeTypeTag::Wbr => write!(f, "wbr"),
            NodeTypeTag::Ruby => write!(f, "ruby"),
            NodeTypeTag::Rt => write!(f, "rt"),
            NodeTypeTag::Rtc => write!(f, "rtc"),
            NodeTypeTag::Rp => write!(f, "rp"),
            NodeTypeTag::Data => write!(f, "data"),

            // Embedded content
            NodeTypeTag::Canvas => write!(f, "canvas"),
            NodeTypeTag::Object => write!(f, "object"),
            NodeTypeTag::Param => write!(f, "param"),
            NodeTypeTag::Embed => write!(f, "embed"),
            NodeTypeTag::Audio => write!(f, "audio"),
            NodeTypeTag::Video => write!(f, "video"),
            NodeTypeTag::Source => write!(f, "source"),
            NodeTypeTag::Track => write!(f, "track"),
            NodeTypeTag::Map => write!(f, "map"),
            NodeTypeTag::Area => write!(f, "area"),
            NodeTypeTag::Svg => write!(f, "svg"),

            // Metadata
            NodeTypeTag::Title => write!(f, "title"),
            NodeTypeTag::Meta => write!(f, "meta"),
            NodeTypeTag::Link => write!(f, "link"),
            NodeTypeTag::Script => write!(f, "script"),
            NodeTypeTag::Style => write!(f, "style"),
            NodeTypeTag::Base => write!(f, "base"),

            // Content elements
            NodeTypeTag::Text => write!(f, "text"),
            NodeTypeTag::Img => write!(f, "img"),
            NodeTypeTag::IFrame => write!(f, "iframe"),
            NodeTypeTag::Icon => write!(f, "icon"),

            // Pseudo-elements
            NodeTypeTag::Before => write!(f, "::before"),
            NodeTypeTag::After => write!(f, "::after"),
            NodeTypeTag::Marker => write!(f, "::marker"),
            NodeTypeTag::Placeholder => write!(f, "::placeholder"),
        }
    }
}

/// Represents a full CSS path (i.e. the "div#id.class" selector belonging to
///  a CSS "content group" (the following key-value block)).
///
/// ```no_run,ignore
/// "#div > .my_class:focus" ==
/// [
///   CssPathSelector::Type(NodeTypeTag::Div),
///   CssPathSelector::PseudoSelector(CssPathPseudoSelector::LimitChildren),
///   CssPathSelector::Class("my_class"),
///   CssPathSelector::PseudoSelector(CssPathPseudoSelector::Focus),
/// ]
#[derive(Clone, Hash, Default, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct CssPath {
    pub selectors: CssPathSelectorVec,
}

impl_vec!(CssPathSelector, CssPathSelectorVec, CssPathSelectorVecDestructor, CssPathSelectorVecDestructorType, CssPathSelectorVecSlice, OptionCssPathSelector);
impl_vec_debug!(CssPathSelector, CssPathSelectorVec);
impl_vec_partialord!(CssPathSelector, CssPathSelectorVec);
impl_vec_ord!(CssPathSelector, CssPathSelectorVec);
impl_vec_clone!(
    CssPathSelector,
    CssPathSelectorVec,
    CssPathSelectorVecDestructor
);
impl_vec_partialeq!(CssPathSelector, CssPathSelectorVec);
impl_vec_eq!(CssPathSelector, CssPathSelectorVec);
impl_vec_hash!(CssPathSelector, CssPathSelectorVec);

impl CssPath {
    pub fn new(selectors: Vec<CssPathSelector>) -> Self {
        Self {
            selectors: selectors.into(),
        }
    }
}

impl fmt::Display for CssPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for selector in self.selectors.as_ref() {
            write!(f, "{}", selector)?;
        }
        Ok(())
    }
}

impl fmt::Debug for CssPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C, u8)]
pub enum CssPathSelector {
    /// Represents the `*` selector
    Global,
    /// `div`, `p`, etc.
    Type(NodeTypeTag),
    /// `.something`
    Class(AzString),
    /// `#something`
    Id(AzString),
    /// `:something`
    PseudoSelector(CssPathPseudoSelector),
    /// Represents the `>` selector (direct child)
    DirectChildren,
    /// Represents the ` ` selector (descendant)
    Children,
    /// Represents the `+` selector (adjacent sibling)
    AdjacentSibling,
    /// Represents the `~` selector (general sibling)
    GeneralSibling,
}

impl_option!(
    CssPathSelector,
    OptionCssPathSelector,
    copy = false,
    [Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
);

impl Default for CssPathSelector {
    fn default() -> Self {
        CssPathSelector::Global
    }
}

impl fmt::Display for CssPathSelector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::CssPathSelector::*;
        match &self {
            Global => write!(f, "*"),
            Type(n) => write!(f, "{}", n),
            Class(c) => write!(f, ".{}", c),
            Id(i) => write!(f, "#{}", i),
            PseudoSelector(p) => write!(f, ":{}", p),
            DirectChildren => write!(f, ">"),
            Children => write!(f, " "),
            AdjacentSibling => write!(f, "+"),
            GeneralSibling => write!(f, "~"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C, u8)]
pub enum CssPathPseudoSelector {
    /// `:first`
    First,
    /// `:last`
    Last,
    /// `:nth-child`
    NthChild(CssNthChildSelector),
    /// `:hover` - mouse is over element
    Hover,
    /// `:active` - mouse is pressed and over element
    Active,
    /// `:focus` - element has received focus
    Focus,
    /// `:lang(de)` - element matches language
    Lang(AzString),
    /// `:backdrop` - window is not focused (GTK compatibility)
    Backdrop,
    /// `:dragging` - element is currently being dragged
    Dragging,
    /// `:drag-over` - a dragged element is over this drop target
    DragOver,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C, u8)]
pub enum CssNthChildSelector {
    Number(u32),
    Even,
    Odd,
    Pattern(CssNthChildPattern),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C)]
pub struct CssNthChildPattern {
    pub pattern_repeat: u32,
    pub offset: u32,
}

impl fmt::Display for CssNthChildSelector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::CssNthChildSelector::*;
        match &self {
            Number(u) => write!(f, "{}", u),
            Even => write!(f, "even"),
            Odd => write!(f, "odd"),
            Pattern(p) => write!(f, "{}n + {}", p.pattern_repeat, p.offset),
        }
    }
}

impl fmt::Display for CssPathPseudoSelector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::CssPathPseudoSelector::*;
        match &self {
            First => write!(f, "first"),
            Last => write!(f, "last"),
            NthChild(u) => write!(f, "nth-child({})", u),
            Hover => write!(f, "hover"),
            Active => write!(f, "active"),
            Focus => write!(f, "focus"),
            Lang(lang) => write!(f, "lang({})", lang.as_str()),
            Backdrop => write!(f, "backdrop"),
            Dragging => write!(f, "dragging"),
            DragOver => write!(f, "drag-over"),
        }
    }
}

impl Css {
    /// Creates a new, empty CSS with no stylesheets
    pub fn empty() -> Self {
        Default::default()
    }

    pub fn sort_by_specificity(&mut self) {
        self.stylesheets
            .as_mut()
            .iter_mut()
            .for_each(|s| s.sort_by_specificity());
    }

    pub fn rules<'a>(&'a self) -> RuleIterator<'a> {
        RuleIterator {
            current_stylesheet: 0,
            current_rule: 0,
            css: self,
        }
    }
}

pub struct RuleIterator<'a> {
    current_stylesheet: usize,
    current_rule: usize,
    css: &'a Css,
}

impl<'a> Iterator for RuleIterator<'a> {
    type Item = &'a CssRuleBlock;
    fn next(&mut self) -> Option<&'a CssRuleBlock> {
        let current_stylesheet = self.css.stylesheets.get(self.current_stylesheet)?;
        match current_stylesheet.rules.get(self.current_rule) {
            Some(s) => {
                self.current_rule += 1;
                Some(s)
            }
            None => {
                self.current_rule = 0;
                self.current_stylesheet += 1;
                self.next()
            }
        }
    }
}

impl Stylesheet {
    /// Creates a new stylesheet with no style rules.
    pub fn empty() -> Self {
        Default::default()
    }

    /// Sort the style rules by their weight, so that the rules are applied in the correct order.
    /// Should always be called when a new style is loaded from an external source.
    pub fn sort_by_specificity(&mut self) {
        self.rules
            .as_mut()
            .sort_by(|a, b| get_specificity(&a.path).cmp(&get_specificity(&b.path)));
    }
}

/// Returns specificity of the given css path. Further information can be found on
/// [the w3 website](http://www.w3.org/TR/selectors/#specificity).
pub fn get_specificity(path: &CssPath) -> (usize, usize, usize, usize) {
    let id_count = path
        .selectors
        .iter()
        .filter(|x| {
            if let CssPathSelector::Id(_) = x {
                true
            } else {
                false
            }
        })
        .count();
    let class_count = path
        .selectors
        .iter()
        .filter(|x| {
            if let CssPathSelector::Class(_) = x {
                true
            } else {
                false
            }
        })
        .count();
    let div_count = path
        .selectors
        .iter()
        .filter(|x| {
            if let CssPathSelector::Type(_) = x {
                true
            } else {
                false
            }
        })
        .count();
    (id_count, class_count, div_count, path.selectors.len())
}

// --- Formatting ---

// High-level CSS to Rust code generation

pub fn css_to_rust_code(css: &Css) -> String {
    let mut output = String::new();

    output.push_str("const CSS: Css = Css {\r\n");
    output.push_str("\tstylesheets: [\r\n");

    for stylesheet in css.stylesheets.iter() {
        output.push_str("\t\tStylesheet {\r\n");
        output.push_str("\t\t\trules: [\r\n");

        for block in stylesheet.rules.iter() {
            output.push_str("\t\t\t\tCssRuleBlock: {\r\n");
            output.push_str(&format!(
                "\t\t\t\t\tpath: {},\r\n",
                print_block_path(&block.path, 5)
            ));

            output.push_str("\t\t\t\t\tdeclarations: [\r\n");

            for declaration in block.declarations.iter() {
                output.push_str(&format!(
                    "\t\t\t\t\t\t{},\r\n",
                    print_declaration(declaration, 6)
                ));
            }

            output.push_str("\t\t\t\t\t]\r\n");

            output.push_str("\t\t\t\t},\r\n");
        }

        output.push_str("\t\t\t]\r\n");
        output.push_str("\t\t},\r\n");
    }

    output.push_str("\t]\r\n");
    output.push_str("};");

    let output = output.replace("\t", "    ");

    output
}

pub fn format_node_type(n: &NodeTypeTag) -> &'static str {
    match n {
        // Document structure
        NodeTypeTag::Html => "NodeTypeTag::Html",
        NodeTypeTag::Head => "NodeTypeTag::Head",
        NodeTypeTag::Body => "NodeTypeTag::Body",

        // Block elements
        NodeTypeTag::Div => "NodeTypeTag::Div",
        NodeTypeTag::P => "NodeTypeTag::P",
        NodeTypeTag::Article => "NodeTypeTag::Article",
        NodeTypeTag::Section => "NodeTypeTag::Section",
        NodeTypeTag::Nav => "NodeTypeTag::Nav",
        NodeTypeTag::Aside => "NodeTypeTag::Aside",
        NodeTypeTag::Header => "NodeTypeTag::Header",
        NodeTypeTag::Footer => "NodeTypeTag::Footer",
        NodeTypeTag::Main => "NodeTypeTag::Main",
        NodeTypeTag::Figure => "NodeTypeTag::Figure",
        NodeTypeTag::FigCaption => "NodeTypeTag::FigCaption",

        // Headings
        NodeTypeTag::H1 => "NodeTypeTag::H1",
        NodeTypeTag::H2 => "NodeTypeTag::H2",
        NodeTypeTag::H3 => "NodeTypeTag::H3",
        NodeTypeTag::H4 => "NodeTypeTag::H4",
        NodeTypeTag::H5 => "NodeTypeTag::H5",
        NodeTypeTag::H6 => "NodeTypeTag::H6",

        // Text formatting
        NodeTypeTag::Br => "NodeTypeTag::Br",
        NodeTypeTag::Hr => "NodeTypeTag::Hr",
        NodeTypeTag::Pre => "NodeTypeTag::Pre",
        NodeTypeTag::BlockQuote => "NodeTypeTag::BlockQuote",
        NodeTypeTag::Address => "NodeTypeTag::Address",
        NodeTypeTag::Details => "NodeTypeTag::Details",
        NodeTypeTag::Summary => "NodeTypeTag::Summary",
        NodeTypeTag::Dialog => "NodeTypeTag::Dialog",

        // List elements
        NodeTypeTag::Ul => "NodeTypeTag::Ul",
        NodeTypeTag::Ol => "NodeTypeTag::Ol",
        NodeTypeTag::Li => "NodeTypeTag::Li",
        NodeTypeTag::Dl => "NodeTypeTag::Dl",
        NodeTypeTag::Dt => "NodeTypeTag::Dt",
        NodeTypeTag::Dd => "NodeTypeTag::Dd",
        NodeTypeTag::Menu => "NodeTypeTag::Menu",
        NodeTypeTag::MenuItem => "NodeTypeTag::MenuItem",
        NodeTypeTag::Dir => "NodeTypeTag::Dir",

        // Table elements
        NodeTypeTag::Table => "NodeTypeTag::Table",
        NodeTypeTag::Caption => "NodeTypeTag::Caption",
        NodeTypeTag::THead => "NodeTypeTag::THead",
        NodeTypeTag::TBody => "NodeTypeTag::TBody",
        NodeTypeTag::TFoot => "NodeTypeTag::TFoot",
        NodeTypeTag::Tr => "NodeTypeTag::Tr",
        NodeTypeTag::Th => "NodeTypeTag::Th",
        NodeTypeTag::Td => "NodeTypeTag::Td",
        NodeTypeTag::ColGroup => "NodeTypeTag::ColGroup",
        NodeTypeTag::Col => "NodeTypeTag::Col",

        // Form elements
        NodeTypeTag::Form => "NodeTypeTag::Form",
        NodeTypeTag::FieldSet => "NodeTypeTag::FieldSet",
        NodeTypeTag::Legend => "NodeTypeTag::Legend",
        NodeTypeTag::Label => "NodeTypeTag::Label",
        NodeTypeTag::Input => "NodeTypeTag::Input",
        NodeTypeTag::Button => "NodeTypeTag::Button",
        NodeTypeTag::Select => "NodeTypeTag::Select",
        NodeTypeTag::OptGroup => "NodeTypeTag::OptGroup",
        NodeTypeTag::SelectOption => "NodeTypeTag::SelectOption",
        NodeTypeTag::TextArea => "NodeTypeTag::TextArea",
        NodeTypeTag::Output => "NodeTypeTag::Output",
        NodeTypeTag::Progress => "NodeTypeTag::Progress",
        NodeTypeTag::Meter => "NodeTypeTag::Meter",
        NodeTypeTag::DataList => "NodeTypeTag::DataList",

        // Inline elements
        NodeTypeTag::Span => "NodeTypeTag::Span",
        NodeTypeTag::A => "NodeTypeTag::A",
        NodeTypeTag::Em => "NodeTypeTag::Em",
        NodeTypeTag::Strong => "NodeTypeTag::Strong",
        NodeTypeTag::B => "NodeTypeTag::B",
        NodeTypeTag::I => "NodeTypeTag::I",
        NodeTypeTag::U => "NodeTypeTag::U",
        NodeTypeTag::S => "NodeTypeTag::S",
        NodeTypeTag::Mark => "NodeTypeTag::Mark",
        NodeTypeTag::Del => "NodeTypeTag::Del",
        NodeTypeTag::Ins => "NodeTypeTag::Ins",
        NodeTypeTag::Code => "NodeTypeTag::Code",
        NodeTypeTag::Samp => "NodeTypeTag::Samp",
        NodeTypeTag::Kbd => "NodeTypeTag::Kbd",
        NodeTypeTag::Var => "NodeTypeTag::Var",
        NodeTypeTag::Cite => "NodeTypeTag::Cite",
        NodeTypeTag::Dfn => "NodeTypeTag::Dfn",
        NodeTypeTag::Abbr => "NodeTypeTag::Abbr",
        NodeTypeTag::Acronym => "NodeTypeTag::Acronym",
        NodeTypeTag::Q => "NodeTypeTag::Q",
        NodeTypeTag::Time => "NodeTypeTag::Time",
        NodeTypeTag::Sub => "NodeTypeTag::Sub",
        NodeTypeTag::Sup => "NodeTypeTag::Sup",
        NodeTypeTag::Small => "NodeTypeTag::Small",
        NodeTypeTag::Big => "NodeTypeTag::Big",
        NodeTypeTag::Bdo => "NodeTypeTag::Bdo",
        NodeTypeTag::Bdi => "NodeTypeTag::Bdi",
        NodeTypeTag::Wbr => "NodeTypeTag::Wbr",
        NodeTypeTag::Ruby => "NodeTypeTag::Ruby",
        NodeTypeTag::Rt => "NodeTypeTag::Rt",
        NodeTypeTag::Rtc => "NodeTypeTag::Rtc",
        NodeTypeTag::Rp => "NodeTypeTag::Rp",
        NodeTypeTag::Data => "NodeTypeTag::Data",

        // Embedded content
        NodeTypeTag::Canvas => "NodeTypeTag::Canvas",
        NodeTypeTag::Object => "NodeTypeTag::Object",
        NodeTypeTag::Param => "NodeTypeTag::Param",
        NodeTypeTag::Embed => "NodeTypeTag::Embed",
        NodeTypeTag::Audio => "NodeTypeTag::Audio",
        NodeTypeTag::Video => "NodeTypeTag::Video",
        NodeTypeTag::Source => "NodeTypeTag::Source",
        NodeTypeTag::Track => "NodeTypeTag::Track",
        NodeTypeTag::Map => "NodeTypeTag::Map",
        NodeTypeTag::Area => "NodeTypeTag::Area",
        NodeTypeTag::Svg => "NodeTypeTag::Svg",

        // Metadata
        NodeTypeTag::Title => "NodeTypeTag::Title",
        NodeTypeTag::Meta => "NodeTypeTag::Meta",
        NodeTypeTag::Link => "NodeTypeTag::Link",
        NodeTypeTag::Script => "NodeTypeTag::Script",
        NodeTypeTag::Style => "NodeTypeTag::Style",
        NodeTypeTag::Base => "NodeTypeTag::Base",

        // Content elements
        NodeTypeTag::Text => "NodeTypeTag::Text",
        NodeTypeTag::Img => "NodeTypeTag::Img",
        NodeTypeTag::IFrame => "NodeTypeTag::IFrame",
        NodeTypeTag::Icon => "NodeTypeTag::Icon",

        // Pseudo-elements
        NodeTypeTag::Before => "NodeTypeTag::Before",
        NodeTypeTag::After => "NodeTypeTag::After",
        NodeTypeTag::Marker => "NodeTypeTag::Marker",
        NodeTypeTag::Placeholder => "NodeTypeTag::Placeholder",
    }
}

pub fn print_block_path(path: &CssPath, tabs: usize) -> String {
    let t = String::from("    ").repeat(tabs);
    let t1 = String::from("    ").repeat(tabs + 1);

    format!(
        "CssPath {{\r\n{}selectors: {}\r\n{}}}",
        t1,
        format_selectors(path.selectors.as_ref(), tabs + 1),
        t
    )
}

pub fn format_selectors(selectors: &[CssPathSelector], tabs: usize) -> String {
    let t = String::from("    ").repeat(tabs);
    let t1 = String::from("    ").repeat(tabs + 1);

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

    format!("vec![\r\n{}\r\n{}].into()", selectors_formatted, t)
}

pub fn format_single_selector(p: &CssPathSelector, _tabs: usize) -> String {
    match p {
        CssPathSelector::Global => format!("CssPathSelector::Global"),
        CssPathSelector::Type(ntp) => format!("CssPathSelector::Type({})", format_node_type(ntp)),
        CssPathSelector::Class(class) => {
            format!("CssPathSelector::Class(String::from({:?}))", class)
        }
        CssPathSelector::Id(id) => format!("CssPathSelector::Id(String::from({:?}))", id),
        CssPathSelector::PseudoSelector(cps) => format!(
            "CssPathSelector::PseudoSelector({})",
            format_pseudo_selector_type(cps)
        ),
        CssPathSelector::DirectChildren => format!("CssPathSelector::DirectChildren"),
        CssPathSelector::Children => format!("CssPathSelector::Children"),
        CssPathSelector::AdjacentSibling => format!("CssPathSelector::AdjacentSibling"),
        CssPathSelector::GeneralSibling => format!("CssPathSelector::GeneralSibling"),
    }
}

pub fn format_pseudo_selector_type(p: &CssPathPseudoSelector) -> String {
    match p {
        CssPathPseudoSelector::First => format!("CssPathPseudoSelector::First"),
        CssPathPseudoSelector::Last => format!("CssPathPseudoSelector::Last"),
        CssPathPseudoSelector::NthChild(n) => format!(
            "CssPathPseudoSelector::NthChild({})",
            format_nth_child_selector(n)
        ),
        CssPathPseudoSelector::Hover => format!("CssPathPseudoSelector::Hover"),
        CssPathPseudoSelector::Active => format!("CssPathPseudoSelector::Active"),
        CssPathPseudoSelector::Focus => format!("CssPathPseudoSelector::Focus"),
        CssPathPseudoSelector::Backdrop => format!("CssPathPseudoSelector::Backdrop"),
        CssPathPseudoSelector::Lang(lang) => format!(
            "CssPathPseudoSelector::Lang(AzString::from_const_str(\"{}\"))",
            lang.as_str()
        ),
        CssPathPseudoSelector::Dragging => format!("CssPathPseudoSelector::Dragging"),
        CssPathPseudoSelector::DragOver => format!("CssPathPseudoSelector::DragOver"),
    }
}

pub fn format_nth_child_selector(n: &CssNthChildSelector) -> String {
    match n {
        CssNthChildSelector::Number(num) => format!("CssNthChildSelector::Number({})", num),
        CssNthChildSelector::Even => format!("CssNthChildSelector::Even"),
        CssNthChildSelector::Odd => format!("CssNthChildSelector::Odd"),
        CssNthChildSelector::Pattern(CssNthChildPattern {
            pattern_repeat,
            offset,
        }) => format!(
            "CssNthChildSelector::Pattern(CssNthChildPattern {{ pattern_repeat: {}, offset: {} }})",
            pattern_repeat, offset
        ),
    }
}

pub fn print_declaration(decl: &CssDeclaration, tabs: usize) -> String {
    match decl {
        CssDeclaration::Static(s) => format!(
            "CssDeclaration::Static({})",
            format_static_css_prop(s, tabs)
        ),
        CssDeclaration::Dynamic(d) => format!(
            "CssDeclaration::Dynamic({})",
            format_dynamic_css_prop(d, tabs)
        ),
    }
}

pub fn format_dynamic_css_prop(decl: &DynamicCssProperty, tabs: usize) -> String {
    let t = String::from("    ").repeat(tabs);
    format!(
        "DynamicCssProperty {{\r\n{}    dynamic_id: {:?},\r\n{}    default_value: {},\r\n{}}}",
        t,
        decl.dynamic_id,
        t,
        format_static_css_prop(&decl.default_value, tabs + 1),
        t
    )
}