bun_css 0.1.0

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
//! Generic trait-based dispatch for CSS value operations.
//!
//! The Zig original (`generics.zig`) uses `@typeInfo`/`@hasDecl`/`@field` comptime
//! reflection to derive `eql`, `hash`, `deepClone`, `toCss`, `parse`, etc. across
//! every CSS value type. Per PORTING.md §Comptime reflection, that has no Rust
//! equivalent — the port defines a trait per protocol, provides blanket impls for
//! the structural cases (Option/Box/slice/Vec/Vec/SmallList/primitives), and
//! per-struct/-enum impls come from the `bun_css_derive` proc-macros
//! (`#[derive(ToCss, DeepClone, CssEql, CssHash)]` etc.).
//!
//! Free functions with the original names are kept as thin trait-method wrappers
//! so call sites in sibling files port 1:1.

use core::cmp::Ordering;

use bun_alloc::Arena; // bumpalo::Bump re-export
use bun_collections::VecExt;
// Zig `std.hash.Wyhash` (iterative) → `bun_wyhash::Wyhash` (the final4 variant
// matching upstream `std.hash.Wyhash`; NOT `Wyhash11`, which is a legacy v0.11
// variant kept only for on-disk lockfile compat — different digest).
// Re-exported `pub` so `#[derive(CssHash)]` (in `bun_css_derive`) can name the
// hasher type as `::bun_css::generics::Wyhash` without depending on `bun_wyhash`
// directly.
pub use bun_wyhash::Wyhash;

use crate::SmallList;
use crate::css_parser as css;
use crate::css_parser::CssResult;
use crate::css_parser::{Parser, ParserOptions};
use crate::printer::Printer;
use crate::values::angle::Angle;
use crate::values::ident::{
    CustomIdent, CustomIdentFns, DashedIdent, DashedIdentFns, Ident, IdentFns,
};
use crate::values::number::{CSSInteger, CSSIntegerFns, CSSNumber, CSSNumberFns};
use crate::values::rect::Rect;
use crate::values::size::Size2D;
use crate::{PrintErr, VendorPrefix};

// `ArrayList(T)` in the Zig is `std.ArrayListUnmanaged(T)` fed the parser arena.
// In this AST crate that maps to `bun_alloc::ArenaVec<'bump, T>`.
pub type ArrayList<'bump, T> = bun_alloc::ArenaVec<'bump, T>;

// ───────────────────────────────────────────────────────────────────────────────
// DeepClone
// ───────────────────────────────────────────────────────────────────────────────

/// Arena-aware deep clone. Equivalent of Zig's `deepClone(T, *const T, Allocator) T`.
///
/// Per-struct/-enum impls come from `#[derive(DeepClone)]`;
/// the Zig `implementDeepClone` body is the spec for that derive (field-wise /
/// variant-wise recursion).
pub trait DeepClone<'bump>: Sized {
    fn deep_clone(&self, bump: &'bump Arena) -> Self;
}

/// `#[derive(DeepClone)]` — field-wise / variant-wise port of Zig's
/// `css.implementDeepClone`. See `src/css_derive/lib.rs` for the expansion
/// rules. Re-exported here so `use crate::generics::DeepClone;` brings both
/// the trait and the derive into scope (same-name trait+derive is the std
/// idiom, cf. `Clone`).
pub use bun_css_derive::DeepClone;

#[inline]
pub fn implement_deep_clone<'bump, T: DeepClone<'bump>>(this: &T, bump: &'bump Arena) -> T {
    // TODO(port): Zig `implementDeepClone` is comptime field/variant reflection.
    // In Rust this is the body of `#[derive(DeepClone)]`; the free fn just
    // forwards to the trait so existing callers keep working.
    this.deep_clone(bump)
}

// Alias: in Zig `deepClone` (structural type-dispatch entry) and
// `implementDeepClone` (field-reflection body) are distinct, but in Rust both
// collapse to `T::deep_clone` because the structural dispatch lives in the
// blanket impls below and the field-reflection lives in `#[derive(DeepClone)]`.
// Kept as a re-export so generated code (`properties_generated.rs`) and
// hand-written callers can use either name.
pub use implement_deep_clone as deep_clone;

// Blanket impls covering the structural cases the Zig switch handled inline.

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for Option<T> {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        self.as_ref().map(|v| v.deep_clone(bump))
    }
}

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for &'bump T {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        // Zig: `bun.create(arena, TT, deepClone(TT, this.*, arena))`
        bump.alloc((**self).deep_clone(bump))
    }
}

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for &'bump [T] {
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        // Zig: alloc slice, then memcpy if simple-copy else element-wise deepClone.
        // PERF(port): Zig fast-paths `isSimpleCopyType` with @memcpy — profile if hot
        // (specialization would let `T: Copy` use `alloc_slice_copy`).
        bump.alloc_slice_fill_iter(self.iter().map(|e| e.deep_clone(bump)))
    }
}

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for ArrayList<'bump, T> {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        // Zig: `css.deepClone(T, arena, this)` → alloc capacity, element-wise deepClone.
        // PERF(port): Zig fast-paths simple-copy types with @memcpy — profile if hot.
        let mut out = ArrayList::with_capacity_in(self.len(), bump);
        for item in self.iter() {
            out.push(item.deep_clone(bump));
        }
        out
    }
}

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for Vec<T> {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        // `Vec::deep_clone_with` takes a per-element closure so the arena
        // lifetime carried by *this* trait's `deep_clone` threads through.
        self.deep_clone_with(|e| e.deep_clone(bump))
    }
}

impl<'bump, T: DeepClone<'bump>, const N: usize> DeepClone<'bump> for SmallList<T, N> {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        let mut ret = SmallList::<T, N>::init_capacity(self.len());
        for item in self.slice() {
            ret.append(item.deep_clone(bump));
        }
        ret
    }
}

// "Simple copy types" + arena-borrowed strings: clone is identity.
macro_rules! deep_clone_copy {
    ($($t:ty),* $(,)?) => {$(
        impl<'bump> DeepClone<'bump> for $t {
            #[inline]
            fn deep_clone(&self, _bump: &'bump Arena) -> Self { *self }
        }
    )*};
}
// `u8` is intentionally omitted: a `DeepClone for u8` impl would make the
// generic `&'bump [T]` impl below overlap the explicit `&'bump [u8]` impl
// (Rust has no stable specialization). Bytes only appear as `[u8]` slices in
// the CSS AST, never as standalone values.
deep_clone_copy!(f32, f64, i32, u32, i64, u64, usize, isize, u16, bool);

impl<'bump> DeepClone<'bump> for &'bump [u8] {
    #[inline]
    fn deep_clone(&self, _bump: &'bump Arena) -> Self {
        // Strings in the CSS parser are always arena allocated
        // So it is safe to skip const strings as they will never be mutated
        *self
    }
}

