arcature 0.1.2

Arcature: an opinionated full-stack Rust web framework. One package, batteries included.
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
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
//! What each `arc make:<kind>` writes, and where.
//!
//! One function per kind would spread twenty-two near-identical decisions
//! across twenty-two places, so instead [`plan`] answers all of them: the
//! destination path, the file body, whether a sibling `mod.rs` should learn
//! about the new file, and any follow-up the generator cannot do for the
//! developer.
//!
//! # Scaffolds that compile
//!
//! Every blueprint here produces a file that compiles as written, with two
//! kinds of exception.
//!
//! `policy` and `listener` bind to a type the developer chooses -- the model
//! a policy guards, the event a listener reacts to -- and no generator can
//! guess it. Those two name a placeholder and say so at the top of the file,
//! because a scaffold that silently omits the binding is worse than one that
//! fails to compile until it is pointed somewhere real.
//!
//! `notification`, `upload` and `auth` compile only once the application
//! enables the features their imports live behind, which a fresh `arc new`
//! does not. The alternative was to have the generator edit `Cargo.toml`, and
//! a generator that reaches into the manifest is a generator that can break a
//! build it was never pointed at. Each artifact's notes say which feature and
//! why, and adding it is one line.
//!
//! `auth` is also the one kind that does not scaffold its own screens. What a
//! login form looks like is an application's answer, and a generator that
//! guesses Inertia or a server-rendered view is wrong for half of them; what a
//! login *handler* does is not, and getting it wrong leaks who has an account.
//! So the dangerous half is written and the visible half is left to
//! `arc make:page` and `arc make:view`.

use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use super::name::{ArtifactName, pluralize, to_pascal_case};
use crate::cli::parser::MakeKind;

/// One file a generator is about to write, plus what to do around it.
#[derive(Debug, Clone)]
pub struct Artifact {
    /// Where the file goes, relative to the project root.
    pub path: PathBuf,
    /// The rendered file body.
    pub contents: String,
    /// Whether the sibling `mod.rs` should gain a `pub mod` line. False only
    /// for `tests/`, where each file is its own crate and no `mod.rs` exists.
    pub register_module: bool,
    /// Follow-up the generator deliberately left to the developer.
    pub notes: Vec<String>,
}

/// Decide everything about the file `kind` + `name` produces.
///
/// For a kind that writes more than one file this is the *primary* artifact
/// -- the one whose path names the thing. Use [`plan_all`] to get the rest.
#[must_use]
pub fn plan(kind: MakeKind, name: &ArtifactName) -> Artifact {
    match kind {
        MakeKind::Module => module_root(name),
        MakeKind::Controller => rust(name, "app/controllers", "Controller", controller),
        MakeKind::Model => rust(name, "app/models", "", model),
        MakeKind::Migration => migration(name),
        MakeKind::Request => rust(name, "app/requests", "Request", request),
        MakeKind::Resource => rust(name, "app/resources", "Resource", resource),
        MakeKind::Policy => policy(name),
        MakeKind::Service => rust(name, "app/services", "Service", service),
        MakeKind::Job => rust(name, "app/jobs", "", job),
        MakeKind::Event => rust(name, "app/events", "", event),
        MakeKind::Listener => listener(name),
        MakeKind::Middleware => rust(name, "app/middleware", "", middleware),
        MakeKind::Command => rust(name, "app/commands", "", command),
        MakeKind::Page => rust(name, "app/pages", "Page", page),
        MakeKind::Test => test(name),
        MakeKind::Factory => rust(name, "database/factories", "Factory", factory),
        MakeKind::Seeder => rust(name, "database/seeders", "Seeder", seeder),
        MakeKind::Notification => notification(name),
        MakeKind::Mail => rust(name, "app/mail", "", mailable),
        MakeKind::View => rust(name, "app/views", "View", view_struct),
        MakeKind::Upload => upload(name),
        MakeKind::Auth => account(name),
    }
}

/// Every file `kind` + `name` produces, primary artifact first.
///
/// Nineteen of the twenty-two kinds write one file, and [`plan`] is the older,
/// narrower way to ask for one of those. Three kinds are not one file, for the
/// same underlying reason: what they produce is not a file, it is a set of
/// files that only means anything together.
///
/// A module is a directory whose entire point is that the controller, the
/// service and the routes sit together, and a directory holding one of the
/// three is not a module. A view is a struct and the template it is the type
/// of, and askama reads that template when the crate compiles -- so a view
/// without its template is not a half-finished scaffold, it is a build
/// failure. And a sign-in flow is an account, the handlers that read and
/// write it, and the table it lives in -- a registration handler that does
/// not know how the login handler compares passwords stores a hash nothing
/// can verify.
///
/// A second function rather than a `Vec<Artifact>` field on [`Artifact`] --
/// or a second path on [`super::Generated`] -- because both of those structs
/// are public API and neither is `#[non_exhaustive]`, so growing either a
/// field is a breaking change. Growing a module a function is not.
#[must_use]
pub fn plan_all(kind: MakeKind, name: &ArtifactName) -> Vec<Artifact> {
    match kind {
        MakeKind::Module => module(name),
        MakeKind::View => view(name),
        MakeKind::Auth => auth(name),
        _ => vec![plan(kind, name)],
    }
}

/// The shape almost every kind shares: `<root>/<segments>/<stem>.rs`, a
/// `mod.rs` registration, and no follow-up.
fn rust(
    name: &ArtifactName,
    root: &str,
    suffix: &str,
    render: fn(&Rendered) -> String,
) -> Artifact {
    let rendered = Rendered::new(name, suffix);
    Artifact {
        path: destination(root, name, &rendered.stem),
        contents: render(&rendered),
        register_module: true,
        notes: Vec::new(),
    }
}

/// `<root>/<segments...>/<stem>.rs`.
fn destination(root: &str, name: &ArtifactName, stem: &str) -> PathBuf {
    let mut path = PathBuf::from(root);
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(format!("{stem}.rs"));
    path
}

/// The handful of strings a blueprint interpolates, computed once so a
/// template body reads as a template and not as string plumbing.
pub struct Rendered {
    /// The snake_case file stem (no extension).
    pub stem: String,
    /// The PascalCase type name.
    pub type_name: String,
    /// The name as a `/`-joined path, for page contracts and doc lines.
    pub slash_path: String,
    /// The base PascalCase name with the kind suffix removed (`UserPolicy`
    /// -> `User`), which is the model or event a binding points at.
    pub base_type: String,
    /// The base name in snake_case, for a sibling module path.
    pub base_stem: String,
}

impl Rendered {
    fn new(name: &ArtifactName, suffix: &str) -> Self {
        let base_stem = name.file_stem("");
        Self {
            stem: name.file_stem(suffix),
            type_name: name.type_name(suffix),
            slash_path: name.slash_path(),
            base_type: to_pascal_case(&base_stem),
            base_stem,
        }
    }
}

// ---------------------------------------------------------------------------
// The blueprints.
// ---------------------------------------------------------------------------

