dioxus-core-macro 0.7.4

Core macro for Dioxus Virtual DOM
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
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
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
//! This code mostly comes from idanarye/rust-typed-builder
//!
//! However, it has been adopted to fit the Dioxus Props builder pattern.
//!
//! For Dioxus, we make a few changes:
//! - [x] Automatically implement [`Into<Option>`] on the setters (IE the strip setter option)
//! - [x] Automatically implement a default of none for optional fields (those explicitly wrapped with [`Option<T>`])

use proc_macro2::TokenStream;

use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{parse::Error, PathArguments};

use quote::quote;
use syn::{parse_quote, GenericArgument, Ident, PathSegment, Type};

pub fn impl_my_derive(ast: &syn::DeriveInput) -> Result<TokenStream, Error> {
    let data = match &ast.data {
        syn::Data::Struct(data) => match &data.fields {
            syn::Fields::Named(fields) => {
                let struct_info = struct_info::StructInfo::new(ast, fields.named.iter())?;
                let builder_creation = struct_info.builder_creation_impl()?;
                let conversion_helper = struct_info.conversion_helper_impl()?;
                let fields = struct_info
                    .included_fields()
                    .map(|f| struct_info.field_impl(f))
                    .collect::<Result<Vec<_>, _>>()?;
                let extends = struct_info
                    .extend_fields()
                    .map(|f| struct_info.extends_impl(f))
                    .collect::<Result<Vec<_>, _>>()?;
                let fields = quote!(#(#fields)*).into_iter();
                let required_fields = struct_info
                    .included_fields()
                    .filter(|f| {
                        f.builder_attr.default.is_none() && f.builder_attr.extends.is_empty()
                    })
                    .map(|f| struct_info.required_field_impl(f))
                    .collect::<Result<Vec<_>, _>>()?;
                let build_method = struct_info.build_method_impl();

                quote! {
                    #builder_creation
                    #conversion_helper
                    #( #fields )*
                    #( #extends )*
                    #( #required_fields )*
                    #build_method
                }
            }
            syn::Fields::Unnamed(_) => {
                return Err(Error::new(
                    ast.span(),
                    "Props is not supported for tuple structs",
                ))
            }
            syn::Fields::Unit => {
                return Err(Error::new(
                    ast.span(),
                    "Props is not supported for unit structs",
                ))
            }
        },
        syn::Data::Enum(_) => {
            return Err(Error::new(ast.span(), "Props is not supported for enums"))
        }
        syn::Data::Union(_) => {
            return Err(Error::new(ast.span(), "Props is not supported for unions"))
        }
    };
    Ok(data)
}

mod util {
    use quote::ToTokens;

    pub fn path_to_single_string(path: &syn::Path) -> Option<String> {
        if path.leading_colon.is_some() {
            return None;
        }
        let mut it = path.segments.iter();
        let segment = it.next()?;
        if it.next().is_some() {
            // Multipart path
            return None;
        }
        if segment.arguments != syn::PathArguments::None {
            return None;
        }
        Some(segment.ident.to_string())
    }

    pub fn expr_to_single_string(expr: &syn::Expr) -> Option<String> {
        if let syn::Expr::Path(path) = expr {
            path_to_single_string(&path.path)
        } else {
            None
        }
    }

    pub fn ident_to_type(ident: syn::Ident) -> syn::Type {
        let mut path = syn::Path {
            leading_colon: None,
            segments: Default::default(),
        };
        path.segments.push(syn::PathSegment {
            ident,
            arguments: Default::default(),
        });
        syn::Type::Path(syn::TypePath { qself: None, path })
    }

    pub fn empty_type() -> syn::Type {
        syn::TypeTuple {
            paren_token: Default::default(),
            elems: Default::default(),
        }
        .into()
    }

    pub fn type_tuple(elems: impl Iterator<Item = syn::Type>) -> syn::TypeTuple {
        let mut result = syn::TypeTuple {
            paren_token: Default::default(),
            elems: elems.collect(),
        };
        if !result.elems.empty_or_trailing() {
            result.elems.push_punct(Default::default());
        }
        result
    }

    pub fn empty_type_tuple() -> syn::TypeTuple {
        syn::TypeTuple {
            paren_token: Default::default(),
            elems: Default::default(),
        }
    }

    pub fn make_punctuated_single<T, P: Default>(value: T) -> syn::punctuated::Punctuated<T, P> {
        let mut punctuated = syn::punctuated::Punctuated::new();
        punctuated.push(value);
        punctuated
    }

    pub fn modify_types_generics_hack<F>(
        ty_generics: &syn::TypeGenerics,
        mut mutator: F,
    ) -> syn::AngleBracketedGenericArguments
    where
        F: FnMut(&mut syn::punctuated::Punctuated<syn::GenericArgument, syn::token::Comma>),
    {
        let mut abga: syn::AngleBracketedGenericArguments =
            syn::parse(ty_generics.clone().into_token_stream().into()).unwrap_or_else(|_| {
                syn::AngleBracketedGenericArguments {
                    colon2_token: None,
                    lt_token: Default::default(),
                    args: Default::default(),
                    gt_token: Default::default(),
                }
            });
        mutator(&mut abga.args);
        abga
    }

    pub fn strip_raw_ident_prefix(mut name: String) -> String {
        if name.starts_with("r#") {
            name.replace_range(0..2, "");
        }
        name
    }
}

mod field_info {
    use crate::props::{looks_like_store_type, looks_like_write_type, type_from_inside_option};
    use proc_macro2::TokenStream;
    use quote::{format_ident, quote};
    use syn::spanned::Spanned;
    use syn::{parse::Error, punctuated::Punctuated};
    use syn::{parse_quote, Expr, Path};

    use super::util::{
        expr_to_single_string, ident_to_type, path_to_single_string, strip_raw_ident_prefix,
    };

    #[derive(Debug)]
    pub struct FieldInfo<'a> {
        pub ordinal: usize,
        pub name: &'a syn::Ident,
        pub generic_ident: syn::Ident,
        pub ty: &'a syn::Type,
        pub builder_attr: FieldBuilderAttr,
    }

    impl FieldInfo<'_> {
        pub fn new(
            ordinal: usize,
            field: &syn::Field,
            field_defaults: FieldBuilderAttr,
        ) -> Result<FieldInfo<'_>, Error> {
            if let Some(ref name) = field.ident {
                let mut builder_attr = field_defaults.with(&field.attrs)?;

                let strip_option_auto = builder_attr.strip_option
                    || !builder_attr.ignore_option && type_from_inside_option(&field.ty).is_some();

                // children field is automatically defaulted to an empty VNode unless it is marked as optional (in which case it defaults to None)
                if name == "children" && !strip_option_auto {
                    builder_attr.default =
                        Some(syn::parse(quote!(dioxus_core::VNode::empty()).into()).unwrap());
                }

                // String fields automatically use impl Display
                if field.ty == parse_quote!(::std::string::String)
                    || field.ty == parse_quote!(std::string::String)
                    || field.ty == parse_quote!(string::String)
                    || field.ty == parse_quote!(String)
                {
                    builder_attr.from_displayable = true;
                    // ToString is both more general and provides a more useful error message than From<String>. If the user tries to use `#[into]`, use ToString instead.
                    if builder_attr.auto_into {
                        builder_attr.auto_to_string = true;
                    }
                    builder_attr.auto_into = false;
                }

                // Write and Store fields automatically use impl Into
                if looks_like_write_type(&field.ty) || looks_like_store_type(&field.ty) {
                    builder_attr.auto_into = true;
                }

                // If this is a child field or extends, default to Default::default() if a default isn't set
                if !builder_attr.extends.is_empty() {
                    builder_attr.default.get_or_insert_with(|| {
                        syn::parse(quote!(::core::default::Default::default()).into()).unwrap()
                    });
                }

                // auto detect optional
                if !builder_attr.strip_option && strip_option_auto {
                    builder_attr.strip_option = true;
                    // only change the default if it isn't manually set above
                    builder_attr.default.get_or_insert_with(|| {
                        syn::parse(quote!(::core::default::Default::default()).into()).unwrap()
                    });
                }

                Ok(FieldInfo {
                    ordinal,
                    name,
                    generic_ident: syn::Ident::new(
                        &format!("__{}", strip_raw_ident_prefix(name.to_string())),
                        name.span(),
                    ),
                    ty: &field.ty,
                    builder_attr,
                })
            } else {
                Err(Error::new(field.span(), "Nameless field in struct"))
            }
        }

        pub fn generic_ty_param(&self) -> syn::GenericParam {
            syn::GenericParam::Type(self.generic_ident.clone().into())
        }

        pub fn type_ident(&self) -> syn::Type {
            ident_to_type(self.generic_ident.clone())
        }

        pub fn tuplized_type_ty_param(&self) -> syn::Type {
            let mut types = syn::punctuated::Punctuated::default();
            types.push(self.ty.clone());
            types.push_punct(Default::default());
            syn::TypeTuple {
                paren_token: Default::default(),
                elems: types,
            }
            .into()
        }

        pub fn extends_vec_ident(&self) -> Option<syn::Ident> {
            (!self.builder_attr.extends.is_empty()).then(|| {
                let ident = &self.name;
                format_ident!("__{ident}_attributes")
            })
        }
    }

    #[derive(Debug, Default, Clone)]
    pub struct FieldBuilderAttr {
        pub default: Option<syn::Expr>,
        pub docs: Vec<syn::Attribute>,
        pub skip: bool,
        pub auto_into: bool,
        pub auto_to_string: bool,
        pub from_displayable: bool,
        pub strip_option: bool,
        pub ignore_option: bool,
        pub extends: Vec<Path>,
    }

    impl FieldBuilderAttr {
        pub fn with(mut self, attrs: &[syn::Attribute]) -> Result<Self, Error> {
            let mut skip_tokens = None;
            for attr in attrs {
                if attr.path().is_ident("doc") {
                    self.docs.push(attr.clone());
                    continue;
                }

                if path_to_single_string(attr.path()).as_deref() != Some("props") {
                    continue;
                }

                match &attr.meta {
                    syn::Meta::List(list) => {
                        if list.tokens.is_empty() {
                            continue;
                        }
                    }
                    _ => {
                        continue;
                    }
                }

                let as_expr = attr.parse_args_with(
                    Punctuated::<Expr, syn::Token![,]>::parse_separated_nonempty,
                )?;

                for expr in as_expr.into_iter() {
                    self.apply_meta(expr)?;
                }

                // Stash its span for later (we don’t yet know if it’ll be an error)
                if self.skip && skip_tokens.is_none() {
                    skip_tokens = Some(attr.meta.clone());
                }
            }

            if self.skip && self.default.is_none() {
                return Err(Error::new_spanned(
                    skip_tokens.unwrap(),
                    "#[props(skip)] must be accompanied by default or default_code",
                ));
            }

            Ok(self)
        }

        pub fn apply_meta(&mut self, expr: syn::Expr) -> Result<(), Error> {
            match expr {
                // #[props(default = "...")]
                syn::Expr::Assign(assign) => {
                    let name = expr_to_single_string(&assign.left)
                        .ok_or_else(|| Error::new_spanned(&assign.left, "Expected identifier"))?;
                    match name.as_str() {
                        "extends" => {
                            if let syn::Expr::Path(path) = *assign.right {
                                self.extends.push(path.path);
                                Ok(())
                            } else {
                                Err(Error::new_spanned(
                                    assign.right,
                                    "Expected simple identifier",
                                ))
                            }
                        }
                        "default" => {
                            self.default = Some(*assign.right);
                            Ok(())
                        }
                        "default_code" => {
                            if let syn::Expr::Lit(syn::ExprLit {
                                lit: syn::Lit::Str(code),
                                ..
                            }) = *assign.right
                            {
                                use std::str::FromStr;
                                let tokenized_code = TokenStream::from_str(&code.value())?;
                                self.default = Some(
                                    syn::parse(tokenized_code.into())
                                        .map_err(|e| Error::new_spanned(code, format!("{e}")))?,
                                );
                            } else {
                                return Err(Error::new_spanned(assign.right, "Expected string"));
                            }
                            Ok(())
                        }
                        _ => Err(Error::new_spanned(
                            &assign,
                            format!("Unknown parameter {name:?}"),
                        )),
                    }
                }

                // #[props(default)]
                syn::Expr::Path(path) => {
                    let name = path_to_single_string(&path.path)
                        .ok_or_else(|| Error::new_spanned(&path, "Expected identifier"))?;
                    match name.as_str() {
                        "default" => {
                            self.default = Some(
                                syn::parse(quote!(::core::default::Default::default()).into())
                                    .unwrap(),
                            );
                            Ok(())
                        }

                        "optional" => {
                            self.default = Some(
                                syn::parse(quote!(::core::default::Default::default()).into())
                                    .unwrap(),
                            );
                            self.strip_option = true;
                            Ok(())
                        }

                        "extend" => {
                            self.extends.push(path.path);
                            Ok(())
                        }

                        _ => {
                            macro_rules! handle_fields {
                                ( $( $flag:expr, $field:ident, $already:expr; )* ) => {
                                    match name.as_str() {
                                        $(
                                            $flag => {
                                                if self.$field {
                                                    Err(Error::new(path.span(), concat!("Illegal setting - field is already ", $already)))
                                                } else {
                                                    self.$field = true;
                                                    Ok(())
                                                }
                                            }
                                        )*
                                        _ => Err(Error::new_spanned(
                                                &path,
                                                format!("Unknown setter parameter {:?}", name),
                                        ))
                                    }
                                }
                            }
                            handle_fields!(
                                "skip", skip, "skipped";
                                "into", auto_into, "calling into() on the argument";
                                "displayable", from_displayable, "calling to_string() on the argument";
                                "strip_option", strip_option, "putting the argument in Some(...)";
                            )
                        }
                    }
                }

                syn::Expr::Unary(syn::ExprUnary {
                    op: syn::UnOp::Not(_),
                    expr,
                    ..
                }) => {
                    if let syn::Expr::Path(path) = *expr {
                        let name = path_to_single_string(&path.path)
                            .ok_or_else(|| Error::new_spanned(&path, "Expected identifier"))?;
                        match name.as_str() {
                            "default" => {
                                self.default = None;
                                Ok(())
                            }
                            "skip" => {
                                self.skip = false;
                                Ok(())
                            }
                            "auto_into" => {
                                self.auto_into = false;
                                Ok(())
                            }
                            "displayable" => {
                                self.from_displayable = false;
                                Ok(())
                            }
                            "optional" => {
                                self.strip_option = false;
                                self.ignore_option = true;
                                Ok(())
                            }
                            _ => Err(Error::new_spanned(path, "Unknown setting".to_owned())),
                        }
                    } else {
                        Err(Error::new_spanned(
                            expr,
                            "Expected simple identifier".to_owned(),
                        ))
                    }
                }
                _ => Err(Error::new_spanned(expr, "Expected (<...>=<...>)")),
            }
        }
    }
}