impl<'bump> DeepClone<'bump> for &'bump str {
    #[inline]
    fn deep_clone(&self, _bump: &'bump Arena) -> Self {
        // Same arena-borrowed-string rule as `&[u8]` above.
        *self
    }
}

impl<'bump, T: DeepClone<'bump>> DeepClone<'bump> for Box<T> {
    #[inline]
    fn deep_clone(&self, bump: &'bump Arena) -> Self {
        Box::new((**self).deep_clone(bump))
    }
}

impl<'bump> DeepClone<'bump> for bun_ast::Loc {
    #[inline]
    fn deep_clone(&self, _bump: &'bump Arena) -> Self {
        *self
    }
}

// ───────────────────────────────────────────────────────────────────────────────
// Eql
// ───────────────────────────────────────────────────────────────────────────────

/// `lhs.eql(&rhs)` for CSS types. This is the equivalent of doing
/// `#[derive(PartialEq)]` in Rust — and most impls could be exactly that.
/// Kept as a separate trait because some CSS types want structural
/// equality that differs from `PartialEq` (e.g. `VendorPrefix`, idents).
pub trait CssEql {
    fn eql(&self, other: &Self) -> bool;
}

/// `#[derive(CssEql)]` — field-wise / variant-wise port of Zig's
/// `css.implementEql`. See `src/css_derive/lib.rs` for the expansion rules.
/// Re-exported here so `use crate::generics::CssEql;` brings both trait and
/// derive into scope (same-name idiom, cf. `Clone`).
pub use bun_css_derive::CssEql;

#[inline]
pub fn implement_eql<T: CssEql>(this: &T, other: &T) -> bool {
    // TODO(port): Zig `implementEql` is comptime field/variant reflection ==
    // the body of `#[derive(CssEql)]`. Free fn forwards to trait.
    this.eql(other)
}

#[inline]
pub(crate) fn eql<T: CssEql>(lhs: &T, rhs: &T) -> bool {
    lhs.eql(rhs)
}

pub(crate) fn eql_list<T: CssEql>(lhs: &ArrayList<'_, T>, rhs: &ArrayList<'_, T>) -> bool {
    if lhs.len() != rhs.len() {
        return false;
    }
    debug_assert_eq!(lhs.len(), rhs.len());
    for (left, right) in lhs.iter().zip(rhs.iter()) {
        if !left.eql(right) {
            return false;
        }
    }
    true
}

// Blanket / base impls.

impl<T: CssEql> CssEql for Option<T> {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        match (self, other) {
            (None, None) => true,
            (Some(a), Some(b)) => a.eql(b),
            _ => false,
        }
    }
}

impl<T: CssEql + ?Sized> CssEql for &T {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        (**self).eql(&**other)
    }
}

impl<T: CssEql> CssEql for [T] {
    fn eql(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }
        for (a, b) in self.iter().zip(other.iter()) {
            if !a.eql(b) {
                return false;
            }
        }
        true
    }
}

impl<'bump, T: CssEql> CssEql for ArrayList<'bump, T> {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        eql_list(self, other)
    }
}

impl<T: CssEql> CssEql for Vec<T> {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        self.slice_const().eql(other.slice_const())
    }
}

impl<T: CssEql, const N: usize> CssEql for SmallList<T, N> {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        self.slice().eql(other.slice())
    }
}

macro_rules! eql_simple {
    ($($t:ty),* $(,)?) => {$(
        impl CssEql for $t {
            #[inline]
            fn eql(&self, other: &Self) -> bool { *self == *other }
        }
    )*};
}
// `u8` omitted to avoid `[T]`/`[u8]` overlap — see deep_clone_copy! note.
eql_simple!(f32, f64, i32, u32, i64, u64, usize, isize, u16, bool);

/// Stamp `impl CssEql for $T` forwarding to `PartialEq::eq`.
///
/// Exported sibling of `eql_simple!` for crate-defined types whose
/// inherent `pub fn eql(&self, other) { self == other }` was a pure `PartialEq`
/// forwarder (Zig `css.implementEql(@This())` leakage).
///
/// Unlike `#[derive(CssEql)]` (field-wise `.eql()` walk), this does **not**
/// require every field type to itself impl `CssEql`; it bridges
/// `PartialEq` → `CssEql` wholesale. Prefer `#[derive(CssEql)]` only when a
/// field's `CssEql` is intentionally *different* from its `PartialEq` (e.g.
/// `Ident`, `Url`).
///
/// REJECTED alternative: `impl<T: PartialEq> CssEql for T {}` blanket — would
/// overlap the existing `Option<T>`/`Vec<T>`/`[T]`/`Box<T>` blanket impls
/// above (coherence).
#[macro_export]
macro_rules! css_eql_partialeq {
    ($($t:ty),+ $(,)?) => {$(
        impl $crate::generics::CssEql for $t {
            #[inline]
            fn eql(&self, other: &Self) -> bool { self == other }
        }
    )+};
}
pub use css_eql_partialeq;

impl CssEql for [u8] {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        bun_core::eql(self, other)
    }
}

impl CssEql for str {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        bun_core::eql(self.as_bytes(), other.as_bytes())
    }
}

impl<T: CssEql, const N: usize> CssEql for [T; N] {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        // Zig: element-wise eql (length is `N` on both sides by type).
        for (a, b) in self.iter().zip(other.iter()) {
            if !a.eql(b) {
                return false;
            }
        }
        true
    }
}

impl<T: CssEql + ?Sized> CssEql for Box<T> {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        (**self).eql(&**other)
    }
}

impl CssEql for VendorPrefix {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        // Zig: `VendorPrefix.eql` is bitwise compare of the packed struct.
        *self == *other
    }
}

impl CssEql for bun_ast::Loc {
    #[inline]
    fn eql(&self, other: &Self) -> bool {
        self.start == other.start
    }
}

impl CssEql for () {
    #[inline]
    fn eql(&self, _other: &Self) -> bool {
        true
    }
}

// CustomIdent/DashedIdent/Ident wrapper structs are hoisted as data-only stubs
// in `crate::values::ident` (lib.rs); the full impls live in gated
// `values/ident.rs` and supersede these on un-gate.
mod ident_eql {
    use super::CssEql;
    use crate::values::ident::{CustomIdent, DashedIdent, Ident};