fn controller(r: &Rendered) -> String {
    let Rendered {
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! The `{type_name}`: HTTP entry points for `{slash_path}`.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// The `{slash_path}` controller.\n\
         pub struct {type_name};\n\
         \n\
         #[controller]\n\
         impl {type_name} {{\n\
         \x20   /// The index action. Register it in `routes/mod.rs`, then\n\
         \x20   /// replace this body with the real response.\n\
         \x20   pub async fn index() -> Result<Response> {{\n\
         \x20       Ok(text(StatusCode::OK, \"{type_name}::index\"))\n\
         \x20   }}\n\
         }}\n"
    )
}

fn model(r: &Rendered) -> String {
    let Rendered {
        type_name,
        base_stem,
        ..
    } = r;
    let table = pluralize(base_stem);
    format!(
        "//! The `{type_name}` model: one row of `{table}`.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// A `{table}` row.\n\
         #[model(table = \"{table}\")]\n\
         pub struct {type_name} {{\n\
         \x20   #[sea_orm(primary_key)]\n\
         \x20   pub id: i64,\n\
         }}\n"
    )
}

fn migration(name: &ArtifactName) -> Artifact {
    let stem = name.file_stem("");
    let module = format!("m{}_{stem}", utc_stamp());
    let mut path = PathBuf::from("database/migrations");
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(format!("{module}.rs"));

    let contents = format!(
        "//! Migration `{stem}`.\n\
         //!\n\
         //! `up` runs on `arc migrate`. `down` has to undo exactly what `up`\n\
         //! did -- a rollback that leaves the schema in a state no migration\n\
         //! describes is worse than no rollback at all.\n\
         \n\
         // `DeriveMigrationName` expands to a path relative to a crate\n\
         // named `sea_orm_migration`, and the prelude carries names out\n\
         // of that crate rather than the crate itself.\n\
         use arcature::database::sea_orm_migration;\n\
         use arcature::database::sea_orm_migration::prelude::*;\n\
         \n\
         /// The `{stem}` schema change.\n\
         #[derive(DeriveMigrationName)]\n\
         pub struct Migration;\n\
         \n\
         #[async_trait::async_trait]\n\
         impl MigrationTrait for Migration {{\n\
         \x20   async fn up(&self, _manager: &SchemaManager) -> Result<(), DbErr> {{\n\
         \x20       todo!(\"describe the schema change for `{stem}`\")\n\
         \x20   }}\n\
         \n\
         \x20   async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {{\n\
         \x20       todo!(\"undo the schema change for `{stem}`\")\n\
         \x20   }}\n\
         }}\n"
    );

    Artifact {
        path,
        contents,
        register_module: true,
        notes: vec![format!(
            "add `Box::new({module}::Migration)` to `Migrator::migrations()` \
             in database/migrations/mod.rs -- ordering is yours to choose, so \
             the generator does not guess it"
        )],
    }
}

fn request(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}` payload.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// A validated request body. `#[request]` adds `Validate`; the\n\
         /// `Deserialize` derive stays explicit so the extractor can\n\
         /// deserialize and validate in one step.\n\
         #[request]\n\
         #[derive(Debug, Clone, Deserialize)]\n\
         pub struct {type_name} {{\n\
         \x20   #[validate(length(min = 1, max = 255))]\n\
         \x20   pub name: String,\n\
         }}\n"
    )
}

fn resource(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}`: the JSON shape the client sees.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// A browser-safe projection. Convert from the model explicitly\n\
         /// (`impl From<Model> for {type_name}`) so the database schema can\n\
         /// change without breaking the API.\n\
         #[resource]\n\
         pub struct {type_name} {{\n\
         \x20   pub id: String,\n\
         \x20   pub name: String,\n\
         }}\n"
    )
}

fn policy(name: &ArtifactName) -> Artifact {
    let r = Rendered::new(name, "Policy");
    let Rendered {
        type_name,
        base_type,
        base_stem,
        ..
    } = &r;

    let contents = format!(
        "//! The `{type_name}` authorization policy.\n\
         //!\n\
         //! `#[policy(M)]` records *which* model this policy guards; the\n\
         //! `Policy<M>` impl below is the decision itself, and no macro can\n\
         //! guess it. Point the import, the attribute, and `type User` at\n\
         //! real types -- until then this file names `{base_type}` and a user\n\
         //! model that may not exist yet.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use crate::app::models::{base_stem}::{base_type};\n\
         use crate::app::models::user::User;\n\
         \n\
         /// Authorization decisions for `{base_type}`.\n\
         #[policy({base_type})]\n\
         pub struct {type_name};\n\
         \n\
         impl Policy<{base_type}> for {type_name} {{\n\
         \x20   type User = User;\n\
         \n\
         \x20   fn check(_user: &Self::User, action: &str, _resource: &{base_type}) -> bool {{\n\
         \x20       matches!(action, \"view\")\n\
         \x20   }}\n\
         }}\n"
    );

    Artifact {
        path: destination("app/policies", name, &r.stem),
        contents,
        register_module: true,
        notes: vec![format!(
            "{} names `{base_type}` and `User`; point them at the model this \
             policy guards and the application's user type",
            r.stem
        )],
    }
}

fn service(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}`: business logic over the models.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// `#[service]` builds this per request from the application's\n\
         /// resources. Keep the methods framework-agnostic -- take domain\n\
         /// values, return domain values, and let the controller map the\n\
         /// result to HTTP.\n\
         #[service]\n\
         pub struct {type_name} {{\n\
         \x20   db: Db,\n\
         }}\n\
         \n\
         impl {type_name} {{\n\
         \x20   /// The pool this service was resolved with.\n\
         \x20   pub fn db(&self) -> &Db {{\n\
         \x20       &self.db\n\
         \x20   }}\n\
         }}\n"
    )
}

fn job(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}` background job.\n\
         \n\
         use arcature::Job;\n\
         use arcature::prelude::*;\n\
         \n\
         /// The payload the worker deserializes. Keep it small and\n\
         /// self-contained: a job outlives the request that enqueued it, so\n\
         /// anything it needs has to travel in these fields or be re-read\n\
         /// from the database by the handler.\n\
         #[derive(Debug, Clone, Serialize, Deserialize, Job)]\n\
         pub struct {type_name} {{\n\
         \x20   pub id: i64,\n\
         }}\n\
         \n\
         /// The handler. Register it with `Registry::add` at startup.\n\
         #[job_handler]\n\
         pub async fn handle(_job: {type_name}) -> Result<()> {{\n\
         \x20   Ok(())\n\
         }}\n"
    )
}

fn event(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}` in-process event.\n\
         \n\
         use arcature::Event;\n\
         \n\
         /// Dispatched through the `Dispatcher`; listeners receive it by\n\
         /// reference, so the fields describe what happened rather than what\n\
         /// should happen next.\n\
         #[derive(Debug, Clone, Event)]\n\
         pub struct {type_name} {{\n\
         \x20   pub id: i64,\n\
         }}\n"
    )
}

fn listener(name: &ArtifactName) -> Artifact {
    let r = Rendered::new(name, "");
    let Rendered {
        stem,
        base_type,
        base_stem,
        ..
    } = &r;
    let event_type = format!("{base_type}Event");

    let contents = format!(
        "//! The `{stem}` listener.\n\
         //!\n\
         //! A listener is meaningless without an event, and the generator\n\
         //! cannot know which one -- `{event_type}` is a placeholder. Point\n\
         //! the import, the `#[listener(..)]` attribute, and the handler\n\
         //! argument at the event this reacts to.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use crate::app::events::{base_stem}::{event_type};\n\
         \n\
         /// Reacts to `{event_type}`. Register it on the `Dispatcher` at\n\
         /// startup; the attribute only records the binding for inspection.\n\
         #[listener({event_type})]\n\
         pub async fn {stem}(_event: &{event_type}) -> Result<()> {{\n\
         \x20   Ok(())\n\
         }}\n"
    );

    Artifact {
        path: destination("app/listeners", name, stem),
        contents,
        register_module: true,
        notes: vec![format!(
            "{stem} listens for the placeholder `{event_type}`; point it at a \
             real event before building"
        )],
    }
}

fn middleware(r: &Rendered) -> String {
    let Rendered {
        stem, type_name, ..
    } = r;
    format!(
        "//! The `{type_name}` middleware.\n\
         \n\
         use arcature::prelude::*;\n\
         use arcature::routing::Request;\n\
         \n\
         /// `#[middleware]` turns this function into a `pub struct\n\
         /// {type_name}` implementing `Middleware`, so `routes!` can name it\n\
         /// as `middleware: [{type_name}]`. The function stays callable\n\
         /// directly, which is what makes it testable without a router.\n\
         #[middleware]\n\
         pub async fn {stem}(request: Request, next: Next) -> Result<Response> {{\n\
         \x20   Ok(next.run(request).await)\n\
         }}\n"
    )
}

fn command(r: &Rendered) -> String {
    let Rendered {
        stem, slash_path, ..
    } = r;
    let command_name = slash_path.replace('/', ":");
    format!(
        "//! The `{command_name}` application command.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// Invoked by name. The attribute records the binding for\n\
         /// inspection; registration stays explicit in the application's\n\
         /// `CommandRegistry` so nothing runs that was not asked for.\n\
         #[command(\"{command_name}\")]\n\
         pub async fn {stem}() -> Result<()> {{\n\
         \x20   Ok(())\n\
         }}\n"
    )
}

fn page(r: &Rendered) -> String {
    let Rendered {
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! The props for the `{slash_path}` Inertia page.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// Everything the `{slash_path}` component receives. Every field\n\
         /// crosses the Client Exposure Firewall, so a nested type has to be\n\
         /// a `#[resource]` (or another `#[page]`) -- a plain `Serialize`\n\
         /// domain model will not compile here, by design.\n\
         #[page(\"{slash_path}\")]\n\
         pub struct {type_name} {{\n\
         \x20   pub title: String,\n\
         }}\n"
    )
}

fn test(name: &ArtifactName) -> Artifact {
    let stem = name.file_stem("");
    let mut path = PathBuf::from("tests");
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(format!("{stem}.rs"));

    let contents = format!(
        "//! Integration test: {stem}.\n\
         \n\
         #[test]\n\
         fn {stem}_behaves_as_specified() {{\n\
         \x20   // Replace with the behaviour under test. Name the test after\n\
         \x20   // the guarantee it protects, not after the function it calls.\n\
         }}\n"
    );

    Artifact {
        path,
        contents,
        // Each file under `tests/` is its own crate; there is no `mod.rs` to
        // register with, and creating one would break `cargo test`.
        register_module: false,
        notes: Vec::new(),
    }
}

fn factory(r: &Rendered) -> String {
    let Rendered {
        type_name,
        base_type,
        base_stem,
        ..
    } = r;
    format!(
        "//! The `{type_name}`: deterministic `{base_type}` values for tests\n\
         //! and seeders.\n\
         //!\n\
         //! Arcature ships no factory runtime, so a factory is a plain\n\
         //! constructor. The counter keeps generated values unique inside one\n\
         //! test without reaching for a random source, which is what makes a\n\
         //! failure reproducible.\n\
         \n\
         /// Builds `{base_type}` field values.\n\
         #[derive(Debug, Default)]\n\
         pub struct {type_name} {{\n\
         \x20   sequence: u32,\n\
         }}\n\
         \n\
         impl {type_name} {{\n\
         \x20   /// A fresh factory, starting its sequence at zero.\n\
         \x20   pub fn new() -> Self {{\n\
         \x20       Self::default()\n\
         \x20   }}\n\
         \n\
         \x20   /// The next unique `(id, name)` pair.\n\
         \x20   pub fn next(&mut self) -> (u32, String) {{\n\
         \x20       self.sequence += 1;\n\
         \x20       (self.sequence, format!(\"{base_stem}-{{}}\", self.sequence))\n\
         \x20   }}\n\
         }}\n"
    )
}

fn seeder(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}`: rows this seeder owns.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// Seeds a known starting state. `arc db:seed` reaches this through\n\
         /// the application's own binary, which decides what runs and in what\n\
         /// order -- the CLI never guesses at seeder ordering.\n\
         pub struct {type_name};\n\
         \n\
         impl {type_name} {{\n\
         \x20   /// Insert this seeder's rows.\n\
         \x20   pub async fn run(_db: &Db) -> Result<()> {{\n\
         \x20       Ok(())\n\
         \x20   }}\n\
         }}\n"
    )
}

fn notification(name: &ArtifactName) -> Artifact {
    let r = Rendered::new(name, "");
    let Rendered {
        stem, type_name, ..
    } = &r;
    let kind = stem.replace('_', ".");

    let contents = format!(
        "//! The `{type_name}` notification.\n\
         //!\n\
         //! All three channels are written out, because the trait defaults\n\
         //! every one of them to `None` and a channel that was never\n\
         //! considered looks exactly like a channel that was considered and\n\
         //! declined. Delete the ones this notification should not use; the\n\
         //! deletion is the decision.\n\
         \n\
         use arcature::notifications::{{\n\
         \x20   BroadcastContent, DatabaseContent, MailContent, Notification, Recipient,\n\
         }};\n\
         use arcature::serde_json;\n\
         \n\
         /// What happened, in the fields the channels below render from.\n\
         #[derive(Debug, Clone)]\n\
         pub struct {type_name} {{\n\
         \x20   pub id: i64,\n\
         }}\n\
         \n\
         impl Notification for {type_name} {{\n\
         \x20   fn to_mail(&self, recipient: &Recipient) -> Option<MailContent> {{\n\
         \x20       // No address, no mail -- and no error. A recipient\n\
         \x20       // without one is not a failure to send.\n\
         \x20       recipient.email_address()?;\n\
         \n\
         \x20       Some(MailContent::new(\n\
         \x20           \"{type_name}\",\n\
         \x20           format!(\"Something happened, and it concerns #{{}}.\", self.id),\n\
         \x20       ))\n\
         \x20   }}\n\
         \n\
         \x20   fn to_database(&self, _recipient: &Recipient) -> Option<DatabaseContent> {{\n\
         \x20       Some(DatabaseContent::new(\n\
         \x20           KIND,\n\
         \x20           serde_json::json!({{ \"id\": self.id }}),\n\
         \x20       ))\n\
         \x20   }}\n\
         \n\
         \x20   fn to_broadcast(&self, _recipient: &Recipient) -> Option<BroadcastContent> {{\n\
         \x20       Some(BroadcastContent::new(\n\
         \x20           KIND,\n\
         \x20           serde_json::json!({{ \"id\": self.id }}),\n\
         \x20       ))\n\
         \x20   }}\n\
         }}\n\
         \n\
         /// The name the front end and the stored rows know this by.\n\
         ///\n\
         /// Deliberately a constant and deliberately not derived from\n\
         /// `{type_name}`: this string is written into the notifications\n\
         /// table and switched on by the client, so a `refactor: rename` of\n\
         /// the type must not quietly rewrite a protocol and orphan every row\n\
         /// already stored. Renaming the type is free; changing this is a\n\
         /// migration.\n\
         const KIND: &str = \"{kind}\";\n"
    );

    Artifact {
        path: destination("app/notifications", name, stem),
        contents,
        register_module: true,
        notes: vec![
            "notifications are behind the `notifications` feature; add it to the \
             application's `arcature` dependency before building"
                .to_string(),
            "`to_database` and `to_broadcast` render whatever the features are, but \
             delivering them needs `notifications-db` and `notifications-broadcast`"
                .to_string(),
        ],
    }
}

fn mailable(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "//! The `{type_name}` mailable.\n\
         //!\n\
         //! There is no `use arcature::prelude::*` here, and its absence is\n\
         //! load-bearing: the prelude exports a one-parameter `Result<T>`\n\
         //! alias, which would shadow the two-parameter `Result` that\n\
         //! `Mailable::build` is required to return.\n\
         \n\
         use arcature::mail::lettre::Message;\n\
         use arcature::mail::{{Email, EmailError, Mailable}};\n\
         \n\
         /// Everything this email says, in the fields it says it from.\n\
         pub struct {type_name} {{\n\
         \x20   pub name: String,\n\
         }}\n\
         \n\
         impl Mailable for {type_name} {{\n\
         \x20   fn build(&self, email: Email) -> Result<Message, EmailError> {{\n\
         \x20       // `From` and `To` are already set by\n\
         \x20       // `Mail::to(..).send(..)`; the rest is this type's\n\
         \x20       // decision.\n\
         \x20       //\n\
         \x20       // Plain text only, deliberately. `.alternative(plain,\n\
         \x20       // html)` adds an HTML part, but nothing escapes what you\n\
         \x20       // interpolate into it -- a `format!` into an HTML body is\n\
         \x20       // the same mistake in an email as it is in a page. Render\n\
         \x20       // the HTML half from an askama template and let the\n\
         \x20       // template engine escape it.\n\
         \x20       email\n\
         \x20           .subject(\"{type_name}\")\n\
         \x20           .plain(format!(\"Hello, {{}}.\", self.name))\n\
         \x20   }}\n\
         }}\n"
    )
}

// ---------------------------------------------------------------------------
// The view blueprint: a struct and the template it is the type of.
// ---------------------------------------------------------------------------

/// A server-rendered view: the Rust struct, and the `.html` it renders.
///
/// Two files because askama reads the template at build time. A view struct
/// whose `path` names a file that is not there is not a scaffold with a gap
/// in it -- it is a compile error, and the project stops building until the
/// developer writes the half the generator declined to. The pair is the
/// artifact.
fn view(name: &ArtifactName) -> Vec<Artifact> {
    let rendered = Rendered::new(name, "View");
    vec![
        Artifact {
            path: destination("app/views", name, &rendered.stem),
            contents: view_struct(&rendered),
            register_module: true,
            notes: Vec::new(),
        },
        Artifact {
            path: view_template_path(name),
            contents: view_template(&rendered),
            // No `mod.rs` beside a template: `templates/` is read by the
            // askama derive, not declared to rustc. Registering it would
            // write `pub mod welcome;` into a directory that has no Rust in
            // it at all.
            register_module: false,
            notes: Vec::new(),
        },
    ]
}

/// `templates/<segments...>/<base stem>.html`.
///
/// The base stem, not the file stem: the struct is `WelcomeView` and the
/// template is `welcome.html`, because `path` is a template's name and
/// nothing in askama makes it a type's name.
fn view_template_path(name: &ArtifactName) -> PathBuf {
    let mut path = PathBuf::from("templates");
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(format!("{}.html", name.file_stem("")));
    path
}

fn view_struct(r: &Rendered) -> String {
    let Rendered {
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! The `{type_name}`, and the template it is the type of.\n\
         \n\
         // `Template` is both the trait and the `#[derive(Template)]` macro,\n\
         // so one `use` names both. It is in `arcature::prelude` as well,\n\
         // alongside the `view` helper the controller rendering this calls.\n\
         use arcature::view::Template;\n\
         \n\
         /// `templates/{slash_path}.html`.\n\
         ///\n\
         /// The fields are the names the template is allowed to use, and\n\
         /// that is checked when this crate compiles: a `{{{{ subtitle }}}}`\n\
         /// with no `subtitle` field here is a build failure, not a blank\n\
         /// space on a page nobody looked at.\n\
         ///\n\
         /// `askama = arcature::askama` points the derive at the askama the\n\
         /// framework pins, so this application does not depend on askama\n\
         /// directly and cannot drift to a different version of it.\n\
         #[derive(Template)]\n\
         #[template(path = \"{slash_path}.html\", askama = arcature::askama)]\n\
         pub struct {type_name} {{\n\
         \x20   /// The document title, and the heading.\n\
         \x20   pub title: String,\n\
         \x20   /// The body copy.\n\
         \x20   pub message: String,\n\
         }}\n"
    )
}

fn view_template(r: &Rendered) -> String {
    let Rendered { type_name, .. } = r;
    format!(
        "{{# The template `{type_name}` renders. Its field names are the only\n\
         \x20  names available here, and askama checks that at build time.\n\
         \n\
         \x20  `{{{{ }}}}` escapes, because this file's extension is `.html`.\n\
         \x20  Write `{{{{ value|safe }}}}` only for markup you produced\n\
         \x20  yourself -- never for a value that came in on a request. #}}\n\
         {{% extends \"layout.html\" %}}\n\
         \n\
         {{% block title %}}{{{{ title }}}}{{% endblock %}}\n\
         \n\
         {{% block content %}}\n\
         <main>\n\
         \x20 <h1>{{{{ title }}}}</h1>\n\
         \x20 <p>{{{{ message }}}}</p>\n\
         </main>\n\
         {{% endblock %}}\n"
    )
}

fn upload(name: &ArtifactName) -> Artifact {
    // Not the bare `Controller` suffix: `make:controller avatar` and
    // `make:upload avatar` would then be the same path, and the second one
    // would refuse to write rather than sit beside the first.
    let r = Rendered::new(name, "UploadController");
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = &r;

    let contents = format!(
        "//! The `{type_name}`: one upload endpoint for `{slash_path}`.\n\
         //!\n\
         //! Every check `arcature::validation::upload` documents has already\n\
         //! run by the time this handler's body starts -- the filename is\n\
         //! sanitized, the bytes were sniffed, and the extension is one the\n\
         //! policy allows. What is left here is where the bytes go and what\n\
         //! the response says.\n\
         \n\
         use arcature::prelude::*;\n\
         use arcature::storage::StorageError;\n\
         use arcature::validation::upload::UploadedFile;\n\
         \n\
         use crate::bootstrap::AppState;\n\
         \n\
         /// The disk sub-tree these uploads live under.\n\
         ///\n\
         /// A constant, and the application's own string. Nothing that\n\
         /// arrived on the request is ever a prefix -- that is the whole\n\
         /// reason this is not a parameter.\n\
         const PREFIX: &str = \"{slash_path}\";\n\
         \n\
         /// The `{slash_path}` upload controller.\n\
         pub struct {type_name};\n\
         \n\
         #[controller]\n\
         impl {type_name} {{\n\
         \x20   /// Accept one file and store it under its content address.\n\
         \x20   ///\n\
         \x20   /// Register this as a `post` route. With no `UploadPolicy`\n\
         \x20   /// layer on it the route still refuses everything outside\n\
         \x20   /// `AllowedExtensions::images()`, because the default is\n\
         \x20   /// fail-closed on purpose -- so a route that takes documents\n\
         \x20   /// needs the layer, and one that takes images does not.\n\
         \x20   ///\n\
         \x20   /// `UploadedFile` implements `FromRequest`, not\n\
         \x20   /// `FromRequestParts`: it consumes the body, so it has to be\n\
         \x20   /// the last argument.\n\
         \x20   pub async fn store(\n\
         \x20       State(state): State<AppState>,\n\
         \x20       upload: UploadedFile,\n\
         \x20   ) -> Result<Response> {{\n\
         \x20       let storage = state.storage.as_ref().ok_or_else(|| {{\n\
         \x20           Error::Storage(\"no storage disk is configured\".to_string())\n\
         \x20       }})?;\n\
         \n\
         \x20       // `?` rather than a `map_err`: `UploadError` splits the\n\
         \x20       // server's problem from the client's, and\n\
         \x20       // `From<UploadError> for Error` keeps that split. A disk\n\
         \x20       // that is down answers 500; a file whose bytes disagree\n\
         \x20       // with its extension answers 422.\n\
         \x20       let address =\n\
         \x20           upload.store_under(&storage.default_disk(), PREFIX).await?;\n\
         \n\
         \x20       // `address.path()` is the key without the prefix, and the\n\
         \x20       // object was written under one -- so this, not that, is\n\
         \x20       // the name that finds the bytes again.\n\
         \x20       let key = address.path_under(PREFIX).map_err(StorageError::from)?;\n\
         \n\
         \x20       Ok((\n\
         \x20           StatusCode::CREATED,\n\
         \x20           json(serde_json::json!({{\n\
         \x20               \"path\": key.as_str(),\n\
         \x20               \"bytes\": address.byte_len(),\n\
         \x20               // Metadata, and only ever metadata: show it, send\n\
         \x20               // it in a `Content-Disposition`, never resolve it\n\
         \x20               // as a path.\n\
         \x20               \"filename\": upload.filename().to_string(),\n\
         \x20           }})),\n\
         \x20       )\n\
         \x20           .into_response())\n\
         \x20   }}\n\
         }}\n"
    );

    Artifact {
        path: destination("app/controllers", name, stem),
        contents,
        register_module: true,
        notes: vec![
            "uploads are behind the `uploads` feature; add it to the application's \
             `arcature` dependency before building"
                .to_string(),
            "an `UploadPolicy` layer decides which extensions the route accepts; \
             with no layer the route accepts images and nothing else"
                .to_string(),
        ],
    }
}

// ---------------------------------------------------------------------------
// The auth blueprint: one account, three controllers, a route table, a table.
// ---------------------------------------------------------------------------

/// Everything a sign-in needs, minus the screens.
///
/// The six files answer one question each -- who an account is, how one is
/// created, how a session starts and ends, how a forgotten password is
/// recovered, which paths reach those, and what the table looks like -- and
/// the reason they arrive together is that five of the six are wrong on
/// their own. A registration handler that does not know how the login
/// handler compares passwords is a registration handler that stores a hash
/// nothing can verify.
///
/// It is headless: JSON in, JSON or `204` out. A generated login *form* would
/// have to pick Inertia or a server-rendered view, a CSS convention and a
/// copy deck, and be wrong about at least one of them in every application.
/// The parts that are the same everywhere -- and that are dangerous to get
/// wrong -- are here; `arc make:page` and `arc make:view` are one command
/// each for the parts that are not.
fn auth(name: &ArtifactName) -> Vec<Artifact> {
    let r = Rendered::new(name, "");
    vec![
        account(name),
        auth_part(name, "registration_controller", registration_controller(&r)),
        auth_part(name, "session_controller", session_controller(&r)),
        auth_part(name, "password_controller", password_controller(&r)),
        auth_part(name, "routes", auth_routes(&r)),
        auth_migration(name),
    ]
}

/// `app/auth/<segments...>/<stem>_<part>.rs`, registered like any other file.
///
/// Every sibling is registered, unlike `make:module`, because these are
/// ordinary files in an ordinary directory rather than a directory whose own
/// `mod.rs` is written by hand. The generic registration walks up from
/// `app/auth/` and declares each level it had to create, so `app/mod.rs`
/// learns about `auth` without the generator naming it.
fn auth_part(name: &ArtifactName, part: &str, contents: String) -> Artifact {
    let stem = format!("{}_{part}", name.file_stem(""));
    Artifact {
        path: destination("app/auth", name, &stem),
        contents,
        register_module: true,
        notes: Vec::new(),
    }
}

/// The account itself: the model, plus the two traits that make it signable.
fn account(name: &ArtifactName) -> Artifact {
    let r = Rendered::new(name, "");
    let Rendered {
        stem,
        type_name,
        base_stem,
        ..
    } = &r;
    let table = pluralize(base_stem);

    let contents = format!(
        "//! The `{type_name}` account: one row of `{table}`.\n\
         //!\n\
         //! Three impls, and only the first is what `arc make:model` writes.\n\
         //! `AuthUser` is what lets a session hold this account; `UserLoader`\n\
         //! is what turns the id in that session back into an account on the\n\
         //! next request. A model with neither still compiles -- it simply\n\
         //! cannot be signed in.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use crate::bootstrap::AppState;\n\
         \n\
         /// A `{table}` row.\n\
         #[model(table = \"{table}\")]\n\
         pub struct {type_name} {{\n\
         \x20   #[sea_orm(primary_key)]\n\
         \x20   pub id: i64,\n\
         \x20   #[sea_orm(unique)]\n\
         \x20   pub email: String,\n\
         \x20   /// The Argon2 PHC string. Never the password.\n\
         \x20   pub password_hash: String,\n\
         \x20   /// `None` until the address is proved reachable.\n\
         \x20   pub email_verified_at: Option<DateTimeUtc>,\n\
         }}\n\
         \n\
         impl AuthUser for {type_name} {{\n\
         \x20   type Id = i64;\n\
         \n\
         \x20   fn id(&self) -> &i64 {{\n\
         \x20       &self.id\n\
         \x20   }}\n\
         \n\
         \x20   /// The credential every session is stamped against.\n\
         \x20   ///\n\
         \x20   /// Returning it is what signs the other devices out when the\n\
         \x20   /// password changes: a session carries a digest of this, and\n\
         \x20   /// one whose digest no longer matches is flushed on its next\n\
         \x20   /// request. Return `None` and nothing checks, so a stolen\n\
         \x20   /// session outlives the password it was issued against.\n\
         \x20   fn stored_credential(&self) -> Option<&[u8]> {{\n\
         \x20       Some(self.password_hash.as_bytes())\n\
         \x20   }}\n\
         }}\n\
         \n\
         impl UserLoader<AppState> for {type_name} {{\n\
         \x20   type Error = arcature::Error;\n\
         \n\
         \x20   /// `core::result::Result` spelled out: the prelude brings a\n\
         \x20   /// one-parameter `Result<T>` alias, and this signature needs\n\
         \x20   /// the two-parameter one.\n\
         \x20   async fn load_user(\n\
         \x20       id: &i64,\n\
         \x20       state: &AppState,\n\
         \x20   ) -> core::result::Result<Option<Self>, Self::Error> {{\n\
         \x20       {type_name}::query(connection(state)?)\n\
         \x20           .where_eq({type_name}Column::Id, *id)\n\
         \x20           .one()\n\
         \x20           .await\n\
         \x20   }}\n\
         }}\n\
         \n\
         /// Find one account by address.\n\
         pub async fn find_by_email(\n\
         \x20   state: &AppState,\n\
         \x20   email: &str,\n\
         ) -> Result<Option<{type_name}>> {{\n\
         \x20   {type_name}::query(connection(state)?)\n\
         \x20       .where_eq({type_name}Column::Email, email)\n\
         \x20       .one()\n\
         \x20       .await\n\
         }}\n\
         \n\
         /// Insert one account. The caller hashes the password.\n\
         pub async fn create(\n\
         \x20   state: &AppState,\n\
         \x20   email: &str,\n\
         \x20   password_hash: &str,\n\
         ) -> Result<{type_name}> {{\n\
         \x20   let record = {type_name}ActiveModel {{\n\
         \x20       id: ActiveValue::NotSet,\n\
         \x20       email: ActiveValue::Set(email.to_string()),\n\
         \x20       password_hash: ActiveValue::Set(password_hash.to_string()),\n\
         \x20       email_verified_at: ActiveValue::Set(None),\n\
         \x20   }};\n\
         \x20   insert(connection(state)?, record).await\n\
         }}\n\
         \n\
         /// Replace one account's password hash.\n\
         ///\n\
         /// Writing this column is what ends every session that account has\n\
         /// open, including the one doing the writing: the digest\n\
         /// `stored_credential` returns changes with it. Call\n\
         /// `AuthManager::rebind_credential` with the *re-read* account to\n\
         /// keep the current session alive and drop only the others.\n\
         pub async fn set_password(\n\
         \x20   state: &AppState,\n\
         \x20   id: i64,\n\
         \x20   password_hash: &str,\n\
         ) -> Result<{type_name}> {{\n\
         \x20   let record = {type_name}ActiveModel {{\n\
         \x20       id: ActiveValue::Unchanged(id),\n\
         \x20       email: ActiveValue::NotSet,\n\
         \x20       password_hash: ActiveValue::Set(password_hash.to_string()),\n\
         \x20       email_verified_at: ActiveValue::NotSet,\n\
         \x20   }};\n\
         \x20   update(connection(state)?, record).await\n\
         }}\n\
         \n\
         /// The database, or a server error rather than a panic.\n\
         pub fn connection(state: &AppState) -> Result<&Db> {{\n\
         \x20   state\n\
         \x20       .db\n\
         \x20       .as_ref()\n\
         \x20       .ok_or_else(|| Error::Config(\"no database is configured\".to_string()))\n\
         }}\n"
    );

    Artifact {
        path: destination("app/auth", name, stem),
        contents,
        register_module: true,
        notes: vec![
            "the flows these controllers call live behind `auth-flows` and \
             `auth-reset`, neither of which is in the feature list `arc new` \
             writes; add both to the application's `arcature` dependency"
                .to_string(),
        ],
    }
}

fn registration_controller(r: &Rendered) -> String {
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! Registration for `{type_name}`.\n\
         //!\n\
         //! One handler, and it answers identically whether or not the\n\
         //! address is already taken. \"That address is already registered\"\n\
         //! is a membership oracle anyone can query for any address, and a\n\
         //! registration form is the one place it needs no password.\n\
         //!\n\
         //! It also does not sign the new account in. Auto-login is the\n\
         //! friendlier default and it reopens the same hole from the other\n\
         //! side: a `Set-Cookie` on one branch and not the other says which\n\
         //! branch ran. Sign in after the address is verified, or add\n\
         //! `AuthManager` here and accept the oracle knowingly.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use super::{stem}::{{create, find_by_email}};\n\
         use crate::bootstrap::AppState;\n\
         \n\
         /// What a registration form sends.\n\
         #[request]\n\
         #[derive(Debug, Clone, Deserialize)]\n\
         pub struct Register{type_name} {{\n\
         \x20   /// Length only. `validator` is built here without its `email`\n\
         \x20   /// feature, and that is the better check anyway: an address\n\
         \x20   /// is proved by mail arriving at it, not by a regular\n\
         \x20   /// expression agreeing with its shape.\n\
         \x20   #[validate(length(min = 3, max = 254))]\n\
         \x20   pub email: String,\n\
         \x20   #[validate(length(min = 12, max = 4096))]\n\
         \x20   pub password: String,\n\
         }}\n\
         \n\
         /// The `{slash_path}` registration controller.\n\
         pub struct {type_name}RegistrationController;\n\
         \n\
         #[controller]\n\
         impl {type_name}RegistrationController {{\n\
         \x20   /// Create one account.\n\
         \x20   pub async fn store(\n\
         \x20       State(state): State<AppState>,\n\
         \x20       Validated(input): Validated<Register{type_name}>,\n\
         \x20   ) -> Result<Response> {{\n\
         \x20       let email = input.email.trim().to_lowercase();\n\
         \n\
         \x20       // Hash first, unconditionally. Hashing only when the\n\
         \x20       // address turns out to be free would make the response\n\
         \x20       // time say whether it was.\n\
         \x20       let hash = state\n\
         \x20           .hasher\n\
         \x20           .hash(input.password.as_bytes())\n\
         \x20           .map_err(|error| Error::Other(error.to_string()))?;\n\
         \n\
         \x20       if find_by_email(&state, &email).await?.is_none() {{\n\
         \x20           create(&state, &email, hash.as_str()).await?;\n\
         \x20       }}\n\
         \n\
         \x20       Ok((\n\
         \x20           StatusCode::ACCEPTED,\n\
         \x20           json(serde_json::json!({{\n\
         \x20               \"status\": \"check your mail to finish signing up\",\n\
         \x20           }})),\n\
         \x20       )\n\
         \x20           .into_response())\n\
         \x20   }}\n\
         }}\n"
    )
}

fn session_controller(r: &Rendered) -> String {
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! Sign-in and sign-out for `{type_name}`.\n\
         //!\n\
         //! Two decisions here are load-bearing, and both are about what the\n\
         //! response does *not* say.\n\
         //!\n\
         //! A rejected sign-in answers `CREDENTIAL_REJECTION` whether the\n\
         //! address is unknown or the password is wrong, and\n\
         //! `CredentialChecker` runs a full Argon2 verification either way --\n\
         //! against a throwaway hash when there is no account. Skipping that\n\
         //! work for an unknown address makes the two cases differ by\n\
         //! milliseconds, which is a membership oracle measurable over the\n\
         //! network.\n\
         //!\n\
         //! And a stored hash that will not parse falls through to the same\n\
         //! rejection rather than a 500, because a distinguishable error is\n\
         //! that oracle again by another route.\n\
         \n\
         use std::sync::{{LazyLock, OnceLock}};\n\
         \n\
         use arcature::auth::flows::{{CREDENTIAL_REJECTION, CredentialChecker, LoginThrottle}};\n\
         use arcature::prelude::*;\n\
         \n\
         use super::{stem}::{{{type_name}, find_by_email}};\n\
         use crate::bootstrap::AppState;\n\
         \n\
         /// Failed sign-ins, counted per address and per client address.\n\
         ///\n\
         /// A `static` because the count has to outlive the request: an\n\
         /// instance built per call counts to one forever and throttles\n\
         /// nothing. That makes it per-process, so two instances behind a\n\
         /// load balancer each keep their own tally. Move it into `AppState`\n\
         /// when it should be shared, and to a shared store when it should be\n\
         /// shared across processes.\n\
         static THROTTLE: LazyLock<LoginThrottle> = LazyLock::new(LoginThrottle::new);\n\
         \n\
         /// The verifier, built once.\n\
         ///\n\
         /// `CredentialChecker::new` hashes a throwaway password at\n\
         /// construction so it has something to verify against when no\n\
         /// account matches, and that costs one full Argon2. Building it per\n\
         /// request would spend that on every sign-in.\n\
         static CHECKER: OnceLock<CredentialChecker> = OnceLock::new();\n\
         \n\
         fn checker(state: &AppState) -> Result<&'static CredentialChecker> {{\n\
         \x20   if let Some(checker) = CHECKER.get() {{\n\
         \x20       return Ok(checker);\n\
         \x20   }}\n\
         \x20   // Built from the application's own hasher, so the throwaway\n\
         \x20   // verification costs exactly what a real one costs. A checker\n\
         \x20   // with default parameters would take a different amount of\n\
         \x20   // time and put the timing difference straight back.\n\
         \x20   let built = CredentialChecker::new((*state.hasher).clone())\n\
         \x20       .map_err(|error| Error::Other(error.to_string()))?;\n\
         \x20   Ok(CHECKER.get_or_init(|| built))\n\
         }}\n\
         \n\
         /// What a sign-in form sends.\n\
         #[request]\n\
         #[derive(Debug, Clone, Deserialize)]\n\
         pub struct {type_name}Credentials {{\n\
         \x20   #[validate(length(min = 3, max = 254))]\n\
         \x20   pub email: String,\n\
         \x20   #[validate(length(min = 1, max = 4096))]\n\
         \x20   pub password: String,\n\
         }}\n\
         \n\
         /// The `{slash_path}` session controller.\n\
         pub struct {type_name}SessionController;\n\
         \n\
         #[controller]\n\
         impl {type_name}SessionController {{\n\
         \x20   /// Start a session.\n\
         \x20   ///\n\
         \x20   /// `ClientIp` arrives as an extension rather than an\n\
         \x20   /// extractor, and optionally: the raw serve path does not\n\
         \x20   /// insert one. `None` throttles by address alone, which is\n\
         \x20   /// weaker than throttling by both and stronger than a 500.\n\
         \x20   pub async fn store(\n\
         \x20       State(state): State<AppState>,\n\
         \x20       auth: AuthManager<{type_name}>,\n\
         \x20       client: Option<Extension<ClientIp>>,\n\
         \x20       Validated(input): Validated<{type_name}Credentials>,\n\
         \x20   ) -> Result<Response> {{\n\
         \x20       let email = input.email.trim().to_lowercase();\n\
         \x20       let address = client.map(|Extension(ip)| ip.addr());\n\
         \n\
         \x20       let decision = THROTTLE.check(&email, address);\n\
         \x20       if !decision.is_allowed() {{\n\
         \x20           let seconds = decision.retry_after().map_or(60, |d| d.as_secs());\n\
         \x20           return Ok((\n\
         \x20               StatusCode::TOO_MANY_REQUESTS,\n\
         \x20               [(\"retry-after\", seconds.to_string())],\n\
         \x20               json(serde_json::json!({{ \"message\": \"too many attempts\" }})),\n\
         \x20           )\n\
         \x20               .into_response());\n\
         \x20       }}\n\
         \n\
         \x20       let account = find_by_email(&state, &email).await?;\n\
         \n\
         \x20       // A hash the parser rejects becomes \"no stored hash\",\n\
         \x20       // which the checker answers with the same rejection as a\n\
         \x20       // wrong password. The alternative is a 500 on exactly the\n\
         \x20       // accounts whose rows are damaged, which says something\n\
         \x20       // about those accounts.\n\
         \x20       let stored = account\n\
         \x20           .as_ref()\n\
         \x20           .and_then(|found| PasswordHashString::new(&found.password_hash).ok());\n\
         \n\
         \x20       let outcome = checker(&state)?.check(stored.as_ref(), input.password.as_bytes());\n\
         \n\
         \x20       match (outcome.is_verified(), account) {{\n\
         \x20           (true, Some(account)) => {{\n\
         \x20               THROTTLE.record_success(&email, address);\n\
         \x20               auth.login(&account)\n\
         \x20                   .await\n\
         \x20                   .map_err(|error| Error::Other(error.to_string()))?;\n\
         \x20               Ok(no_content())\n\
         \x20           }}\n\
         \x20           _ => {{\n\
         \x20               THROTTLE.record_failure(&email, address);\n\
         \x20               Ok((\n\
         \x20                   StatusCode::UNPROCESSABLE_ENTITY,\n\
         \x20                   json(serde_json::json!({{ \"message\": CREDENTIAL_REJECTION }})),\n\
         \x20               )\n\
         \x20                   .into_response())\n\
         \x20           }}\n\
         \x20       }}\n\
         \x20   }}\n\
         \n\
         \x20   /// End the session.\n\
         \x20   pub async fn destroy(auth: AuthManager<{type_name}>) -> Result<Response> {{\n\
         \x20       auth.logout()\n\
         \x20           .await\n\
         \x20           .map_err(|error| Error::Other(error.to_string()))?;\n\
         \x20       Ok(no_content())\n\
         \x20   }}\n\
         }}\n"
    )
}

fn password_controller(r: &Rendered) -> String {
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = r;
    format!(
        "//! Password reset for `{type_name}`.\n\
         //!\n\
         //! `request` mints a one-time link; `reset` redeems it.\n\
         //!\n\
         //! `request` answers the same way for an address with no account,\n\
         //! for the reason registration does -- a forgotten-password form\n\
         //! that says \"no such address\" is a membership oracle needing no\n\
         //! password at all.\n\
         //!\n\
         //! What it does not do is send the mail. The link has to reach the\n\
         //! person somehow, and which template, transport and copy that means\n\
         //! is the application's. The line below builds the URL and stops.\n\
         \n\
         use std::time::Duration;\n\
         \n\
         use arcature::auth::flows::PasswordResets;\n\
         use arcature::prelude::*;\n\
         \n\
         use super::{stem}::{{connection, find_by_email, set_password}};\n\
         use crate::bootstrap::AppState;\n\
         \n\
         /// How long a reset link is good for.\n\
         ///\n\
         /// Short on purpose: the window is how long a link sitting in an\n\
         /// inbox, a proxy log or a browser history is still worth stealing.\n\
         const RESET_TTL: Duration = Duration::from_secs(60 * 60);\n\
         \n\
         /// What a forgotten-password form sends.\n\
         #[request]\n\
         #[derive(Debug, Clone, Deserialize)]\n\
         pub struct {type_name}PasswordRequest {{\n\
         \x20   #[validate(length(min = 3, max = 254))]\n\
         \x20   pub email: String,\n\
         }}\n\
         \n\
         /// What the reset form sends back.\n\
         #[request]\n\
         #[derive(Debug, Clone, Deserialize)]\n\
         pub struct {type_name}PasswordReset {{\n\
         \x20   #[validate(length(min = 1, max = 512))]\n\
         \x20   pub token: String,\n\
         \x20   #[validate(length(min = 12, max = 4096))]\n\
         \x20   pub password: String,\n\
         }}\n\
         \n\
         /// The `{slash_path}` password controller.\n\
         pub struct {type_name}PasswordController;\n\
         \n\
         #[controller]\n\
         impl {type_name}PasswordController {{\n\
         \x20   /// Mint a reset link, if the address has an account.\n\
         \x20   pub async fn request(\n\
         \x20       State(state): State<AppState>,\n\
         \x20       Validated(input): Validated<{type_name}PasswordRequest>,\n\
         \x20   ) -> Result<Response> {{\n\
         \x20       let email = input.email.trim().to_lowercase();\n\
         \x20       let resets = PasswordResets::new(connection(&state)?.sqlx().clone());\n\
         \n\
         \x20       if find_by_email(&state, &email).await?.is_some() {{\n\
         \x20           let issued = resets\n\
         \x20               .issue(&email, RESET_TTL)\n\
         \x20               .await\n\
         \x20               .map_err(|error| Error::Other(error.to_string()))?;\n\
         \n\
         \x20           // The one place the plaintext exists. Mail it. Do not\n\
         \x20           // log it, and do not return it -- a reset link in a\n\
         \x20           // response body belongs to anyone who can read the\n\
         \x20           // response.\n\
         \x20           let _link = format!(\n\
         \x20               \"{{}}/password/reset?token={{}}\",\n\
         \x20               state.app_url,\n\
         \x20               issued.plaintext().expose()\n\
         \x20           );\n\
         \x20       }}\n\
         \n\
         \x20       Ok(json(serde_json::json!({{\n\
         \x20           \"status\": \"if that address has an account, a link is on its way\",\n\
         \x20       }})))\n\
         \x20   }}\n\
         \n\
         \x20   /// Redeem a reset link and set the new password.\n\
         \x20   pub async fn reset(\n\
         \x20       State(state): State<AppState>,\n\
         \x20       Validated(input): Validated<{type_name}PasswordReset>,\n\
         \x20   ) -> Result<Response> {{\n\
         \x20       let resets = PasswordResets::new(connection(&state)?.sqlx().clone());\n\
         \n\
         \x20       let Some(email) = resets\n\
         \x20           .consume(&input.token)\n\
         \x20           .await\n\
         \x20           .map_err(|error| Error::Other(error.to_string()))?\n\
         \x20       else {{\n\
         \x20           return Ok((\n\
         \x20               StatusCode::UNPROCESSABLE_ENTITY,\n\
         \x20               json(serde_json::json!({{ \"message\": \"that link is not valid\" }})),\n\
         \x20           )\n\
         \x20               .into_response());\n\
         \x20       }};\n\
         \n\
         \x20       let Some(account) = find_by_email(&state, &email).await? else {{\n\
         \x20           // The link redeemed, but the account is gone.\n\
         \x20           return Ok(no_content());\n\
         \x20       }};\n\
         \n\
         \x20       let hash = state\n\
         \x20           .hasher\n\
         \x20           .hash(input.password.as_bytes())\n\
         \x20           .map_err(|error| Error::Other(error.to_string()))?;\n\
         \x20       set_password(&state, account.id, hash.as_str()).await?;\n\
         \n\
         \x20       // Writing the hash already ended every session bound to\n\
         \x20       // the old one. This revokes the outstanding *links* too,\n\
         \x20       // so a second copy of the mail cannot be redeemed after\n\
         \x20       // the first.\n\
         \x20       resets\n\
         \x20           .revoke_all_for(&email)\n\
         \x20           .await\n\
         \x20           .map_err(|error| Error::Other(error.to_string()))?;\n\
         \n\
         \x20       Ok(no_content())\n\
         \x20   }}\n\
         }}\n"
    )
}

fn auth_routes(r: &Rendered) -> String {
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = r;
    let namespace = slash_path.replace('/', ".");
    format!(
        "//! The paths the `{slash_path}` sign-in flow serves.\n\
         //!\n\
         //! The paths are absolute: a `Routes` collection adds no prefix of\n\
         //! its own. Wrap the block in a `group` if this flow needs one, or\n\
         //! if a second `arc make:auth` would otherwise claim `/login` twice\n\
         //! -- two routes on one path is a panic at boot, from axum.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use super::{stem}_password_controller::{type_name}PasswordController;\n\
         use super::{stem}_registration_controller::{type_name}RegistrationController;\n\
         use super::{stem}_session_controller::{type_name}SessionController;\n\
         use crate::bootstrap::AppState;\n\
         \n\
         routes! {{\n\
         \x20   pub {stem}_auth {{\n\
         \x20       state: AppState;\n\
         \n\
         \x20       post \"/register\" => {type_name}RegistrationController::store \
         {{ name: {namespace}.register }}\n\
         \x20       post \"/login\" => {type_name}SessionController::store \
         {{ name: {namespace}.login }}\n\
         \x20       post \"/logout\" => {type_name}SessionController::destroy \
         {{ name: {namespace}.logout }}\n\
         \x20       post \"/password/forgot\" => {type_name}PasswordController::request \
         {{ name: {namespace}.password.forgot }}\n\
         \x20       post \"/password/reset\" => {type_name}PasswordController::reset \
         {{ name: {namespace}.password.reset }}\n\
         \x20   }}\n\
         }}\n"
    )
}

/// The table the account maps to.
///
/// Explicit `ColumnDef` builders rather than the `schema::*` shorthands, and
/// a `DeriveIden` enum named for the table so the default snake-casing spells
/// it -- both so this file depends on nothing beyond the migration prelude it
/// already imports.
fn auth_migration(name: &ArtifactName) -> Artifact {
    let base_stem = name.file_stem("");
    let table = pluralize(&base_stem);
    let iden = to_pascal_case(&table);
    let module = format!("m{}_create_{table}", utc_stamp());
    let mut path = PathBuf::from("database/migrations");
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(format!("{module}.rs"));

    let contents = format!(
        "//! Migration `create_{table}`: the table the account model maps to.\n\
         \n\
         // `DeriveMigrationName` expands to a path relative to a crate\n\
         // named `sea_orm_migration`, and the prelude carries names out\n\
         // of that crate rather than the crate itself.\n\
         use arcature::database::sea_orm_migration;\n\
         use arcature::database::sea_orm_migration::prelude::*;\n\
         \n\
         /// Create `{table}`.\n\
         #[derive(DeriveMigrationName)]\n\
         pub struct Migration;\n\
         \n\
         /// The columns, spelled the way `#[model]` spells them.\n\
         #[derive(DeriveIden)]\n\
         enum {iden} {{\n\
         \x20   Table,\n\
         \x20   Id,\n\
         \x20   Email,\n\
         \x20   PasswordHash,\n\
         \x20   EmailVerifiedAt,\n\
         }}\n\
         \n\
         #[async_trait::async_trait]\n\
         impl MigrationTrait for Migration {{\n\
         \x20   async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {{\n\
         \x20       manager\n\
         \x20           .create_table(\n\
         \x20               Table::create()\n\
         \x20                   .table({iden}::Table)\n\
         \x20                   .if_not_exists()\n\
         \x20                   .col(\n\
         \x20                       ColumnDef::new({iden}::Id)\n\
         \x20                           .big_integer()\n\
         \x20                           .not_null()\n\
         \x20                           .auto_increment()\n\
         \x20                           .primary_key(),\n\
         \x20                   )\n\
         \x20                   // Unique in the database, not only in the\n\
         \x20                   // handler: two registrations racing each other\n\
         \x20                   // both read \"no such address\" and both insert.\n\
         \x20                   // The constraint is what makes the second one\n\
         \x20                   // fail.\n\
         \x20                   .col(\n\
         \x20                       ColumnDef::new({iden}::Email)\n\
         \x20                           .string_len(254)\n\
         \x20                           .not_null()\n\
         \x20                           .unique_key(),\n\
         \x20                   )\n\
         \x20                   .col(\n\
         \x20                       ColumnDef::new({iden}::PasswordHash)\n\
         \x20                           .string_len(255)\n\
         \x20                           .not_null(),\n\
         \x20                   )\n\
         \x20                   .col(\n\
         \x20                       ColumnDef::new({iden}::EmailVerifiedAt)\n\
         \x20                           .timestamp_with_time_zone()\n\
         \x20                           .null(),\n\
         \x20                   )\n\
         \x20                   .to_owned(),\n\
         \x20           )\n\
         \x20           .await\n\
         \x20   }}\n\
         \n\
         \x20   async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {{\n\
         \x20       manager\n\
         \x20           .drop_table(Table::drop().table({iden}::Table).to_owned())\n\
         \x20           .await\n\
         \x20   }}\n\
         }}\n"
    );

    Artifact {
        path,
        contents,
        register_module: true,
        notes: vec![
            format!(
                "add `Box::new({module}::Migration)` to `Migrator::migrations()` \
                 in database/migrations/mod.rs -- ordering is yours to choose, so \
                 the generator does not guess it"
            ),
            "the reset links need `arcature_password_resets`, which is not this \
             migration: call `PasswordResets::new(pool).migrate()` once at boot, \
             or write the table into a migration of your own"
                .to_string(),
            format!(
                "the route collection is not mounted anywhere -- merge \
                 `crate::app::auth::{base_stem}_routes::{base_stem}_auth_routes()` \
                 into `bootstrap/app.rs` beside `crate::routes::app_routes()`"
            ),
        ],
    }
}

// ---------------------------------------------------------------------------
// The module blueprint: one directory, four files.
// ---------------------------------------------------------------------------

/// A feature module: the `module!` block, and the controller, service and
/// routes it declares.
///
/// Four files rather than five. `arc make:policy` is one command away, and a
/// policy scaffold cannot compile until it is pointed at a model and a user
/// type that a fresh application does not have -- shipping one inside a
/// module would mean `arc make:module billing` produces a project that does
/// not build.
fn module(name: &ArtifactName) -> Vec<Artifact> {
    let rendered = Rendered::new(name, "");
    vec![
        module_root(name),
        module_part(name, "controller.rs", module_controller(&rendered)),
        module_part(name, "service.rs", module_service(&rendered)),
        module_part(name, "routes.rs", module_routes(&rendered)),
    ]
}

/// `app/modules/<segments...>/<stem>/<file>` -- the directory a module owns.
fn module_file(name: &ArtifactName, file: &str) -> PathBuf {
    let mut path = PathBuf::from("app/modules");
    for segment in name.segments() {
        path.push(segment);
    }
    path.push(name.file_stem(""));
    path.push(file);
    path
}

/// One of the three files the module's own `mod.rs` already declares.
///
/// `register_module` is false here, and on the root as well. The generic
/// registration derives the declaration from the file stem, which for these
/// three would append a second `pub mod controller;` to a `mod.rs` that
/// already has one -- and for the root would write `pub mod mod;`.
/// [`super::generate_all`] registers the module as a whole instead.
fn module_part(name: &ArtifactName, file: &str, contents: String) -> Artifact {
    Artifact {
        path: module_file(name, file),
        contents,
        register_module: false,
        notes: Vec::new(),
    }
}

fn module_root(name: &ArtifactName) -> Artifact {
    let r = Rendered::new(name, "");
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = &r;
    let controller_type = format!("{type_name}Controller");
    let service_type = format!("{type_name}Service");
    let routes_const = format!("{}_ROUTES", stem.to_uppercase());

    let contents = format!(
        "//! The `{slash_path}` feature module.\n\
         //!\n\
         //! Everything this feature owns lives in this directory, and the\n\
         //! `module!` block below is the index of it. The application graph\n\
         //! reads that index at boot: a type missing from it still compiles\n\
         //! and still serves, it is simply invisible to `arc routes` and\n\
         //! `arc typegen`.\n\
         \n\
         pub mod controller;\n\
         pub mod routes;\n\
         pub mod service;\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         // `controllers:` and `routes:` are resolved at this site -- the\n\
         // first reads the controller's method metadata, the second is a path\n\
         // to a const. `services:` and `policies:` are recorded as names\n\
         // only, which is why `{service_type}` is named below but not\n\
         // imported: an import the macro never resolves is an unused one.\n\
         use controller::{controller_type};\n\
         \n\
         module! {{\n\
         \x20   pub {type_name} {{\n\
         \x20       controllers: [{controller_type}],\n\
         \x20       services: [{service_type}],\n\
         \x20       routes: routes::{routes_const},\n\
         \x20   }}\n\
         }}\n"
    );

    Artifact {
        path: module_file(name, "mod.rs"),
        contents,
        register_module: false,
        notes: Vec::new(),
    }
}

fn module_controller(r: &Rendered) -> String {
    let Rendered {
        type_name,
        slash_path,
        ..
    } = r;
    let controller_type = format!("{type_name}Controller");
    format!(
        "//! HTTP entry points for the `{slash_path}` module.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// The `{slash_path}` controller.\n\
         pub struct {controller_type};\n\
         \n\
         #[controller]\n\
         impl {controller_type} {{\n\
         \x20   /// The index action, already wired up in this module's\n\
         \x20   /// `routes.rs`. Replace the body with the real response.\n\
         \x20   pub async fn index() -> Result<Response> {{\n\
         \x20       Ok(text(StatusCode::OK, \"{controller_type}::index\"))\n\
         \x20   }}\n\
         }}\n"
    )
}

fn module_service(r: &Rendered) -> String {
    let Rendered {
        type_name,
        slash_path,
        ..
    } = r;
    let service_type = format!("{type_name}Service");
    format!(
        "//! Business logic for the `{slash_path}` module.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         /// `#[service]` builds this per request from the application's\n\
         /// resources. Keep the methods framework-agnostic -- take domain\n\
         /// values, return domain values, and let the controller map the\n\
         /// result to HTTP.\n\
         #[service]\n\
         pub struct {service_type} {{\n\
         \x20   db: Db,\n\
         }}\n\
         \n\
         impl {service_type} {{\n\
         \x20   /// The pool this service was resolved with.\n\
         \x20   pub fn db(&self) -> &Db {{\n\
         \x20       &self.db\n\
         \x20   }}\n\
         }}\n"
    )
}

fn module_routes(r: &Rendered) -> String {
    let Rendered {
        stem,
        type_name,
        slash_path,
        ..
    } = r;
    let controller_type = format!("{type_name}Controller");
    let route_name = slash_path.replace('/', ".");
    format!(
        "//! The paths the `{slash_path}` module serves.\n\
         //!\n\
         //! `app/modules/mod.rs` merges this block into the application's own\n\
         //! table, so the paths here are absolute -- living in a module adds\n\
         //! no prefix. Two modules claiming the same path is a panic at boot,\n\
         //! from axum; two modules claiming the same route *name* is not, and\n\
         //! the later one silently wins. That is why the name below carries\n\
         //! the module's own.\n\
         \n\
         use arcature::prelude::*;\n\
         \n\
         use super::controller::{controller_type};\n\
         use crate::bootstrap::AppState;\n\
         \n\
         routes! {{\n\
         \x20   pub {stem} {{\n\
         \x20       state: AppState;\n\
         \n\
         \x20       get \"/{slash_path}\" => {controller_type}::index \
         {{ name: {route_name}.index }}\n\
         \x20   }}\n\
         }}\n"
    )
}

// ---------------------------------------------------------------------------
// Timestamps.
// ---------------------------------------------------------------------------

/// `YYYYMMDD_HHMMSS` in UTC, the ordering prefix SeaORM migrations use.
///
/// Computed from `SystemTime` by hand rather than through `chrono`: `chrono`
/// arrives with the `database` feature, and `arc make:migration` has to work
/// in a CLI-only build. A clock before the epoch (only reachable on a badly
/// misconfigured machine) falls back to zero rather than panicking -- a
/// wrong-but-ordered filename beats a crashed generator.
fn utc_stamp() -> String {
    let seconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs());
    let days = i64::try_from(seconds / 86_400).unwrap_or(0);
    let time_of_day = seconds % 86_400;
    let (year, month, day) = civil_from_days(days);
    let (hour, minute, second) = (
        time_of_day / 3_600,
        (time_of_day % 3_600) / 60,
        time_of_day % 60,
    );
    format!("{year:04}{month:02}{day:02}_{hour:02}{minute:02}{second:02}")
}

/// Howard Hinnant's `civil_from_days`: days since 1970-01-01 to a proleptic
/// Gregorian date. Reproduced because it is exact, branch-light, and shorter
/// than the dependency it would otherwise justify.
fn civil_from_days(days: i64) -> (i64, u64, u64) {
    let shifted = days + 719_468;
    let era = if shifted >= 0 {
        shifted
    } else {
        shifted - 146_096
    } / 146_097;
    let day_of_era = (shifted - era * 146_097) as u64; // [0, 146096]
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let year = year_of_era as i64 + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_prime = (5 * day_of_year + 2) / 153; // [0, 11], March-based
    let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
    let month = if month_prime < 10 {
        month_prime + 3
    } else {
        month_prime - 9
    };
    (if month <= 2 { year + 1 } else { year }, month, day)
}

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

    fn planned(kind: MakeKind, name: &str) -> Artifact {
        plan(kind, &ArtifactName::parse(name).expect("valid name"))
    }

    #[test]
    fn every_kind_plans_a_rust_file_under_a_known_root() {
        for kind in MakeKind::ALL {
            let artifact = planned(*kind, "widget");
            assert_eq!(
                artifact.path.extension().and_then(|e| e.to_str()),
                Some("rs"),
                "{} did not plan a .rs file",
                kind.as_str()
            );
            assert!(
                !artifact.contents.trim().is_empty(),
                "{} planned an empty file",
                kind.as_str()
            );
            assert!(
                artifact.contents.ends_with('\n'),
                "{} planned a file without a trailing newline",
                kind.as_str()
            );
        }
    }

    #[test]
    fn a_nested_name_nests_the_generated_file() {
        let artifact = planned(MakeKind::Controller, "admin/users");
        assert_eq!(
            artifact.path,
            PathBuf::from("app/controllers/admin/users_controller.rs")
        );
        assert!(artifact.contents.contains("pub struct UsersController;"));
    }

    #[test]
    fn a_model_guesses_a_plural_table_name() {
        let artifact = planned(MakeKind::Model, "Category");
        assert_eq!(artifact.path, PathBuf::from("app/models/category.rs"));
        assert!(
            artifact
                .contents
                .contains("#[model(table = \"categories\")]")
        );
    }

    #[test]
    fn a_page_carries_the_name_the_developer_typed_as_its_contract() {
        let artifact = planned(MakeKind::Page, "users/show");
        assert_eq!(artifact.path, PathBuf::from("app/pages/users/show_page.rs"));
        assert!(artifact.contents.contains("#[page(\"users/show\")]"));
        assert!(artifact.contents.contains("pub struct ShowPage"));
    }

    #[test]
    fn a_command_name_uses_colons_where_the_path_used_slashes() {
        let artifact = planned(MakeKind::Command, "users/prune");
        assert!(artifact.contents.contains("#[command(\"users:prune\")]"));
    }

    #[test]
    fn a_migration_is_timestamped_and_asks_to_be_registered() {
        let artifact = planned(MakeKind::Migration, "create_users_table");
        let file = artifact.path.file_name().and_then(|n| n.to_str()).unwrap();
        assert!(
            file.starts_with('m'),
            "{file} is missing its ordering prefix"
        );
        assert!(file.ends_with("_create_users_table.rs"), "{file}");
        assert_eq!(artifact.notes.len(), 1);
        assert!(artifact.notes[0].contains("Migrator::migrations()"));
    }

    #[test]
    fn a_test_is_not_registered_in_a_module_tree() {
        let artifact = planned(MakeKind::Test, "checkout");
        assert_eq!(artifact.path, PathBuf::from("tests/checkout.rs"));
        assert!(!artifact.register_module);
    }

    #[test]
    fn the_two_blueprints_with_placeholders_say_so() {
        for kind in [MakeKind::Policy, MakeKind::Listener] {
            let artifact = planned(kind, "widget");
            assert!(
                !artifact.notes.is_empty(),
                "{} has a placeholder but no note",
                kind.as_str()
            );
        }
    }

    #[test]
    fn the_civil_calendar_matches_known_dates() {
        assert_eq!(civil_from_days(0), (1970, 1, 1));
        assert_eq!(civil_from_days(-1), (1969, 12, 31));
        // 2000-02-29: the leap day the century rule keeps.
        assert_eq!(civil_from_days(11_016), (2000, 2, 29));
        assert_eq!(civil_from_days(20_454), (2026, 1, 1));
    }

    #[test]
    fn the_migration_stamp_is_fixed_width_and_sortable() {
        let stamp = utc_stamp();
        assert_eq!(stamp.len(), 15, "{stamp}");
        assert_eq!(&stamp[8..9], "_");
        assert!(
            stamp
                .chars()
                .enumerate()
                .all(|(i, c)| i == 8 || c.is_ascii_digit()),
            "{stamp}"
        );
    }
}