fn type_from_inside_option(ty: &Type) -> Option<&Type> {
    let Type::Path(type_path) = ty else {
        return None;
    };

    if type_path.qself.is_some() {
        return None;
    }

    let path = &type_path.path;
    let seg = path.segments.last()?;

    // If the segment is a supported optional type, provide the inner type.
    // Return the inner type if the pattern is `Option<T>` or `ReadSignal<Option<T>>``
    if seg.ident == "ReadSignal" || seg.ident == "ReadOnlySignal" {
        // Get the inner type. E.g. the `u16` in `ReadSignal<u16>` or `Option` in `ReadSignal<Option<bool>>`
        let inner_type = extract_inner_type_from_segment(seg)?;
        let Type::Path(inner_path) = inner_type else {
            // If it isn't a path, the inner type isn't option
            return None;
        };

        // If we're entering an `Option`, we must get the innermost type
        let inner_seg = inner_path.path.segments.last()?;
        if inner_seg.ident == "Option" {
            // Get the innermost type.
            let innermost_type = extract_inner_type_from_segment(inner_seg)?;
            return Some(innermost_type);
        }
    } else if seg.ident == "Option" {
        // Grab the inner time. E.g. Option<u16>
        let inner_type = extract_inner_type_from_segment(seg)?;
        return Some(inner_type);
    }

    None
}