    macro_rules! ident_eql_impl {
        ($($t:ty),* $(,)?) => {$(
            impl CssEql for $t {
                #[inline]
                fn eql(&self, other: &Self) -> bool {
                    // `.v()` borrows the parser arena (see `crate::arena_str`).
                    bun_core::eql(self.v(), other.v())
                }
            }
        )*};
    }
    ident_eql_impl!(CustomIdent, DashedIdent, Ident);
}

// ───────────────────────────────────────────────────────────────────────────────
// Bridge inherent eql/hash/deep_clone → trait impls
//
// Many CSS value types carry hand-rolled inherent `eql`/`hash`/`deep_clone`
// (ported verbatim from the Zig `implementEql`/`implementHash`/
// `implementDeepClone` bodies — usually because a field is a raw `*const [u8]`
// arena slice that the derive can't see through). The `#[derive(CssEql/…)]`
// expansion on *containing* types dispatches via UFCS trait paths, so those
// inherent methods alone don't satisfy the bound. These thin forwarding impls
// close the gap without duplicating logic.
mod inherent_bridge {
    use super::{Arena, CssEql, CssHash, DeepClone, Wyhash};

    macro_rules! bridge_eql {
        ($($t:ty),* $(,)?) => {$(
            impl CssEql for $t {
                #[inline]
                fn eql(&self, other: &Self) -> bool { <$t>::eql(self, other) }
            }
        )*};
    }
    macro_rules! bridge_hash {
        ($($t:ty),* $(,)?) => {$(
            impl CssHash for $t {
                #[inline]
                fn hash(&self, hasher: &mut Wyhash) { <$t>::hash(self, hasher) }
            }
        )*};
    }
    macro_rules! bridge_deep_clone {
        ($($t:ty),* $(,)?) => {$(
            impl<'bump> DeepClone<'bump> for $t {
                #[inline]
                fn deep_clone(&self, bump: &'bump Arena) -> Self { <$t>::deep_clone(self, bump) }
            }
        )*};
    }
    macro_rules! bridge_deep_clone_copy {
        ($($t:ty),* $(,)?) => {$(
            impl<'bump> DeepClone<'bump> for $t {
                #[inline]
                fn deep_clone(&self, _bump: &'bump Arena) -> Self { Clone::clone(self) }
            }
        )*};
    }

    use crate::values::ident::{CustomIdent, DashedIdent, DashedIdentReference, Ident};
    bridge_hash!(CustomIdent, DashedIdent, Ident, DashedIdentReference);
    bridge_eql!(DashedIdentReference);
    bridge_deep_clone!(CustomIdent, DashedIdent, Ident, DashedIdentReference);

    use crate::values::color::CssColor;
    bridge_hash!(CssColor);
    bridge_deep_clone!(CssColor);

    use crate::values::url::Url;
    bridge_eql!(Url);
    bridge_hash!(Url);
    bridge_deep_clone!(Url);

    use crate::properties::animation::AnimationName;
    bridge_eql!(AnimationName);
    bridge_hash!(AnimationName);
    bridge_deep_clone!(AnimationName);

    use crate::properties::custom::UAEnvironmentVariable;
    impl CssEql for UAEnvironmentVariable {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            UAEnvironmentVariable::eql(*self, *other)
        }
    }
    // CssHash — via #[derive(CssHash)] on the enum (properties/custom.rs).
    bridge_deep_clone_copy!(UAEnvironmentVariable);

    // `Direction` is re-exported from `properties::text` — bridged below as `TextDirection`.
    use crate::selectors::parser::{ViewTransitionPartName, WebKitScrollbarPseudoElement};
    impl CssEql for WebKitScrollbarPseudoElement {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            WebKitScrollbarPseudoElement::eql(*self, *other)
        }
    }
    bridge_eql!(ViewTransitionPartName);
    // CssHash for WebKitScrollbarPseudoElement — via #[derive(CssHash)] on the enum.
    bridge_hash!(ViewTransitionPartName);
    bridge_deep_clone_copy!(WebKitScrollbarPseudoElement, ViewTransitionPartName);

    // ───────────────────────────────────────────────────────────────────────
    // Property value-type bridges — `Property::deep_clone`/`eql` dispatch via
    // `css::generic::deep_clone`/`eql` (trait bounds), but most leaf types
    // only carry inherent methods or `derive(Clone, PartialEq)`. Bridge them.
    // ───────────────────────────────────────────────────────────────────────

    /// Forward `CssEql` to `PartialEq`.
    macro_rules! bridge_eql_partialeq {
        ($($t:ty),* $(,)?) => {$(
            impl CssEql for $t {
                #[inline]
                fn eql(&self, other: &Self) -> bool { PartialEq::eq(self, other) }
            }
        )*};
    }
    /// Forward `IsCompatible` to inherent `is_compatible`.
    macro_rules! bridge_is_compatible {
        ($($t:ty),* $(,)?) => {$(
            impl super::IsCompatible for $t {
                #[inline]
                fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
                    <$t>::is_compatible(self, browsers)
                }
            }
        )*};
    }
    /// Combo: `Clone` → `DeepClone`, `PartialEq` → `CssEql`.
    macro_rules! bridge_clone_partialeq {
        ($($t:ty),* $(,)?) => {
            bridge_deep_clone_copy!($($t),*);
            bridge_eql_partialeq!($($t),*);
        };
    }

    // ── values/ ──
    use crate::values::length::{Length, LengthOrNumber, LengthPercentageOrAuto};
    bridge_clone_partialeq!(LengthPercentageOrAuto, LengthOrNumber, Length);
    bridge_is_compatible!(LengthPercentageOrAuto, LengthOrNumber, Length);

    use crate::values::easing::EasingFunction;
    bridge_clone_partialeq!(EasingFunction);

    use crate::values::alpha::AlphaValue;
    bridge_clone_partialeq!(AlphaValue);

    use crate::values::image::Image;
    bridge_eql!(Image);
    bridge_deep_clone!(Image);
    bridge_is_compatible!(Image);
    // Gradient payload structs only carry inherent `deep_clone`; bridge them so
    // `#[derive(css::DeepClone)]` on `Gradient` / `WebKitGradient` (UFCS trait
    // dispatch) resolves.
    use crate::values::gradient::{
        ConicGradient, LinearGradient, RadialGradient, WebKitGradientLinear, WebKitGradientRadial,
    };
    bridge_deep_clone!(
        LinearGradient,
        RadialGradient,
        ConicGradient,
        WebKitGradientLinear,
        WebKitGradientRadial
    );

    use crate::values::position::{
        HorizontalPositionKeyword, Position, PositionComponent, VerticalPositionKeyword,
    };
    bridge_clone_partialeq!(Position, HorizontalPositionKeyword, VerticalPositionKeyword);
    // `PositionComponent<S>` is generic, so `bridge_clone_partialeq!` (which
    // takes concrete `ty`s) can't cover it — spell the trivial impls out.
    impl<S: Clone + PartialEq> CssEql for PositionComponent<S> {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            PartialEq::eq(self, other)
        }
    }
    impl<'bump, S: Clone + PartialEq> DeepClone<'bump> for PositionComponent<S> {
        #[inline]
        fn deep_clone(&self, _bump: &'bump Arena) -> Self {
            Clone::clone(self)
        }
    }

    use crate::values::rect::Rect;
    impl<T: PartialEq> CssEql for Rect<T> {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            Rect::<T>::eql(self, other)
        }
    }
    impl<'bump, T: Clone> DeepClone<'bump> for Rect<T> {
        #[inline]
        fn deep_clone(&self, bump: &'bump Arena) -> Self {
            Rect::<T>::deep_clone(self, bump)
        }
    }

    use crate::values::size::Size2D;
    impl<T: Clone + PartialEq> CssEql for Size2D<T> {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            Size2D::<T>::eql(self, other)
        }
    }
    impl<'bump, T: Clone + PartialEq> DeepClone<'bump> for Size2D<T> {
        #[inline]
        fn deep_clone(&self, bump: &'bump Arena) -> Self {
            Size2D::<T>::deep_clone(self, bump)
        }
    }

    bridge_is_compatible!(CssColor);

    // ── properties/border ──
    use crate::properties::border::{
        BorderBlockColor, BorderBlockStyle, BorderBlockWidth, BorderColor, BorderInlineColor,
        BorderInlineStyle, BorderInlineWidth, BorderSideWidth, BorderStyle, BorderWidth,
        GenericBorder, LineStyle,
    };
    bridge_eql_partialeq!(LineStyle);
    bridge_deep_clone!(BorderSideWidth);
    bridge_deep_clone_copy!(LineStyle);
    impl super::IsCompatible for BorderSideWidth {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            BorderSideWidth::is_compatible(self, browsers)
        }
    }
    impl super::IsCompatible for LineStyle {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            LineStyle::is_compatible(*self, browsers)
        }
    }
    bridge_clone_partialeq!(
        BorderColor,
        BorderStyle,
        BorderWidth,
        BorderBlockColor,
        BorderBlockStyle,
        BorderBlockWidth,
        BorderInlineColor,
        BorderInlineStyle,
        BorderInlineWidth,
    );
    impl<S: CssEql, const P: u8> CssEql for GenericBorder<S, P> {
        #[inline]
        fn eql(&self, other: &Self) -> bool {
            self.width.eql(&other.width)
                && self.style.eql(&other.style)
                && self.color.eql(&other.color)
        }
    }
    impl<'bump, S: DeepClone<'bump>, const P: u8> DeepClone<'bump> for GenericBorder<S, P> {
        #[inline]
        fn deep_clone(&self, bump: &'bump Arena) -> Self {
            GenericBorder {
                width: self.width.deep_clone(bump),
                style: self.style.deep_clone(bump),
                color: self.color.deep_clone(bump),
            }
        }
    }

    use crate::properties::outline::OutlineStyle;
    bridge_clone_partialeq!(OutlineStyle);

    use crate::properties::border_image::{
        BorderImage, BorderImageRepeat, BorderImageSideWidth, BorderImageSlice,
    };
    // PORT NOTE: BorderImageRepeat/SideWidth/Slice carry inherent
    // `deep_clone(&self, &Arena)` / `eql(&self, &Self)` (no `Clone`/`PartialEq`
    // derives — see border_image.rs), so route through bridge_deep_clone/eql.
    bridge_deep_clone!(BorderImageRepeat, BorderImageSideWidth, BorderImageSlice);
    bridge_eql!(BorderImageRepeat, BorderImageSlice);
    bridge_deep_clone!(BorderImage);
    bridge_eql!(BorderImage);

    use crate::properties::border_radius::BorderRadius;
    // PORT NOTE: BorderRadius has inherent deep_clone/eql (Size2D<T> lacks
    // Clone/PartialEq derives — see border_radius.rs PORT NOTE).
    bridge_deep_clone!(BorderRadius);
    bridge_eql!(BorderRadius);

    // ── properties/background ──
    use crate::properties::background::{
        Background, BackgroundAttachment, BackgroundClip, BackgroundOrigin, BackgroundPosition,
        BackgroundRepeat, BackgroundSize,
    };
    bridge_eql!(Background);
    bridge_deep_clone!(Background, BackgroundSize, BackgroundPosition);
    bridge_deep_clone_copy!(BackgroundRepeat);
    bridge_clone_partialeq!(BackgroundAttachment, BackgroundClip, BackgroundOrigin);

    // ── properties/align ──
    use crate::properties::align::{
        AlignContent, AlignItems, AlignSelf, Gap, GapValue, JustifyContent, JustifyItems,
        JustifySelf, PlaceContent, PlaceItems, PlaceSelf,
    };
    bridge_clone_partialeq!(
        AlignContent,
        AlignItems,
        AlignSelf,
        JustifyContent,
        JustifyItems,
        JustifySelf,
        PlaceContent,
        PlaceItems,
        PlaceSelf,
        Gap,
        GapValue,
    );

    // ── properties/flex ──
    use crate::properties::flex::{
        BoxAlign, BoxDirection, BoxLines, BoxOrient, BoxPack, Flex, FlexDirection, FlexFlow,
        FlexItemAlign, FlexLinePack, FlexPack, FlexWrap,
    };
    bridge_clone_partialeq!(
        FlexDirection,
        FlexWrap,
        FlexFlow,
        Flex,
        BoxOrient,
        BoxDirection,
        BoxAlign,
        BoxPack,
        BoxLines,
        FlexPack,
        FlexItemAlign,
        FlexLinePack,
    );

    // ── properties/font ──
    use crate::properties::font::{
        FontFamily, FontSize, FontStretch, FontStyle, FontVariantCaps, FontWeight, LineHeight,
    };
    bridge_clone_partialeq!(
        FontWeight,
        FontSize,
        FontStretch,
        FontStyle,
        FontVariantCaps,
        LineHeight,
    );
    impl super::IsCompatible for FontWeight {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontWeight::is_compatible(self, browsers)
        }
    }
    impl super::IsCompatible for FontSize {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontSize::is_compatible(self, browsers)
        }
    }
    impl super::IsCompatible for FontStretch {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontStretch::is_compatible(*self, browsers)
        }
    }
    impl super::IsCompatible for FontStyle {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontStyle::is_compatible(*self, browsers)
        }
    }
    impl super::IsCompatible for FontVariantCaps {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontVariantCaps::is_compatible(*self, browsers)
        }
    }
    impl super::IsCompatible for LineHeight {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            LineHeight::is_compatible(self, browsers)
        }
    }
    impl super::IsCompatible for FontFamily {
        #[inline]
        fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
            FontFamily::is_compatible(self, browsers)
        }
    }
    bridge_clone_partialeq!(FontFamily);
    // `Font` DeepClone/CssEql now via `#[derive]` on the struct (properties/font.rs).

    // ── properties/size ──
    use crate::properties::size::{AspectRatio, BoxSizing, MaxSize, Size};
    bridge_eql!(Size, MaxSize, AspectRatio);
    bridge_deep_clone!(Size, MaxSize, AspectRatio);
    bridge_clone_partialeq!(BoxSizing);

    // ── properties/display ──
    use crate::properties::display::{Display, Visibility};
    bridge_deep_clone_copy!(Display);
    bridge_eql_partialeq!(Display);
    bridge_clone_partialeq!(Visibility);

    // ── properties/overflow ──
    use crate::properties::overflow::{Overflow, OverflowKeyword, TextOverflow};
    bridge_clone_partialeq!(Overflow, OverflowKeyword, TextOverflow);

    // ── properties/position ──
    use crate::properties::position::Position as PositionProp;
    bridge_clone_partialeq!(PositionProp);

    // ── properties/text ──
    use crate::properties::text::{Direction as TextDirection, TextShadow};
    bridge_clone_partialeq!(TextDirection);
    // CssHash for TextDirection — via #[derive(CssHash)] on the enum (properties/text.rs).
    bridge_deep_clone!(TextShadow);
    bridge_eql_partialeq!(TextShadow);

    // ── properties/transform ──
    use crate::properties::transform::{
        BackfaceVisibility, Perspective, Rotate, Scale, TransformBox, TransformList,
        TransformStyle, Translate,
    };
    bridge_deep_clone!(TransformList, Translate, Rotate, Scale, Perspective);
    // Unit enums via DefineEnumProperty — no inherent eql/deep_clone, route through Copy/PartialEq.
    bridge_clone_partialeq!(TransformStyle, TransformBox, BackfaceVisibility);

    // ── properties/transition ──
    use crate::properties::transition::Transition;
    bridge_deep_clone!(Transition);
    bridge_eql!(Transition);

    // ── properties/masking ──
    use crate::properties::masking::{
        GeometryBox, MaskBorderMode, MaskClip, MaskComposite, MaskMode, MaskType,
        WebKitMaskComposite, WebKitMaskSourceType,
    };
    bridge_clone_partialeq!(
        GeometryBox,
        MaskMode,
        MaskClip,
        MaskComposite,
        MaskType,
        MaskBorderMode,
        WebKitMaskComposite,
        WebKitMaskSourceType,
    );
    // `Mask`/`MaskBorder` DeepClone/CssEql now via `#[derive]` on the structs
    // (properties/masking.rs).

    // ── properties/ui ──
    use crate::properties::ui::ColorScheme;
    bridge_deep_clone_copy!(ColorScheme);
    bridge_eql_partialeq!(ColorScheme);

    // ── properties/css_modules ──
    use crate::properties::css_modules::Composes;
    bridge_deep_clone!(Composes);
    bridge_eql!(Composes);

    // ── properties/margin_padding ──
    use crate::properties::margin_padding::{
        Inset, InsetBlock, InsetInline, Margin, MarginBlock, MarginInline, Padding, PaddingBlock,
        PaddingInline, ScrollMargin, ScrollMarginBlock, ScrollMarginInline, ScrollPadding,
        ScrollPaddingBlock, ScrollPaddingInline,
    };
    bridge_clone_partialeq!(
        Margin,
        MarginBlock,
        MarginInline,
        Padding,
        PaddingBlock,
        PaddingInline,
        ScrollMargin,
        ScrollMarginBlock,
        ScrollMarginInline,
        ScrollPadding,
        ScrollPaddingBlock,
        ScrollPaddingInline,
        Inset,
        InsetBlock,
        InsetInline,
    );

    // ── properties/properties_generated ──
    use crate::properties::PropertyId;
    bridge_deep_clone_copy!(PropertyId);
    bridge_eql_partialeq!(PropertyId);
}