// Extract the inner type from a path segment.
fn extract_inner_type_from_segment(segment: &PathSegment) -> Option<&Type> {
    let PathArguments::AngleBracketed(generic_args) = &segment.arguments else {
        return None;
    };

    let GenericArgument::Type(final_type) = generic_args.args.first()? else {
        return None;
    };

    Some(final_type)
}

mod struct_info {
    use convert_case::{Case, Casing};
    use proc_macro2::TokenStream;
    use quote::{quote, ToTokens};
    use syn::parse::Error;
    use syn::punctuated::Punctuated;
    use syn::spanned::Spanned;
    use syn::{parse_quote, Expr, Ident};

    use crate::props::strip_option;

    use super::field_info::{FieldBuilderAttr, FieldInfo};
    use super::util::{
        empty_type, empty_type_tuple, expr_to_single_string, make_punctuated_single,
        modify_types_generics_hack, path_to_single_string, strip_raw_ident_prefix, type_tuple,
    };
    use super::{child_owned_type, looks_like_callback_type, looks_like_signal_type};

    #[derive(Debug)]
    pub struct StructInfo<'a> {
        pub vis: &'a syn::Visibility,
        pub name: &'a syn::Ident,
        pub generics: &'a syn::Generics,
        pub fields: Vec<FieldInfo<'a>>,