// TODO(port): Zig also special-cases `@typeInfo(T).struct.layout == .packed` →
// bitwise `==`. In Rust those are `bitflags!` types implementing `PartialEq`;
// add `impl<T: BitFlags> CssEql for T` or per-type impls.

// ───────────────────────────────────────────────────────────────────────────────
// Hash
// ───────────────────────────────────────────────────────────────────────────────

pub const HASH_SEED: u64 = 0;

/// Wyhash-based structural hash for CSS values.
pub trait CssHash {
    fn hash(&self, hasher: &mut Wyhash);
}

/// `#[derive(CssHash)]` — field-wise / variant-wise port of Zig's
/// `css.implementHash`. See `src/css_derive/lib.rs` for the expansion rules.
/// Re-exported here so `use crate::generics::CssHash;` brings both trait and
/// derive into scope.
pub use bun_css_derive::CssHash;

#[inline]
pub fn implement_hash<T: CssHash>(this: &T, hasher: &mut Wyhash) {
    // TODO(port): Zig `implementHash` is comptime field/variant reflection ==
    // the body of `#[derive(CssHash)]`. Free fn forwards to trait.
    this.hash(hasher)
}

#[inline]
pub fn hash<T: CssHash>(this: &T, hasher: &mut Wyhash) {
    this.hash(hasher)
}

pub(crate) fn hash_array_list<V: CssHash>(this: &ArrayList<'_, V>, hasher: &mut Wyhash) {
    for item in this.iter() {
        item.hash(hasher);
    }
}

pub(crate) fn hash_baby_list<V: CssHash>(this: &Vec<V>, hasher: &mut Wyhash) {
    for item in this.slice_const() {
        item.hash(hasher);
    }
}

impl CssHash for () {
    #[inline]
    fn hash(&self, _hasher: &mut Wyhash) {}
}

impl<T: CssHash> CssHash for Option<T> {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        // Zig `hash()` dispatcher: Some → hash inner, None → no-op (no prefix).
        // The "null"/"some" prefixes are from implementHash's dead .optional arm
        // (guarded by @compileError) — do NOT emit them here.
        if let Some(v) = self {
            v.hash(hasher);
        }
    }
}

impl<T: CssHash + ?Sized> CssHash for &T {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        (**self).hash(hasher)
    }
}

impl<T: CssHash> CssHash for [T] {
    fn hash(&self, hasher: &mut Wyhash) {
        // Zig `hash()` for `.slice` pointers iterates items only — no len prefix.
        for item in self {
            item.hash(hasher);
        }
    }
}

impl<T: CssHash, const N: usize> CssHash for [T; N] {
    fn hash(&self, hasher: &mut Wyhash) {
        // Zig: `bun.writeAnyToHasher(hasher, list.len)` — feeds the raw bytes
        // of `usize` into the hasher. `bun_core::write_any_to_hasher` exists
        // but is `H: Hasher`-generic and routes through `Hasher::write`, which
        // for `Wyhash11` calls `update` — so inlining the `usize` byte-feed
        // here is byte-identical and avoids the trait hop.
        hasher.update(&self.len().to_ne_bytes());
        for item in self {
            item.hash(hasher);
        }
    }
}

impl<'bump, T: CssHash> CssHash for ArrayList<'bump, T> {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        hash_array_list(self, hasher)
    }
}

impl<T: CssHash> CssHash for Vec<T> {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        hash_baby_list(self, hasher)
    }
}

impl<T: CssHash, const N: usize> CssHash for SmallList<T, N> {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        for item in self.slice() {
            item.hash(hasher);
        }
    }
}

macro_rules! hash_simple {
    ($($t:ty),* $(,)?) => {$(
        impl CssHash for $t {
            #[inline]
            fn hash(&self, hasher: &mut Wyhash) {
                // Zig: `hasher.update(std.mem.asBytes(&this))`
                hasher.update(&self.to_ne_bytes());
            }
        }
    )*};
}
// `u8` omitted to avoid `[T]`/`[u8]` overlap — see deep_clone_copy! note.
hash_simple!(f32, f64, i32, u32, i64, u64, usize, isize, u16);

impl CssHash for bool {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        hasher.update(&[*self as u8]);
    }
}

impl CssHash for [u8] {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        hasher.update(self);
    }
}

impl CssHash for str {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        hasher.update(self.as_bytes());
    }
}

impl<T: CssHash + ?Sized> CssHash for Box<T> {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        (**self).hash(hasher)
    }
}

impl CssHash for VendorPrefix {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        // Zig: `hasher.update(std.mem.asBytes(&this))` on the packed-struct repr.
        hasher.update(&[self.as_bits()]);
    }
}

impl CssHash for bun_ast::Loc {
    #[inline]
    fn hash(&self, hasher: &mut Wyhash) {
        // Zig `implementHash` doesn't reach `Loc` (callers skip it), but
        // providing a structural hash here lets `#[derive(CssHash)]` types
        // include a `loc` field without `#[css(skip)]` if they want.
        hasher.update(&self.start.to_ne_bytes());
    }
}

// ───────────────────────────────────────────────────────────────────────────────
// slice / isCompatible
// ───────────────────────────────────────────────────────────────────────────────

/// Uniform `.as_slice()` over the three list container shapes the CSS parser uses.
pub trait ListContainer {
    type Item;
    fn slice(&self) -> &[Self::Item];
}

impl<'bump, T> ListContainer for ArrayList<'bump, T> {
    type Item = T;
    #[inline]
    fn slice(&self) -> &[T] {
        self.as_slice()
    }
}
impl<T> ListContainer for Vec<T> {
    type Item = T;
    #[inline]
    fn slice(&self) -> &[T] {
        self.slice_const()
    }
}
impl<T, const N: usize> ListContainer for SmallList<T, N> {
    type Item = T;
    #[inline]
    fn slice(&self) -> &[T] {
        SmallList::slice(self)
    }
}

#[inline]
pub fn slice<L: ListContainer>(val: &L) -> &[L::Item] {
    val.slice()
}

pub trait IsCompatible {
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool;
}

/// `#[derive(IsCompatible)]` — field-wise / variant-wise port of the
/// hand-written `isCompatible` pattern (struct → AND of fields, enum → unit
/// variants `true` / payload variants delegate). See `src/css_derive/lib.rs`.
/// Re-exported here so `use crate::generics::IsCompatible;` brings both trait
/// and derive into scope.
pub use bun_css_derive::IsCompatible;

#[inline]
pub(crate) fn is_compatible<T: IsCompatible>(val: &T, browsers: &crate::targets::Browsers) -> bool {
    val.is_compatible(browsers)
}

impl<T: IsCompatible + ?Sized> IsCompatible for &T {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        (**self).is_compatible(browsers)
    }
}

impl<T: IsCompatible + ?Sized> IsCompatible for Box<T> {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        (**self).is_compatible(browsers)
    }
}

impl<T: IsCompatible> IsCompatible for Option<T> {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        // Zig's `isCompatible` doesn't special-case Optional, but every
        // hand-written caller treats absent as compatible (no value → no
        // feature gate to check).
        match self {
            Some(v) => v.is_compatible(browsers),
            None => true,
        }
    }
}

impl<T: IsCompatible> IsCompatible for [T] {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        for item in self {
            if !item.is_compatible(browsers) {
                return false;
            }
        }
        true
    }
}

impl<T: IsCompatible, const N: usize> IsCompatible for [T; N] {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        self.as_slice().is_compatible(browsers)
    }
}

impl<T: IsCompatible> IsCompatible for Vec<T> {
    #[inline]
    fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
        self.as_slice().is_compatible(browsers)
    }
}