        pub builder_attr: TypeBuilderAttr,
        pub builder_name: syn::Ident,
        pub conversion_helper_trait_name: syn::Ident,
        #[allow(unused)]
        pub core: syn::Ident,
    }

    impl<'a> StructInfo<'a> {
        pub fn included_fields(&self) -> impl Iterator<Item = &FieldInfo<'a>> {
            self.fields.iter().filter(|f| !f.builder_attr.skip)
        }

        pub fn extend_fields(&self) -> impl Iterator<Item = &FieldInfo<'a>> {
            self.fields
                .iter()
                .filter(|f| !f.builder_attr.extends.is_empty())
        }

        pub fn new(
            ast: &'a syn::DeriveInput,
            fields: impl Iterator<Item = &'a syn::Field>,
        ) -> Result<StructInfo<'a>, Error> {
            let builder_attr = TypeBuilderAttr::new(&ast.attrs)?;
            let builder_name = strip_raw_ident_prefix(format!("{}Builder", ast.ident));
            Ok(StructInfo {
                vis: &ast.vis,
                name: &ast.ident,
                generics: &ast.generics,
                fields: fields
                    .enumerate()
                    .map(|(i, f)| FieldInfo::new(i, f, builder_attr.field_defaults.clone()))
                    .collect::<Result<_, _>>()?,
                builder_attr,
                builder_name: syn::Ident::new(&builder_name, ast.ident.span()),
                conversion_helper_trait_name: syn::Ident::new(
                    &format!("{builder_name}_Optional"),
                    ast.ident.span(),
                ),
                core: syn::Ident::new(&format!("{builder_name}_core"), ast.ident.span()),
            })
        }

        fn modify_generics<F: FnMut(&mut syn::Generics)>(&self, mut mutator: F) -> syn::Generics {
            let mut generics = self.generics.clone();
            mutator(&mut generics);
            generics
        }

        /// Checks if the props have any fields that should be owned by the child. For example, when converting T to `ReadSignal<T>`, the new signal should be owned by the child
        fn has_child_owned_fields(&self) -> bool {
            self.fields.iter().any(|f| child_owned_type(f.ty))
        }

        fn memoize_impl(&self) -> Result<TokenStream, Error> {
            // First check if there are any ReadSignal fields, if there are not, we can just use the partialEq impl
            let signal_fields: Vec<_> = self
                .included_fields()
                .filter(|f| looks_like_signal_type(f.ty))
                .map(|f| {
                    let name = f.name;
                    quote!(#name)
                })
                .collect();

            let move_signal_fields = quote! {
                trait NonPartialEq: Sized {
                    fn compare(&self, other: &Self) -> bool;
                }

                impl<T> NonPartialEq for &&T {
                    fn compare(&self, other: &Self) -> bool {
                        false
                    }
                }

                trait CanPartialEq: PartialEq {
                    fn compare(&self, other: &Self) -> bool;
                }

                impl<T: PartialEq> CanPartialEq for T {
                    fn compare(&self, other: &Self) -> bool {
                        self == other
                    }
                }

                // If they are equal, we don't need to rerun the component we can just update the existing signals
                #(
                    // Try to memo the signal
                    let field_eq = {
                        let old_value: &_ = &*#signal_fields.peek();
                        let new_value: &_ = &*new.#signal_fields.peek();
                        (&old_value).compare(&&new_value)
                    };
                    // Make the old fields point to the new fields
                    #signal_fields.point_to(new.#signal_fields).unwrap();
                    if !field_eq {
                        // If the fields are not equal, mark the signal as dirty to rerun any subscribers
                        (#signal_fields).mark_dirty();
                    }
                    // Move the old value back
                    self.#signal_fields = #signal_fields;
                )*
            };

            let event_handlers_fields: Vec<_> = self
                .included_fields()
                .filter(|f| looks_like_callback_type(f.ty))
                .collect();

            let regular_fields: Vec<_> = self
                .included_fields()
                .filter(|f| !looks_like_signal_type(f.ty) && !looks_like_callback_type(f.ty))
                .map(|f| {
                    let name = f.name;
                    quote!(#name)
                })
                .collect();

            let move_event_handlers: TokenStream = event_handlers_fields.iter().map(|field| {
                // If this is an optional event handler, we need to check if it's None before we try to update it
                let optional = strip_option(field.ty).is_some();
                let name = field.name;
                if optional {
                    quote! {
                        // If both event handler are Some, update them in place
                        if let (Some(old_handler), Some(new_handler)) = (self.#name.as_mut(), new.#name.as_ref()) {
                            old_handler.__point_to(new_handler);
                        }
                        // Otherwise just move the new handler into self
                        else {
                            self.#name = new.#name;
                        }
                    }
                } else {
                    quote! {
                        // Update the event handlers
                        self.#name.__point_to(&new.#name);
                    }
                }
            }).collect();

            // If there are signals, we automatically try to memoize the signals
            if !signal_fields.is_empty() {
                Ok(quote! {
                    // First check if the fields are equal. This will compare the signal fields by pointer
                    let exactly_equal = self == new;
                    if exactly_equal {
                        // If they are return early, they can be memoized without any changes
                        return true;
                    }

                    // If they are not, move the signal fields into self and check if they are equal now that the signal fields are equal
                    #(
                        let mut #signal_fields = self.#signal_fields;
                        self.#signal_fields = new.#signal_fields;
                    )*

                    // Then check if the fields are equal now that we know the signal fields are equal
                    // NOTE: we don't compare other fields individually because we want to let users opt-out of memoization for certain fields by implementing PartialEq themselves
                    let non_signal_fields_equal = self == new;

                    // If they are not equal, we need to move over all the fields that are not event handlers or signals to self
                    if !non_signal_fields_equal {
                        let new_clone = new.clone();
                        #(
                            self.#regular_fields = new_clone.#regular_fields;
                        )*
                    }
                    // Move any signal and event fields into their old container.
                    // We update signals and event handlers in place so that they are always up to date even if they were moved into a future in a previous render
                    #move_signal_fields
                    #move_event_handlers

                    non_signal_fields_equal
                })
            } else {
                Ok(quote! {
                    let equal = self == new;
                    // Move any signal and event fields into their old container.
                    #move_event_handlers
                    // If they are not equal, we need to move over all the fields that are not event handlers to self
                    if !equal {
                        let new_clone = new.clone();
                        #(
                            self.#regular_fields = new_clone.#regular_fields;
                        )*
                    }
                    equal
                })
            }
        }

        pub fn builder_creation_impl(&self) -> Result<TokenStream, Error> {
            let StructInfo {
                ref vis,
                ref name,
                ref builder_name,
                ..
            } = *self;

            let generics = self.generics.clone();
            let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
            let (_, b_initial_generics, _) = self.generics.split_for_impl();
            let all_fields_param = syn::GenericParam::Type(
                syn::Ident::new("TypedBuilderFields", proc_macro2::Span::call_site()).into(),
            );
            let b_generics = self.modify_generics(|g| {
                g.params.insert(0, all_fields_param.clone());
            });
            let empties_tuple = type_tuple(self.included_fields().map(|_| empty_type()));
            let generics_with_empty = modify_types_generics_hack(&b_initial_generics, |args| {
                args.insert(0, syn::GenericArgument::Type(empties_tuple.clone().into()));
            });
            let phantom_generics = self.generics.params.iter().filter_map(|param| match param {
                syn::GenericParam::Lifetime(lifetime) => {
                    let lifetime = &lifetime.lifetime;
                    Some(quote!(::core::marker::PhantomData<&#lifetime ()>))
                }
                syn::GenericParam::Type(ty) => {
                    let ty = &ty.ident;
                    Some(quote!(::core::marker::PhantomData<#ty>))
                }
                syn::GenericParam::Const(_cnst) => None,
            });
            let builder_method_doc = match self.builder_attr.builder_method_doc {
                Some(ref doc) => quote!(#doc),
                None => {
                    let doc = format!(
                        "
Create a builder for building `{name}`.
On the builder, call {setters} to set the values of the fields.
Finally, call `.build()` to create the instance of `{name}`.
                    ",
                        name = self.name,
                        setters = {
                            let mut result = String::new();
                            let mut is_first = true;
                            for field in self.included_fields() {
                                use std::fmt::Write;
                                if is_first {
                                    is_first = false;
                                } else {
                                    write!(&mut result, ", ").unwrap();
                                }
                                write!(&mut result, "`.{}(...)`", field.name).unwrap();
                                if field.builder_attr.default.is_some() {
                                    write!(&mut result, "(optional)").unwrap();
                                }
                            }
                            result
                        }
                    );
                    quote!(#doc)
                }
            };
            let builder_type_doc = if self.builder_attr.doc {
                match self.builder_attr.builder_type_doc {
                    Some(ref doc) => quote!(#[doc = #doc]),
                    None => {
                        let doc = format!(
                        "Builder for [`{name}`] instances.\n\nSee [`{name}::builder()`] for more info.",
                    );
                        quote!(#[doc = #doc])
                    }
                }
            } else {
                quote!(#[doc(hidden)])
            };

            let (_, _, b_generics_where_extras_predicates) = b_generics.split_for_impl();
            let mut b_generics_where: syn::WhereClause = syn::parse2(quote! {
                where Self: Clone
            })?;
            if let Some(predicates) = b_generics_where_extras_predicates {
                b_generics_where
                    .predicates
                    .extend(predicates.predicates.clone());
            }

            let memoize = self.memoize_impl()?;

            let global_fields = self
                .extend_fields()
                .map(|f| {
                    let name = f.extends_vec_ident();
                    let ty = f.ty;
                    quote!(#name: #ty)
                })
                .chain(
                    self.has_child_owned_fields()
                        .then(|| quote!(owner: dioxus_core::internal::generational_box::Owner)),
                );
            let global_fields_value = self
                .extend_fields()
                .map(|f| {
                    let name = f.extends_vec_ident();
                    quote!(#name: Vec::new())
                })
                .chain(self.has_child_owned_fields().then(
                    || quote!(owner: dioxus_core::internal::generational_box::Owner::default()),
                ));

            Ok(quote! {
                impl #impl_generics #name #ty_generics #where_clause {
                    #[doc = #builder_method_doc]
                    #[allow(dead_code, clippy::type_complexity)]
                    #vis fn builder() -> #builder_name #generics_with_empty {
                        #builder_name {
                            #(#global_fields_value,)*
                            fields: #empties_tuple,
                            _phantom: ::core::default::Default::default(),
                        }
                    }
                }

                #[must_use]
                #builder_type_doc
                #[allow(dead_code, non_camel_case_types, non_snake_case)]
                #vis struct #builder_name #b_generics {
                    #(#global_fields,)*
                    fields: #all_fields_param,
                    _phantom: (#( #phantom_generics ),*),
                }

                impl #impl_generics dioxus_core::Properties for #name #ty_generics
                #b_generics_where
                {
                    type Builder = #builder_name #generics_with_empty;
                    fn builder() -> Self::Builder {
                        #name::builder()
                    }
                    fn memoize(&mut self, new: &Self) -> bool {
                        #memoize
                    }
                }
            })
        }

        // TODO: once the proc-macro crate limitation is lifted, make this an util trait of this
        // crate.
        pub fn conversion_helper_impl(&self) -> Result<TokenStream, Error> {
            let trait_name = &self.conversion_helper_trait_name;
            Ok(quote! {
                #[doc(hidden)]
                #[allow(dead_code, non_camel_case_types, non_snake_case)]
                pub trait #trait_name<T> {
                    fn into_value<F: FnOnce() -> T>(self, default: F) -> T;
                }

                impl<T> #trait_name<T> for () {
                    fn into_value<F: FnOnce() -> T>(self, default: F) -> T {
                        default()
                    }
                }

                impl<T> #trait_name<T> for (T,) {
                    fn into_value<F: FnOnce() -> T>(self, _: F) -> T {
                        self.0
                    }
                }
            })
        }

        pub fn extends_impl(&self, field: &FieldInfo) -> Result<TokenStream, Error> {
            let StructInfo {
                ref builder_name, ..
            } = *self;

            let field_name = field.extends_vec_ident().unwrap();

            let descructuring = self.included_fields().map(|f| {
                let name = f.name;
                quote!(#name)
            });
            let reconstructing = self.included_fields().map(|f| f.name);

            let mut ty_generics: Vec<syn::GenericArgument> = self
                .generics
                .params
                .iter()
                .map(|generic_param| match generic_param {
                    syn::GenericParam::Type(type_param) => {
                        let ident = type_param.ident.clone();
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                    syn::GenericParam::Lifetime(lifetime_def) => {
                        syn::GenericArgument::Lifetime(lifetime_def.lifetime.clone())
                    }
                    syn::GenericParam::Const(const_param) => {
                        let ident = const_param.ident.clone();
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                })
                .collect();
            let mut target_generics_tuple = empty_type_tuple();
            let mut ty_generics_tuple = empty_type_tuple();
            let generics = self.modify_generics(|g| {
                let index_after_lifetime_in_generics = g
                    .params
                    .iter()
                    .filter(|arg| matches!(arg, syn::GenericParam::Lifetime(_)))
                    .count();
                for f in self.included_fields() {
                    if f.ordinal == field.ordinal {
                        g.params.insert(
                            index_after_lifetime_in_generics,
                            syn::GenericParam::Type(self.generic_builder_param(f)),
                        );
                        let generic_argument: syn::Type = f.type_ident();
                        ty_generics_tuple.elems.push_value(generic_argument.clone());
                        target_generics_tuple
                            .elems
                            .push_value(f.tuplized_type_ty_param());
                    } else {
                        g.params
                            .insert(index_after_lifetime_in_generics, f.generic_ty_param());
                        let generic_argument: syn::Type = f.type_ident();
                        ty_generics_tuple.elems.push_value(generic_argument.clone());
                        target_generics_tuple.elems.push_value(generic_argument);
                    }
                    ty_generics_tuple.elems.push_punct(Default::default());
                    target_generics_tuple.elems.push_punct(Default::default());
                }
            });
            let mut target_generics = ty_generics.clone();
            let index_after_lifetime_in_generics = target_generics
                .iter()
                .filter(|arg| matches!(arg, syn::GenericArgument::Lifetime(_)))
                .count();
            target_generics.insert(
                index_after_lifetime_in_generics,
                syn::GenericArgument::Type(target_generics_tuple.into()),
            );
            ty_generics.insert(
                index_after_lifetime_in_generics,
                syn::GenericArgument::Type(ty_generics_tuple.into()),
            );
            let (impl_generics, _, where_clause) = generics.split_for_impl();

            let forward_extended_fields = self.extend_fields().map(|f| {
                let name = f.extends_vec_ident();
                quote!(#name: self.#name)
            });

            let forward_owner = self
                .has_child_owned_fields()
                .then(|| quote!(owner: self.owner))
                .into_iter();

            let extends_impl = field.builder_attr.extends.iter().map(|path| {
                let name_str = path_to_single_string(path).unwrap();
                let camel_name = name_str.to_case(Case::UpperCamel);
                let marker_name = Ident::new(
                    format!("{}Extension", &camel_name).as_str(),
                    path.span(),
                );
                quote! {
                    #[allow(dead_code, non_camel_case_types, missing_docs)]
                    impl #impl_generics dioxus_elements::extensions::#marker_name for #builder_name < #( #ty_generics ),* > #where_clause {}
                }
            });

            Ok(quote! {
                #[allow(dead_code, non_camel_case_types, missing_docs)]
                impl #impl_generics dioxus_core::HasAttributes for #builder_name < #( #ty_generics ),* > #where_clause {
                    fn push_attribute<L>(
                        mut self,
                        ____name: &'static str,
                        ____ns: Option<&'static str>,
                        ____attr: impl dioxus_core::IntoAttributeValue<L>,
                        ____volatile: bool
                    ) -> Self {
                        let ( #(#descructuring,)* ) = self.fields;
                        self.#field_name.push(
                            dioxus_core::Attribute::new(
                                ____name,
                                dioxus_core::IntoAttributeValue::<L>::into_value(____attr),
                                ____ns,
                                ____volatile,
                            )
                        );
                        #builder_name {
                            #(#forward_extended_fields,)*
                            #(#forward_owner,)*
                            fields: ( #(#reconstructing,)* ),
                            _phantom: self._phantom,
                        }
                    }
                }

                #(#extends_impl)*
            })
        }

        pub fn field_impl(&self, field: &FieldInfo) -> Result<TokenStream, Error> {
            let FieldInfo {
                name: field_name, ..
            } = field;
            if *field_name == "key" {
                return Err(Error::new_spanned(field_name, "Naming a prop `key` is not allowed because the name can conflict with the built in key attribute. See https://dioxuslabs.com/learn/0.7/essentials/ui/iteration for more information about keys"));
            }
            let StructInfo {
                ref builder_name, ..
            } = *self;

            let descructuring = self.included_fields().map(|f| {
                if f.ordinal == field.ordinal {
                    quote!(_)
                } else {
                    let name = f.name;
                    quote!(#name)
                }
            });
            let reconstructing = self.included_fields().map(|f| f.name);

            let FieldInfo {
                name: field_name,
                ty: field_type,
                ..
            } = field;
            let mut ty_generics: Vec<syn::GenericArgument> = self
                .generics
                .params
                .iter()
                .map(|generic_param| match generic_param {
                    syn::GenericParam::Type(type_param) => {
                        let ident = type_param.ident.clone();
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                    syn::GenericParam::Lifetime(lifetime_def) => {
                        syn::GenericArgument::Lifetime(lifetime_def.lifetime.clone())
                    }
                    syn::GenericParam::Const(const_param) => {
                        let ident = const_param.ident.clone();
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                })
                .collect();
            let mut target_generics_tuple = empty_type_tuple();
            let mut ty_generics_tuple = empty_type_tuple();
            let generics = self.modify_generics(|g| {
                let index_after_lifetime_in_generics = g
                    .params
                    .iter()
                    .filter(|arg| matches!(arg, syn::GenericParam::Lifetime(_)))
                    .count();
                for f in self.included_fields() {
                    if f.ordinal == field.ordinal {
                        ty_generics_tuple.elems.push_value(empty_type());
                        target_generics_tuple
                            .elems
                            .push_value(f.tuplized_type_ty_param());
                    } else {
                        g.params
                            .insert(index_after_lifetime_in_generics, f.generic_ty_param());
                        let generic_argument: syn::Type = f.type_ident();
                        ty_generics_tuple.elems.push_value(generic_argument.clone());
                        target_generics_tuple.elems.push_value(generic_argument);
                    }
                    ty_generics_tuple.elems.push_punct(Default::default());
                    target_generics_tuple.elems.push_punct(Default::default());
                }
            });
            let mut target_generics = ty_generics.clone();
            let index_after_lifetime_in_generics = target_generics
                .iter()
                .filter(|arg| matches!(arg, syn::GenericArgument::Lifetime(_)))
                .count();
            target_generics.insert(
                index_after_lifetime_in_generics,
                syn::GenericArgument::Type(target_generics_tuple.into()),
            );
            ty_generics.insert(
                index_after_lifetime_in_generics,
                syn::GenericArgument::Type(ty_generics_tuple.into()),
            );

            let (impl_generics, _, where_clause) = generics.split_for_impl();
            let docs = &field.builder_attr.docs;

            let arg_type = field_type;
            // If the field is auto_into, we need to add a generic parameter to the builder for specialization
            let mut marker = None;
            let (arg_type, arg_expr) = if child_owned_type(arg_type) {
                let marker_ident = syn::Ident::new("__Marker", proc_macro2::Span::call_site());
                marker = Some(marker_ident.clone());
                (
                    quote!(impl dioxus_core::SuperInto<#arg_type, #marker_ident>),
                    // If this looks like a signal type, we automatically convert it with SuperInto and use the props struct as the owner
                    quote!(dioxus_core::with_owner(self.owner.clone(), move || dioxus_core::SuperInto::super_into(#field_name))),
                )
            } else if field.builder_attr.auto_into || field.builder_attr.strip_option {
                let marker_ident = syn::Ident::new("__Marker", proc_macro2::Span::call_site());
                marker = Some(marker_ident.clone());
                (
                    quote!(impl dioxus_core::SuperInto<#arg_type, #marker_ident>),
                    quote!(dioxus_core::SuperInto::super_into(#field_name)),
                )
            } else if field.builder_attr.from_displayable {
                (
                    quote!(impl ::core::fmt::Display),
                    quote!(#field_name.to_string()),
                )
            } else {
                (quote!(#arg_type), quote!(#field_name))
            };

            let repeated_fields_error_type_name = syn::Ident::new(
                &format!(
                    "{}_Error_Repeated_field_{}",
                    builder_name,
                    strip_raw_ident_prefix(field_name.to_string())
                ),
                builder_name.span(),
            );
            let repeated_fields_error_message = format!("Repeated field {field_name}");

            let forward_fields = self
                .extend_fields()
                .map(|f| {
                    let name = f.extends_vec_ident();
                    quote!(#name: self.#name)
                })
                .chain(
                    self.has_child_owned_fields()
                        .then(|| quote!(owner: self.owner)),
                );

            Ok(quote! {
                #[allow(dead_code, non_camel_case_types, missing_docs)]
                impl #impl_generics #builder_name < #( #ty_generics ),* > #where_clause {
                    #( #docs )*
                    #[allow(clippy::type_complexity)]
                    pub fn #field_name < #marker > (self, #field_name: #arg_type) -> #builder_name < #( #target_generics ),* > {
                        let #field_name = (#arg_expr,);
                        let ( #(#descructuring,)* ) = self.fields;
                        #builder_name {
                            #(#forward_fields,)*
                            fields: ( #(#reconstructing,)* ),
                            _phantom: self._phantom,
                        }
                    }
                }
                #[doc(hidden)]
                #[allow(dead_code, non_camel_case_types, non_snake_case)]
                pub enum #repeated_fields_error_type_name {}
                #[doc(hidden)]
                #[allow(dead_code, non_camel_case_types, missing_docs)]
                impl #impl_generics #builder_name < #( #target_generics ),* > #where_clause {
                    #[deprecated(
                        note = #repeated_fields_error_message
                    )]
                    #[allow(clippy::type_complexity)]
                    pub fn #field_name< #marker > (self, _: #repeated_fields_error_type_name) -> #builder_name < #( #target_generics ),* > {
                        self
                    }
                }
            })
        }

        pub fn required_field_impl(&self, field: &FieldInfo) -> Result<TokenStream, Error> {
            let StructInfo {
                ref name,
                ref builder_name,
                ..
            } = self;

            let FieldInfo {
                name: ref field_name,
                ..
            } = field;
            let mut builder_generics: Vec<syn::GenericArgument> = self
                .generics
                .params
                .iter()
                .map(|generic_param| match generic_param {
                    syn::GenericParam::Type(type_param) => {
                        let ident = &type_param.ident;
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                    syn::GenericParam::Lifetime(lifetime_def) => {
                        syn::GenericArgument::Lifetime(lifetime_def.lifetime.clone())
                    }
                    syn::GenericParam::Const(const_param) => {
                        let ident = &const_param.ident;
                        syn::parse(quote!(#ident).into()).unwrap()
                    }
                })
                .collect();
            let mut builder_generics_tuple = empty_type_tuple();
            let generics = self.modify_generics(|g| {
                let index_after_lifetime_in_generics = g
                    .params
                    .iter()
                    .filter(|arg| matches!(arg, syn::GenericParam::Lifetime(_)))
                    .count();
                for f in self.included_fields() {
                    if f.builder_attr.default.is_some() {
                        // `f` is not mandatory - it does not have it's own fake `build` method, so `field` will need
                        // to warn about missing `field` whether or not `f` is set.
                        assert!(
                            f.ordinal != field.ordinal,
                            "`required_field_impl` called for optional field {}",
                            field.name
                        );
                        g.params
                            .insert(index_after_lifetime_in_generics, f.generic_ty_param());
                        builder_generics_tuple.elems.push_value(f.type_ident());
                    } else if f.ordinal < field.ordinal {
                        // Only add a `build` method that warns about missing `field` if `f` is set. If `f` is not set,
                        // `f`'s `build` method will warn, since it appears earlier in the argument list.
                        builder_generics_tuple
                            .elems
                            .push_value(f.tuplized_type_ty_param());
                    } else if f.ordinal == field.ordinal {
                        builder_generics_tuple.elems.push_value(empty_type());
                    } else {
                        // `f` appears later in the argument list after `field`, so if they are both missing we will
                        // show a warning for `field` and not for `f` - which means this warning should appear whether
                        // or not `f` is set.
                        g.params
                            .insert(index_after_lifetime_in_generics, f.generic_ty_param());
                        builder_generics_tuple.elems.push_value(f.type_ident());
                    }

                    builder_generics_tuple.elems.push_punct(Default::default());
                }
            });

            let index_after_lifetime_in_generics = builder_generics
                .iter()
                .filter(|arg| matches!(arg, syn::GenericArgument::Lifetime(_)))
                .count();
            builder_generics.insert(
                index_after_lifetime_in_generics,
                syn::GenericArgument::Type(builder_generics_tuple.into()),
            );
            let (impl_generics, _, where_clause) = generics.split_for_impl();
            let (_, ty_generics, _) = self.generics.split_for_impl();

            let early_build_error_type_name = syn::Ident::new(
                &format!(
                    "{}_Error_Missing_required_field_{}",
                    builder_name,
                    strip_raw_ident_prefix(field_name.to_string())
                ),
                builder_name.span(),
            );
            let early_build_error_message = format!("Missing required field {field_name}");

            Ok(quote! {
                #[doc(hidden)]
                #[allow(dead_code, non_camel_case_types, non_snake_case)]
                pub enum #early_build_error_type_name {}
                #[doc(hidden)]
                #[allow(dead_code, non_camel_case_types, missing_docs, clippy::panic)]
                impl #impl_generics #builder_name < #( #builder_generics ),* > #where_clause {
                    #[deprecated(
                        note = #early_build_error_message
                    )]
                    pub fn build(self, _: #early_build_error_type_name) -> #name #ty_generics {
                        panic!()
                    }
                }
            })
        }

        fn generic_builder_param(&self, field: &FieldInfo) -> syn::TypeParam {
            let trait_ref = syn::TraitBound {
                paren_token: None,
                lifetimes: None,
                modifier: syn::TraitBoundModifier::None,
                path: syn::PathSegment {
                    ident: self.conversion_helper_trait_name.clone(),
                    arguments: syn::PathArguments::AngleBracketed(
                        syn::AngleBracketedGenericArguments {
                            colon2_token: None,
                            lt_token: Default::default(),
                            args: make_punctuated_single(syn::GenericArgument::Type(
                                field.ty.clone(),
                            )),
                            gt_token: Default::default(),
                        },
                    ),
                }
                .into(),
            };
            let mut generic_param: syn::TypeParam = field.generic_ident.clone().into();
            generic_param.bounds.push(trait_ref.into());
            generic_param
        }

        pub fn build_method_impl(&self) -> TokenStream {
            let StructInfo {
                ref name,
                ref builder_name,
                ..
            } = *self;

            let generics = self.modify_generics(|g| {
                let index_after_lifetime_in_generics = g
                    .params
                    .iter()
                    .filter(|arg| matches!(arg, syn::GenericParam::Lifetime(_)))
                    .count();
                for field in self.included_fields() {
                    if field.builder_attr.default.is_some() {
                        let generic_param = self.generic_builder_param(field);
                        g.params
                            .insert(index_after_lifetime_in_generics, generic_param.into());
                    }
                }
            });
            let (impl_generics, _, _) = generics.split_for_impl();

            let (original_impl_generics, ty_generics, where_clause) =
                self.generics.split_for_impl();

            let modified_ty_generics = modify_types_generics_hack(&ty_generics, |args| {
                args.insert(
                    0,
                    syn::GenericArgument::Type(
                        type_tuple(self.included_fields().map(|field| {
                            if field.builder_attr.default.is_some() {
                                field.type_ident()
                            } else {
                                field.tuplized_type_ty_param()
                            }
                        }))
                        .into(),
                    ),
                );
            });

            let descructuring = self.included_fields().map(|f| f.name);

            let helper_trait_name = &self.conversion_helper_trait_name;
            // The default of a field can refer to earlier-defined fields, which we handle by
            // writing out a bunch of `let` statements first, which can each refer to earlier ones.
            // This means that field ordering may actually be significant, which isn’t ideal. We could
            // relax that restriction by calculating a DAG of field default dependencies and
            // reordering based on that, but for now this much simpler thing is a reasonable approach.
            let assignments = self.fields.iter().map(|field| {
                let name = &field.name;
                if let Some(extends_vec) = field.extends_vec_ident() {
                    quote!{
                        let mut #name = #helper_trait_name::into_value(#name, || ::core::default::Default::default());
                        #name.extend(self.#extends_vec);
                    }
                } else if let Some(ref default) = field.builder_attr.default {
                    // If field has `into`, apply it to the default value.
                    // Ignore any blank defaults as it causes type inference errors.
                    let is_default = *default == parse_quote!(::core::default::Default::default());
                    // If this is a signal type, we use super_into and the props struct as the owner
                    let is_child_owned_type = child_owned_type(field.ty);


                    let body = if !is_default {
                        if is_child_owned_type {
                            quote!{ dioxus_core::SuperInto::super_into(#default) }
                        } else if field.builder_attr.auto_into {
                            quote!{ (#default).into() }
                        } else if field.builder_attr.auto_to_string {
                            quote!{ (#default).to_string() }
                        } else {
                            default.to_token_stream()
                        }
                    } else {
                        default.to_token_stream()
                    };

                    if field.builder_attr.skip {
                        quote!(let #name = #body;)
                    } else if is_child_owned_type {
                        quote!(let #name = #helper_trait_name::into_value(#name, || dioxus_core::with_owner(self.owner.clone(), move || #body));)
                    } else {
                        quote!(let #name = #helper_trait_name::into_value(#name, || #body);)
                    }
                } else {
                    quote!(let #name = #name.0;)
                }
            });
            let field_names = self.fields.iter().map(|field| field.name);
            let doc = if self.builder_attr.doc {
                match self.builder_attr.build_method_doc {
                    Some(ref doc) => quote!(#[doc = #doc]),
                    None => {
                        // I’d prefer “a” or “an” to “its”, but determining which is grammatically
                        // correct is roughly impossible.
                        let doc =
                            format!("Finalize the builder and create its [`{name}`] instance");
                        quote!(#[doc = #doc])
                    }
                }
            } else {
                quote!()
            };

            if self.has_child_owned_fields() {
                let name = Ident::new(&format!("{}WithOwner", name), name.span());
                let original_name = &self.name;
                let vis = &self.vis;
                let generics_with_bounds = &self.generics;
                let where_clause = &self.generics.where_clause;

                quote! {
                    #[doc(hidden)]
                    #[allow(dead_code, non_camel_case_types, missing_docs)]
                    #[derive(Clone)]
                    #vis struct #name #generics_with_bounds #where_clause {
                        inner: #original_name #ty_generics,
                        owner: dioxus_core::internal::generational_box::Owner,
                    }

                    impl #original_impl_generics PartialEq for #name #ty_generics #where_clause {
                        fn eq(&self, other: &Self) -> bool {
                            self.inner.eq(&other.inner)
                        }
                    }

                    impl #original_impl_generics #name #ty_generics #where_clause {
                        /// Create a component from the props.
                        pub fn into_vcomponent<M: 'static>(
                            self,
                            render_fn: impl dioxus_core::ComponentFunction<#original_name #ty_generics, M>,
                        ) -> dioxus_core::VComponent {
                            use dioxus_core::ComponentFunction;
                            let component_name = ::std::any::type_name_of_val(&render_fn);
                            dioxus_core::VComponent::new(move |wrapper: Self| render_fn.rebuild(wrapper.inner), self, component_name)
                        }
                    }

                    impl #original_impl_generics dioxus_core::Properties for #name #ty_generics #where_clause {
                        type Builder = ();
                        fn builder() -> Self::Builder {
                            unreachable!()
                        }
                        fn memoize(&mut self, new: &Self) -> bool {
                            self.inner.memoize(&new.inner)
                        }
                    }

                    #[allow(dead_code, non_camel_case_types, missing_docs)]
                    impl #impl_generics #builder_name #modified_ty_generics #where_clause {
                        #doc
                        pub fn build(self) -> #name #ty_generics {
                            let ( #(#descructuring,)* ) = self.fields;
                            #( #assignments )*
                            #name {
                                inner: #original_name {
                                    #( #field_names ),*
                                },
                                owner: self.owner,
                            }
                        }
                    }
                }
            } else {
                quote!(
                    #[allow(dead_code, non_camel_case_types, missing_docs)]
                    impl #impl_generics #builder_name #modified_ty_generics #where_clause {
                        #doc
                        pub fn build(self) -> #name #ty_generics {
                            let ( #(#descructuring,)* ) = self.fields;
                            #( #assignments )*
                            #name {
                                #( #field_names ),*
                            }
                        }
                    }
                )
            }
        }
    }

    #[derive(Debug, Default)]
    pub struct TypeBuilderAttr {
        /// Whether to show docs for the `TypeBuilder` type (rather than hiding them).
        pub doc: bool,

        /// Docs on the `Type::builder()` method.
        pub builder_method_doc: Option<syn::Expr>,

        /// Docs on the `TypeBuilder` type. Specifying this implies `doc`, but you can just specify
        /// `doc` instead and a default value will be filled in here.
        pub builder_type_doc: Option<syn::Expr>,

        /// Docs on the `TypeBuilder.build()` method. Specifying this implies `doc`, but you can just
        /// specify `doc` instead and a default value will be filled in here.
        pub build_method_doc: Option<syn::Expr>,

        pub field_defaults: FieldBuilderAttr,
    }

    impl TypeBuilderAttr {
        pub fn new(attrs: &[syn::Attribute]) -> Result<TypeBuilderAttr, Error> {
            let mut result = TypeBuilderAttr::default();
            for attr in attrs {
                if path_to_single_string(attr.path()).as_deref() != Some("builder") {
                    continue;
                }

                match &attr.meta {
                    syn::Meta::List(list) => {
                        if list.tokens.is_empty() {
                            continue;
                        }
                    }
                    _ => {
                        continue;
                    }
                }

                let as_expr = attr.parse_args_with(
                    Punctuated::<Expr, syn::Token![,]>::parse_separated_nonempty,
                )?;

                for expr in as_expr.into_iter() {
                    result.apply_meta(expr)?;
                }
            }

            Ok(result)
        }

        fn apply_meta(&mut self, expr: syn::Expr) -> Result<(), Error> {
            match expr {
                syn::Expr::Assign(assign) => {
                    let name = expr_to_single_string(&assign.left)
                        .ok_or_else(|| Error::new_spanned(&assign.left, "Expected identifier"))?;
                    match name.as_str() {
                        "builder_method_doc" => {
                            self.builder_method_doc = Some(*assign.right);
                            Ok(())
                        }
                        "builder_type_doc" => {
                            self.builder_type_doc = Some(*assign.right);
                            self.doc = true;
                            Ok(())
                        }
                        "build_method_doc" => {
                            self.build_method_doc = Some(*assign.right);
                            self.doc = true;
                            Ok(())
                        }
                        _ => Err(Error::new_spanned(
                            &assign,
                            format!("Unknown parameter {name:?}"),
                        )),
                    }
                }
                syn::Expr::Path(path) => {
                    let name = path_to_single_string(&path.path)
                        .ok_or_else(|| Error::new_spanned(&path, "Expected identifier"))?;
                    match name.as_str() {
                        "doc" => {
                            self.doc = true;
                            Ok(())
                        }
                        _ => Err(Error::new_spanned(
                            &path,
                            format!("Unknown parameter {name:?}"),
                        )),
                    }
                }
                syn::Expr::Call(call) => {
                    let subsetting_name = if let syn::Expr::Path(path) = &*call.func {
                        path_to_single_string(&path.path)
                    } else {
                        None
                    }
                    .ok_or_else(|| {
                        let call_func = &call.func;
                        let call_func = quote!(#call_func);
                        Error::new_spanned(
                            &call.func,
                            format!("Illegal builder setting group {call_func}"),
                        )
                    })?;
                    match subsetting_name.as_str() {
                        "field_defaults" => {
                            for arg in call.args {
                                self.field_defaults.apply_meta(arg)?;
                            }
                            Ok(())
                        }
                        _ => Err(Error::new_spanned(
                            &call.func,
                            format!("Illegal builder setting group name {subsetting_name}"),
                        )),
                    }
                }
                _ => Err(Error::new_spanned(expr, "Expected (<...>=<...>)")),
            }
        }
    }
}

/// A helper function for paring types with a single generic argument.
fn extract_base_type_without_generics(ty: &Type) -> Option<syn::Path> {
    let Type::Path(ty) = ty else {
        return None;
    };
    if ty.qself.is_some() {
        return None;
    }

    let path = &ty.path;

    let mut path_segments_without_generics = Vec::new();

    let mut generic_arg_count = 0;

    for segment in &path.segments {
        let mut segment = segment.clone();
        match segment.arguments {
            PathArguments::AngleBracketed(_) => generic_arg_count += 1,
            PathArguments::Parenthesized(_) => {
                return None;
            }
            _ => {}
        }
        segment.arguments = syn::PathArguments::None;
        path_segments_without_generics.push(segment);
    }

    // If there is more than the type and the single generic argument, it doesn't look like the type we want
    if generic_arg_count > 2 {
        return None;
    }

    let path_without_generics = syn::Path {
        leading_colon: None,
        segments: Punctuated::from_iter(path_segments_without_generics),
    };

    Some(path_without_generics)
}

/// Returns the type inside the Option wrapper if it exists
fn strip_option(type_: &Type) -> Option<Type> {
    if let Type::Path(ty) = &type_ {
        let mut segments_iter = ty.path.segments.iter().peekable();
        // Strip any leading std||core::option:: prefix
        let allowed_segments: &[&[&str]] = &[&["std", "core"], &["option"]];
        let mut allowed_segments_iter = allowed_segments.iter();
        while let Some(segment) = segments_iter.peek() {
            let Some(allowed_segments) = allowed_segments_iter.next() else {
                break;
            };
            if !allowed_segments.contains(&segment.ident.to_string().as_str()) {
                break;
            }
            segments_iter.next();
        }
        // The last segment should be Option
        let option_segment = segments_iter.next()?;
        if option_segment.ident == "Option" && segments_iter.next().is_none() {
            // It should have a single generic argument
            if let PathArguments::AngleBracketed(generic_arg) = &option_segment.arguments {
                if let Some(syn::GenericArgument::Type(ty)) = generic_arg.args.first() {
                    return Some(ty.clone());
                }
            }
        }
    }
    None
}

/// Remove the Option wrapper from a type
fn remove_option_wrapper(type_: Type) -> Type {
    strip_option(&type_).unwrap_or(type_)
}

/// Check if a type should be owned by the child component after conversion
fn child_owned_type(ty: &Type) -> bool {
    looks_like_signal_type(ty) || looks_like_write_type(ty) || looks_like_callback_type(ty)
}

/// Check if the path without generics matches the type we are looking for
fn last_segment_matches(ty: &Type, expected: &Ident) -> bool {
    extract_base_type_without_generics(ty).is_some_and(|path_without_generics| {
        path_without_generics
            .segments
            .last()
            .is_some_and(|seg| seg.ident == *expected)
    })
}

fn looks_like_signal_type(ty: &Type) -> bool {
    last_segment_matches(ty, &parse_quote!(ReadOnlySignal))
        || last_segment_matches(ty, &parse_quote!(ReadSignal))
}

fn looks_like_write_type(ty: &Type) -> bool {
    last_segment_matches(ty, &parse_quote!(WriteSignal))
}

fn looks_like_store_type(ty: &Type) -> bool {
    last_segment_matches(ty, &parse_quote!(Store))
        || last_segment_matches(ty, &parse_quote!(ReadStore))
        || last_segment_matches(ty, &parse_quote!(WriteStore))
}

fn looks_like_callback_type(ty: &Type) -> bool {
    let type_without_option = remove_option_wrapper(ty.clone());
    last_segment_matches(&type_without_option, &parse_quote!(EventHandler))
        || last_segment_matches(&type_without_option, &parse_quote!(Callback))
}

#[test]
fn test_looks_like_type() {
    assert!(!looks_like_signal_type(&parse_quote!(
        Option<ReadOnlySignal<i32>>
    )));
    assert!(looks_like_signal_type(&parse_quote!(ReadOnlySignal<i32>)));
    assert!(looks_like_signal_type(
        &parse_quote!(ReadOnlySignal<i32, SyncStorage>)
    ));
    assert!(looks_like_signal_type(&parse_quote!(
        ReadOnlySignal<Option<i32>, UnsyncStorage>
    )));

    assert!(!looks_like_signal_type(&parse_quote!(
        Option<ReadSignal<i32>>
    )));
    assert!(looks_like_signal_type(&parse_quote!(ReadSignal<i32>)));
    assert!(looks_like_signal_type(
        &parse_quote!(ReadSignal<i32, SyncStorage>)
    ));
    assert!(looks_like_signal_type(&parse_quote!(
        ReadSignal<Option<i32>, UnsyncStorage>
    )));

    assert!(looks_like_callback_type(&parse_quote!(
        Option<EventHandler>
    )));
    assert!(looks_like_callback_type(&parse_quote!(
        std::option::Option<EventHandler<i32>>
    )));
    assert!(looks_like_callback_type(&parse_quote!(
        Option<EventHandler<MouseEvent>>
    )));

    assert!(looks_like_callback_type(&parse_quote!(EventHandler<i32>)));
    assert!(looks_like_callback_type(&parse_quote!(EventHandler)));

    assert!(looks_like_callback_type(&parse_quote!(Callback<i32>)));
    assert!(looks_like_callback_type(&parse_quote!(Callback<i32, u32>)));
}

#[test]
fn test_remove_option_wrapper() {
    let type_without_option = remove_option_wrapper(parse_quote!(Option<i32>));
    assert_eq!(type_without_option, parse_quote!(i32));

    let type_without_option = remove_option_wrapper(parse_quote!(Option<Option<i32>>));
    assert_eq!(type_without_option, parse_quote!(Option<i32>));

    let type_without_option = remove_option_wrapper(parse_quote!(Option<Option<Option<i32>>>));
    assert_eq!(type_without_option, parse_quote!(Option<Option<i32>>));
}