// The Zig original blanket-impls over "any list container". A Rust blanket
// `impl<L: ListContainer>` conflicts with the `&T` impl above (coherence can't
// prove `&T` never impls `ListContainer`), so spell out the three concrete
// container types instead.
macro_rules! is_compatible_container {
    ($(($($gen:tt)*) $ty:ty),* $(,)?) => {$(
        impl<$($gen)*> IsCompatible for $ty
        where
            <$ty as ListContainer>::Item: IsCompatible,
        {
            fn is_compatible(&self, browsers: &crate::targets::Browsers) -> bool {
                for item in ListContainer::slice(self) {
                    if !item.is_compatible(browsers) {
                        return false;
                    }
                }
                true
            }
        }
    )*};
}
is_compatible_container!(
    ('bump, T) ArrayList<'bump, T>,
    (T, const N: usize) SmallList<T, N>,
);

// ───────────────────────────────────────────────────────────────────────────────
// Parse / ParseWithOptions
// ───────────────────────────────────────────────────────────────────────────────
// Zig's `generic.parse(T, input)` / `generic.parseWithOptions(T, input, opts)`
// dispatch via `@hasDecl(T, "parse"[WithOptions])`. In Rust each leaf value
// type either hand-writes an inherent `parse(&mut Parser) -> CssResult<Self>`
// or derives one via `#[derive(Parse)]` / `#[derive(DefineEnumProperty)]`; the
// trait below is the uniform bound that `Property::parse` and the container
// blanket impls (`SmallList`/`Vec`/`Option`/`Size2D`/`Rect`) need.
//
// `Parse` is intentionally lifetime-free: every value-type parser takes
// `&mut Parser<'_>` (the borrowed source slice) and returns an owned value.
// TODO(refactor): `'bump` arena threading is a follow-up; until then the parser
// holds the arena and arena-backed lists go through `from_list(Vec)`.

/// `T::parse(&mut Parser) -> CssResult<T>`.
pub trait Parse: Sized {
    fn parse(input: &mut Parser) -> CssResult<Self>;
}

/// `T::parse_with_options(&mut Parser, &ParserOptions) -> CssResult<T>`.
///
/// Zig falls through to `parse` when a type has no `parseWithOptions` decl.
/// PORT NOTE: Rust can't express that as a `where Self: Parse` default method
/// — the bound becomes part of the *method signature*, so the free
/// `parse_with_options::<T>` below would require `T: Parse` even for impls
/// that override the body. Instead the fallthrough lives in
/// `impl_pwo_via_parse!`/`impl_parse_tocss_via_inherent!` and the container
/// impls; every `ParseWithOptions` impl provides the method explicitly.
pub trait ParseWithOptions: Sized {
    fn parse_with_options(input: &mut Parser, options: &ParserOptions) -> CssResult<Self>;
}

#[inline]
pub fn parse_with_options<T: ParseWithOptions>(
    input: &mut Parser,
    options: &ParserOptions,
) -> CssResult<T> {
    T::parse_with_options(input, options)
}

#[inline]
pub fn parse<T: Parse>(input: &mut Parser) -> CssResult<T> {
    T::parse(input)
}

#[inline]
pub fn parse_for<T: Parse>() -> fn(&mut Parser) -> CssResult<T> {
    |input| T::parse(input)
}

// ── container / primitive Parse impls ────────────────────────────────────────

impl<T: Parse> Parse for Option<T> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        Ok(input.try_parse(T::parse).ok())
    }
}
impl<T: Parse> ParseWithOptions for Option<T> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        <Self as Parse>::parse(input)
    }
}

impl<T: Parse> Parse for Vec<T> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        input.parse_comma_separated(T::parse)
    }
}
impl<T: Parse> ParseWithOptions for Vec<T> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        <Self as Parse>::parse(input)
    }
}

// Zig `.pointer` arm (`generics.zig:273-279`): parse the pointee then heap-allocate.
impl<T: Parse> Parse for Box<T> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        T::parse(input).map(Box::new)
    }
}
impl<T: Parse> ParseWithOptions for Box<T> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        <Self as Parse>::parse(input)
    }
}

impl<T: Parse, const N: usize> Parse for SmallList<T, N> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        input
            .parse_comma_separated(T::parse)
            .map(SmallList::from_list)
    }
}
impl<T: Parse, const N: usize> ParseWithOptions for SmallList<T, N> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        <Self as Parse>::parse(input)
    }
}

impl<T: Parse + Clone + PartialEq> Parse for Size2D<T> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        Size2D::<T>::parse(input)
    }
}
impl<T: Parse + Clone + PartialEq> ParseWithOptions for Size2D<T> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        Size2D::<T>::parse(input)
    }
}

impl<T: Parse + Clone> Parse for Rect<T> {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        Rect::<T>::parse(input)
    }
}
impl<T: Parse + Clone> ParseWithOptions for Rect<T> {
    #[inline]
    fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
        Rect::<T>::parse(input)
    }
}

impl Parse for f32 {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        CSSNumberFns::parse(input)
    }
}
impl Parse for CSSInteger {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        CSSIntegerFns::parse(input)
    }
}
impl Parse for CustomIdent {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        CustomIdentFns::parse(input)
    }
}
impl Parse for DashedIdent {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        DashedIdentFns::parse(input)
    }
}
impl Parse for Ident {
    #[inline]
    fn parse(input: &mut Parser) -> CssResult<Self> {
        IdentFns::parse(input)
    }
}

// ───────────────────────────────────────────────────────────────────────────────
// ToCss
// ───────────────────────────────────────────────────────────────────────────────

pub trait ToCss {
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr>;
}

#[inline]
pub(crate) fn to_css<T: ToCss>(this: &T, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
    this.to_css(dest)
}

impl<T: ToCss + ?Sized> ToCss for &T {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        (**self).to_css(dest)
    }
}

// Zig `.pointer` arm (`generics.zig:338-341`): recurse into `*T` pointee.
impl<T: ToCss + ?Sized> ToCss for Box<T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        (**self).to_css(dest)
    }
}

impl<T: ToCss> ToCss for Option<T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        if let Some(val) = self {
            return val.to_css(dest);
        }
        Ok(())
    }
}

impl<'bump, T: ToCss> ToCss for ArrayList<'bump, T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        css::to_css::from_list(self.as_slice(), dest)
    }
}

impl<T: ToCss> ToCss for Vec<T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        css::to_css::from_list(self.as_slice(), dest)
    }
}

impl<T: ToCss, const N: usize> ToCss for SmallList<T, N> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        css::to_css::from_list(self.slice(), dest)
    }
}

impl<T: ToCss + Clone + PartialEq> ToCss for Size2D<T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        Size2D::<T>::to_css(self, dest)
    }
}

impl<T: ToCss + PartialEq> ToCss for Rect<T> {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        Rect::<T>::to_css(self, dest)
    }
}

impl ToCss for f32 {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        CSSNumberFns::to_css(*self, dest)
    }
}
impl ToCss for CSSInteger {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        CSSIntegerFns::to_css(*self, dest)
    }
}
impl ToCss for CustomIdent {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        CustomIdentFns::to_css(self, dest)
    }
}
impl ToCss for DashedIdent {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        DashedIdentFns::to_css(self, dest)
    }
}
impl ToCss for Ident {
    #[inline]
    fn to_css(&self, dest: &mut Printer) -> core::result::Result<(), PrintErr> {
        IdentFns::to_css(self, dest)
    }
}

// ── leaf-type forwarding macros ──────────────────────────────────────────────
// Every CSS leaf value type carries inherent `parse` / `to_css` (hand-written
// or derived). `Property::{parse,value_to_css}` dispatch through the
// `generic::{Parse,ToCss,ParseWithOptions}` *traits*, so each leaf must impl
// them.
//
// Two sources of the trait impl:
//   1. `#[derive(ToCss/Parse/DefineEnumProperty)]`
//      (bun_css_derive) — emits the trait impl directly.
//   2. Hand-written leaves — list them under `impl_parse_tocss_via_inherent!`
//      to forward the trait to the inherent.
//
// A type must use exactly one of the two; listing a derive-carrying type in
// the macro is an E0119 coherence conflict.
#[macro_export]
macro_rules! impl_parse_tocss_via_inherent {
    ($($ty:ty),+ $(,)?) => {$(
        impl $crate::generics::Parse for $ty {
            #[inline]
            fn parse(input: &mut $crate::css_parser::Parser) -> $crate::css_parser::CssResult<Self> {
                <$ty>::parse(input)
            }
        }
        impl $crate::generics::ParseWithOptions for $ty {
            #[inline]
            fn parse_with_options(
                input: &mut $crate::css_parser::Parser,
                _options: &$crate::css_parser::ParserOptions,
            ) -> $crate::css_parser::CssResult<Self> {
                <$ty>::parse(input)
            }
        }
        impl $crate::generics::ToCss for $ty {
            #[inline]
            fn to_css(
                &self,
                dest: &mut $crate::printer::Printer,
            ) -> ::core::result::Result<(), $crate::PrintErr> {
                (*self).to_css(dest)
            }
        }
    )+};
}

/// `ParseWithOptions` for primitives — same fallthrough as the macro, but
/// listed once rather than duplicated per call site.
macro_rules! impl_pwo_via_parse {
    ($($ty:ty),+ $(,)?) => {$(
        impl ParseWithOptions for $ty {
            #[inline]
            fn parse_with_options(input: &mut Parser, _options: &ParserOptions) -> CssResult<Self> {
                <$ty as Parse>::parse(input)
            }
        }
    )+};
}
impl_pwo_via_parse!(f32, CSSInteger, CustomIdent, DashedIdent, Ident);

// ───────────────────────────────────────────────────────────────────────────────
// Numeric helpers (tryFromAngle / trySign / tryMap / tryOp / tryOpTo / partialCmp)
// ───────────────────────────────────────────────────────────────────────────────

pub trait TryFromAngle: Sized {
    fn try_from_angle(angle: Angle) -> Option<Self>;
}

impl TryFromAngle for CSSNumber {
    #[inline]
    fn try_from_angle(angle: Angle) -> Option<Self> {
        CSSNumberFns::try_from_angle(angle)
    }
}

pub trait TrySign {
    fn try_sign(&self) -> Option<f32>;
}

impl TrySign for CSSNumber {
    #[inline]
    fn try_sign(&self) -> Option<f32> {
        Some(CSSNumberFns::sign(*self))
    }
}
// TODO(port): Zig fallback `if @hasDecl(T, "sign") T.sign else T.trySign` —
// model as a trait with default method delegating to `Sign` where available.

pub trait TryMap: Sized {
    // Zig: `comptime map_fn: *const fn(f32) f32` — generic param preserves monomorphization.
    fn try_map(&self, map_fn: impl Fn(f32) -> f32) -> Option<Self>;
}

impl TryMap for CSSNumber {
    #[inline]
    fn try_map(&self, map_fn: impl Fn(f32) -> f32) -> Option<Self> {
        Some(map_fn(*self))
    }
}

pub trait TryOpTo: Sized {
    // Zig: `comptime op_fn: *const fn(...)` — generic param preserves monomorphization.
    // `R` is method-generic (not trait-generic) so `TryOpTo` can appear as a
    // supertrait bound without committing to a result type.
    fn try_op_to<R, C>(&self, rhs: &Self, ctx: C, op_fn: impl Fn(C, f32, f32) -> R) -> Option<R>;
}

impl TryOpTo for CSSNumber {
    #[inline]
    fn try_op_to<R, C>(&self, rhs: &Self, ctx: C, op_fn: impl Fn(C, f32, f32) -> R) -> Option<R> {
        Some(op_fn(ctx, *self, *rhs))
    }
}

impl IsCompatible for CSSNumber {
    #[inline]
    fn is_compatible(&self, _: &crate::targets::Browsers) -> bool {
        true
    }
}

pub trait TryOp: Sized {
    // Zig: `comptime op_fn: *const fn(...)` — generic param preserves monomorphization.
    fn try_op<C>(&self, rhs: &Self, ctx: C, op_fn: impl Fn(C, f32, f32) -> f32) -> Option<Self>;
}

impl TryOp for CSSNumber {
    #[inline]
    fn try_op<C>(&self, rhs: &Self, ctx: C, op_fn: impl Fn(C, f32, f32) -> f32) -> Option<Self> {
        Some(op_fn(ctx, *self, *rhs))
    }
}

pub trait PartialCmp {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>;
}

#[inline]
pub(crate) fn partial_cmp_f32(lhs: f32, rhs: f32) -> Option<Ordering> {
    let lte = lhs <= rhs;
    let rte = lhs >= rhs;
    if !lte && !rte {
        return None;
    }
    if !lte && rte {
        return Some(Ordering::Greater);
    }
    if lte && !rte {
        return Some(Ordering::Less);
    }
    Some(Ordering::Equal)
}

impl PartialCmp for f32 {
    #[inline]
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        partial_cmp_f32(*self, *rhs)
    }
}
impl PartialCmp for CSSInteger {
    #[inline]
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(Ord::cmp(self, rhs))
    }
}

// ───────────────────────────────────────────────────────────────────────────────
// Zero / MulF32 / TryAdd — numeric protocol traits used by `DimensionPercentage<D>`
// and the `CalcValue` supertrait set. Formerly duplicated in `values::protocol`;
// that module now re-exports from here.
// ───────────────────────────────────────────────────────────────────────────────

/// `D::zero()` / `d.is_zero()` — additive identity.
pub trait Zero: Sized {
    fn zero() -> Self;
    fn is_zero(&self) -> bool;
}
/// `d.mul_f32(rhs)` — scalar multiplication.
pub trait MulF32: Sized {
    fn mul_f32(self, rhs: f32) -> Self;
}
/// `d.try_add(&rhs)` — same-unit addition, `None` if incompatible.
pub trait TryAdd: Sized {
    fn try_add(&self, rhs: &Self) -> Option<Self>;
}

impl MulF32 for CSSNumber {
    #[inline]
    fn mul_f32(self, rhs: f32) -> Self {
        self * rhs
    }
}

// ported from: src/css/generics.zig