truss-image 0.21.0

Image toolkit with a shared Rust core across the CLI, HTTP server, and WASM demo.
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
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
use crate::adapters::server::SignedUrlSource;
use crate::{
    CropRegion, Fit, MediaType, OptimizeMode, Position, Rgba8, Rotation, TargetQuality,
    TransformOptions,
};
use clap::{CommandFactory, Parser, Subcommand};
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use crate::core::error_class::ErrorClass;
use std::str::FromStr;

mod convert;
mod inspect;
mod serve;
mod sign;

const MAX_REMOTE_BYTES: u64 = 32 * 1024 * 1024;

/// The size cap for a watermark fetched over HTTP.
///
/// The server keeps this apart from the source limit and publishes it as
/// `TRUSS_MAX_WATERMARK_BYTES`: an overlay has no business being the size of a source image,
/// and a watermark the server would refuse is one there is no point in accepting here.
pub(super) const MAX_REMOTE_WATERMARK_BYTES: u64 = 10 * 1024 * 1024;

// ---------------------------------------------------------------------------
// Exit codes — kept in sync with help text
// ---------------------------------------------------------------------------

/// Successful completion.
const EXIT_SUCCESS: u8 = 0;
/// Usage error (bad arguments, missing required flags).
const EXIT_USAGE: u8 = 1;
/// I/O error (file not found, permission denied, network failure).
const EXIT_IO: u8 = 2;
/// Input error (unsupported format, corrupt file).
const EXIT_INPUT: u8 = 3;
/// Transform error (encode failure, size limit exceeded, deadline).
const EXIT_TRANSFORM: u8 = 4;
/// Runtime error (bind failure, stdout write failure).
const EXIT_RUNTIME: u8 = 5;

// ---------------------------------------------------------------------------
// Help text — split by topic (hand-crafted for rich output)
// ---------------------------------------------------------------------------

fn help_top_level() -> String {
    format!(
        "\
truss {version} - an image transformation tool and server

Converts, resizes, and re-encodes images (JPEG, PNG, WebP, AVIF, BMP, TIFF, SVG).
Can also run as an HTTP image-transform server.

USAGE:
  truss <COMMAND> [OPTIONS]
  truss <INPUT> -o <OUTPUT> [OPTIONS]   (implicit convert)
  truss --bind <ADDR> [OPTIONS]         (implicit serve)

COMMANDS:
  convert       Convert and transform an image file
  optimize      Optimize an image for smaller output size
  inspect       Show metadata (format, dimensions, alpha) of an image
  serve         Start the HTTP image-transform server
  validate      Check server configuration without starting the server
  sign          Generate a signed public URL for the server
  completions   Generate shell completion scripts
  help          Show help for a command (e.g. truss help convert)

OPTIONS:
  -V, --version   Print version information

EXAMPLES:
  truss photo.png -o photo.jpg --width 800
  truss inspect photo.jpg
  truss serve --bind 0.0.0.0:8080 --storage-root /var/images
  truss sign --base-url https://cdn.example.com --path /hero.jpg \\
    --key-id mykey --secret s3cret --expires 1700000000
  truss completions bash > ~/.local/share/bash-completion/completions/truss

Run 'truss help <command>' for more information on a specific command.

EXIT CODES:
  0  Success
  1  Usage error (bad arguments)
  2  I/O error (file not found, permission denied, network failure)
  3  Input error (unsupported format, corrupt file)
  4  Transform error (encode failure, size limit exceeded, deadline)
  5  Runtime error (bind failure, stdout write failure)

Sponsor: https://github.com/sponsors/nao1215
",
        version = env!("CARGO_PKG_VERSION"),
    )
}

const HELP_CONVERT: &str = "\
truss convert - convert and transform an image file

USAGE:
  truss convert <INPUT> -o <OUTPUT> [OPTIONS]
  truss convert --url <URL> -o <OUTPUT> [OPTIONS]
  truss convert - -o - --format jpeg    (stdin to stdout)

  The 'convert' subcommand can be omitted:
    truss <INPUT> -o <OUTPUT> [OPTIONS]
    truss --url <URL> -o <OUTPUT> [OPTIONS]

  For a file whose name starts with -, put the options first and end them with --,
  or write the path with a ./ prefix. An output cannot be escaped with --, because
  -o takes the next argument whatever it looks like; assign it with = instead:
    truss convert -o out.jpg -- -input.png
    truss convert input.png --output=-output.jpg

OPTIONS:
  -o, --output <OUTPUT>    Output file path, or - for stdout (required)
      --url <URL>          Fetch input from an HTTP(S) URL
      --width <PX>         Target width in pixels
      --height <PX>        Target height in pixels
      --fit <MODE>         How to fit into target dimensions (requires --width and --height)
                           contain: scale to fit, then pad to the exact box (default)
                           cover:   scale to fill the box, cropping the excess
                           fill:    stretch each axis to the box, ignoring aspect ratio
                           inside:  scale to fit, no padding; the output is at most the
                                    box and usually smaller on one axis
      --position <POS>     Crop anchor for cover mode (default: center)
                           center, top, right, bottom, left,
                           top-left, top-right, bottom-left, bottom-right
      --format <FMT>       Output format: jpeg, png, webp, avif, bmp, tiff, svg
                           (default: inferred from output extension)
      --quality <1-100>    Encoding quality for lossy formats
      --optimize <MODE>    Optimization mode: none, auto, lossless, lossy
      --target-quality <TARGET>
                           Perceptual target for lossy optimization (e.g. ssim:0.98, psnr:42)
      --background <COLOR> Background color as RRGGBB or RRGGBBAA hex
      --rotate <DEG>       Rotate clockwise by whole degrees. Negative turns counter-clockwise,
                           and angles past a full turn wrap, so -90 and 270 are the same.
                           A multiple of 90 is exact; any other angle resamples and grows
                           the canvas to the rotated bounding box, filling the exposed
                           corners with --background (transparent, or white for formats
                           without alpha)
      --auto-orient        Apply EXIF orientation and reset tag (default)
      --no-auto-orient     Skip EXIF orientation correction (add --keep-metadata to keep
                           the tag; stripping it as well leaves the image rotated)
      --strip-metadata     Remove all metadata (default; lossy optimization keeps the
                           ICC profile so colors are not shifted by the re-encode)
      --keep-metadata      Preserve EXIF, ICC, and other supported metadata
      --preserve-exif      Preserve EXIF only (strip ICC and others)
      --crop <x,y,w,h>     Explicit crop region as x,y,width,height (applied before resize)
      --blur <SIGMA>       Gaussian blur sigma (0.1-100.0)
      --sharpen <SIGMA>    Sharpen sigma (0.1-100.0)
      --grayscale          Desaturate the image to grayscale (applied after resize, blur,
                           and sharpen, and before the watermark)
      --without-enlargement
                           Never scale an image up. A source already within the requested
                           size keeps that size. Combines with any --fit; contain still
                           pads out to the full box, and cover returns the box intersected
                           with the source rather than the whole box
      --watermark <FILE|URL>
                           Watermark image to composite onto the output, from a file or an
                           HTTP(S) URL of at most 10 MB. The overlay itself must be a raster
                           image; the picture it goes onto may be an SVG
      --watermark-position <POS>  Watermark placement (default: bottom-right)
                           center, top, right, bottom, left,
                           top-left, top-right, bottom-left, bottom-right
      --watermark-opacity <1-100> Watermark opacity percentage (default: 50)
      --watermark-margin <PX>     Margin from edge in pixels (default: 10)

EXAMPLES:
  truss photo.png -o photo.jpg --width 800
  truss --url https://example.com/img.png -o out.webp --format webp --quality 75
  cat photo.png | truss convert - -o - --format jpeg > photo.jpg
  truss photo.png -o thumb.png --width 200 --height 200 --fit cover
  truss diagram.svg -o safe.svg
  truss diagram.svg -o diagram.png --width 1024

SVG:
  An SVG output sanitizes the document and returns it as written, so an option that asks
  for a different picture is refused rather than ignored. Convert to a raster format to
  resize, rotate, or recolour a drawing; --fit, --position, and --without-enlargement then
  mean what they mean for any other input.

ORDER:
  Options are applied in a fixed order, whatever order they are written in:
  auto-orient, rotate, crop, resize, blur, sharpen, grayscale, watermark, encode.
  https://github.com/nao1215/truss/blob/main/docs/pipeline.md
";

const HELP_OPTIMIZE: &str = "\
truss optimize - reduce image file size with format-aware optimization

USAGE:
  truss optimize <INPUT> -o <OUTPUT> [OPTIONS]
  truss optimize --url <URL> -o <OUTPUT> [OPTIONS]
  truss optimize - -o - --format webp

OPTIONS:
  -o, --output <OUTPUT>    Output file path, or - for stdout (required)
      --url <URL>          Fetch input from an HTTP(S) URL
      --format <FMT>       Output format: jpeg, png, webp, avif
                           (default: inferred from output extension or input format)
      --mode <MODE>        Optimization mode: auto (default), lossless, lossy
                           For a plain re-encode with no optimization, use truss convert
                           lossless cannot rotate pixels, so a JPEG carrying an EXIF
                           orientation needs --keep-metadata or --preserve-exif
      --quality <1-100>    Optional quality cap for lossy optimization
      --target-quality <TARGET>
                           Perceptual target for lossy optimization (e.g. ssim:0.98, psnr:42)
      --auto-orient        Apply EXIF orientation and reset tag (default)
      --no-auto-orient     Skip EXIF orientation correction (add --keep-metadata to keep
                           the tag; stripping it as well leaves the image rotated)
      --strip-metadata     Remove all metadata (default; lossy optimization keeps the
                           ICC profile so colors are not shifted by the re-encode)
      --keep-metadata      Preserve EXIF, ICC, and other supported metadata
      --preserve-exif      Preserve EXIF only (strip ICC and others)

EXAMPLES:
  truss optimize photo.jpg -o out.jpg
  truss optimize photo.jpg -o out.jpg --mode lossy --target-quality ssim:0.98
  truss optimize graphic.png -o out.png --mode lossless
";

const HELP_INSPECT: &str = "\
truss inspect - show metadata of an image

USAGE:
  truss inspect <FILE>
  truss inspect --url <URL>
  truss inspect -               (read from stdin)

  Use -- to separate options from file paths starting with -:
    truss inspect -- -weird-name.png

OUTPUT:
  Prints JSON with format, MIME type, dimensions, alpha, and animation info.

  width/height are the dimensions as stored in the file. orientedWidth/orientedHeight
  are what 'truss convert' produces: an EXIF orientation of 5 to 8 transposes them, and
  orientation reports the tag when the file carries one.

EXAMPLES:
  truss inspect photo.jpg
  truss inspect --url https://example.com/photo.jpg
  cat photo.png | truss inspect -
";

fn help_serve() -> String {
    let mut s = String::from(
        "\
truss serve - start the HTTP image-transform server

USAGE:
  truss serve [OPTIONS]

  Server flags can also be used at the top level:
    truss --bind 0.0.0.0:8080 --storage-root /var/images

OPTIONS:
      --bind <ADDR>                   Listen address (default: 127.0.0.1:8080)
      --storage-root <PATH>           Root directory for path-based sources
      --public-base-url <URL>         External base URL for signed URLs
      --signed-url-key-id <KEY_ID>    Key identifier for signed public URLs
      --signed-url-secret <SECRET>    Shared secret for HMAC verification
      --allow-insecure-url-sources    Allow private-network URLs (dev/test only)

ENVIRONMENT VARIABLES:
  TRUSS_BIND_ADDR                     Listen address override
  TRUSS_STORAGE_ROOT                  Storage root override
  TRUSS_PUBLIC_BASE_URL               Public base URL override
  TRUSS_BEARER_TOKEN                  Private API authentication token
  TRUSS_SIGNED_URL_KEY_ID             Signing key identifier (single-key shorthand)
  TRUSS_SIGNED_URL_SECRET             Signing shared secret (single-key shorthand)
  TRUSS_SIGNING_KEYS                  Multiple signing keys as JSON {\"keyId\":\"secret\",...}
  TRUSS_ALLOW_INSECURE_URL_SOURCES    Enable insecure URL sources
  TRUSS_CACHE_ROOT                    On-disk transform cache directory
  TRUSS_PRESETS                       Named transform presets as inline JSON
  TRUSS_PRESETS_FILE                  Path to a JSON file containing named transform presets
",
    );

    // Build the TRUSS_STORAGE_BACKEND description dynamically based on enabled features.
    {
        use std::fmt::Write as FmtWrite;

        #[allow(unused_mut, clippy::useless_vec)]
        let mut backends = vec!["filesystem (default)"];
        #[cfg(feature = "s3")]
        backends.push("s3");
        #[cfg(feature = "gcs")]
        backends.push("gcs");
        #[cfg(feature = "azure")]
        backends.push("azure");
        let _ = writeln!(
            s,
            "  TRUSS_STORAGE_BACKEND               Source for public by-path resolution: {}",
            backends.join(", ")
        );
    }

    #[cfg(feature = "s3")]
    s.push_str(
        "  TRUSS_S3_BUCKET                     Default S3 bucket name (required when backend=s3)
  TRUSS_S3_FORCE_PATH_STYLE           Use path-style S3 addressing (set to 1/true/yes/on for MinIO, etc.)
  AWS_ACCESS_KEY_ID                   AWS access key for S3 authentication
  AWS_SECRET_ACCESS_KEY               AWS secret key for S3 authentication
  AWS_REGION                          AWS region for the S3 client (e.g. us-east-1)
  AWS_ENDPOINT_URL                    Custom S3-compatible endpoint URL (e.g. http://minio:9000)
",
    );

    #[cfg(feature = "gcs")]
    s.push_str(
        "  TRUSS_GCS_BUCKET                    Default GCS bucket name (required when backend=gcs)
  TRUSS_GCS_ENDPOINT                  Custom GCS endpoint URL (for testing with fake-gcs-server, etc.)
  GOOGLE_APPLICATION_CREDENTIALS      Path to GCS service account JSON key file
",
    );

    #[cfg(feature = "azure")]
    s.push_str(
        "  TRUSS_AZURE_CONTAINER               Default Azure container name (required when backend=azure)
  TRUSS_AZURE_ENDPOINT                Custom Azure Blob endpoint URL (for Azurite, etc.)
  AZURE_STORAGE_ACCOUNT_NAME          Storage account name (derives endpoint when TRUSS_AZURE_ENDPOINT is unset)
",
    );

    #[cfg(any(feature = "s3", feature = "gcs", feature = "azure"))]
    s.push_str(
        "  TRUSS_STORAGE_TIMEOUT_SECS          Download timeout for storage backends in seconds (default: 30, range: 1-300)
",
    );

    #[cfg(any(feature = "s3", feature = "gcs", feature = "azure"))]
    s.push_str(
        "\
\nNOTE: When using local emulators (MinIO, fake-gcs-server, Azurite), set
  TRUSS_ALLOW_INSECURE_URL_SOURCES=true to allow plain-HTTP endpoints.
",
    );

    s.push_str(
        "\
\nEXAMPLES:
  truss serve --bind 0.0.0.0:8080 --storage-root /var/images
  truss serve --bind 127.0.0.1:3000 --signed-url-key-id mykey --signed-url-secret s3cret
",
    );
    s
}

const HELP_SIGN: &str = "\
truss sign - generate a signed public URL

USAGE:
  truss sign --base-url <URL> --path <PATH> \\
    --key-id <KEY_ID> --secret <SECRET> --expires <UNIX_SECS> [OPTIONS]
  truss sign --base-url <URL> --url <URL> \\
    --key-id <KEY_ID> --secret <SECRET> --expires <UNIX_SECS> [OPTIONS]

REQUIRED:
      --base-url <URL>     CDN base URL for the signed request
      --path <PATH>        Image path on the server (mutually exclusive with --url)
      --url <URL>          Remote image URL to transform (mutually exclusive with --path)
      --key-id <KEY_ID>    Signing key identifier
      --secret <SECRET>    HMAC shared secret
      --expires <UNIX_SECS> Expiration as Unix timestamp

OPTIONAL:
      --version <VALUE>    Cache-busting version tag
      --width, --height, --fit, --position, --format, --quality,
      --optimize, --target-quality, --background, --rotate, --auto-orient, --no-auto-orient,
      --strip-metadata, --keep-metadata, --preserve-exif, --crop, --blur, --sharpen,
      --grayscale, --without-enlargement
      --watermark-url <URL>          Watermark image URL to embed in the signed URL
      --watermark-position <POS>     Watermark placement (default: bottom-right)
      --watermark-opacity <1-100>    Watermark opacity (default: 50)
      --watermark-margin <PX>        Watermark margin from edge in pixels (default: 10)
      --preset <NAME>                Named transform preset (server-side)

EXAMPLES:
  truss sign --base-url https://cdn.example.com \\
    --path /photos/hero.jpg --key-id mykey --secret s3cret \\
    --expires 1700000000 --width 640 --format webp
";

const HELP_VALIDATE: &str = "\
truss validate - check server configuration without starting the server

USAGE:
  truss validate

Parses and validates all environment variables used by `truss serve`.
Exits 0 when the configuration is valid, or exits 1 with a description
of each error found.

Useful in CI/CD pipelines to catch configuration mistakes early.
";

const HELP_COMPLETIONS: &str = "\
truss completions - generate shell completion scripts

USAGE:
  truss completions <SHELL>

SHELLS:
  bash, zsh, fish, elvish, powershell

EXAMPLES:
  truss completions bash > ~/.local/share/bash-completion/completions/truss
  truss completions zsh > ~/.zfunc/_truss
  truss completions fish > ~/.config/fish/completions/truss.fish
";

const HELP_VERSION: &str = "\
truss version - print version information

USAGE:
  truss version
  truss -V
  truss --version
";

// ---------------------------------------------------------------------------
// Clap derive structs
// ---------------------------------------------------------------------------

#[derive(Parser)]
#[command(
    name = "truss",
    about = "an image transformation tool and server",
    disable_help_subcommand = true,
    disable_help_flag = true,
    disable_version_flag = true
)]
struct Cli {
    #[command(subcommand)]
    command: Option<CliSubcommand>,
}

#[derive(Subcommand)]
enum CliSubcommand {
    /// Convert and transform an image file
    #[command(disable_help_flag = true)]
    Convert(ClapConvertArgs),
    /// Optimize an image for smaller output size
    #[command(disable_help_flag = true)]
    Optimize(ClapOptimizeArgs),
    /// Show metadata (format, dimensions, alpha) of an image
    #[command(disable_help_flag = true)]
    Inspect(ClapInspectArgs),
    /// Start the HTTP image-transform server
    #[command(disable_help_flag = true)]
    Serve(ClapServeArgs),
    /// Generate a signed public URL for the server
    #[command(disable_help_flag = true)]
    Sign(ClapSignArgs),
    /// Show help for a command
    Help { topic: Option<String> },
    /// Print version information
    Version,
    /// Validate server configuration without starting the server
    #[command(disable_help_flag = true)]
    Validate(ClapValidateArgs),
    /// Generate shell completion scripts
    #[command(disable_help_flag = true)]
    Completions {
        #[arg(value_enum)]
        shell: Option<clap_complete::Shell>,
        /// Print help
        #[arg(long)]
        help: bool,
    },
}

#[derive(clap::Args)]
struct ClapConvertArgs {
    /// Input file path, or - for stdin
    #[arg(allow_hyphen_values = true)]
    input: Option<PathBuf>,
    /// Output file path, or - for stdout
    #[arg(short = 'o', long = "output", allow_hyphen_values = true)]
    output: Option<PathBuf>,
    /// Fetch input from an HTTP(S) URL
    #[arg(long)]
    url: Option<String>,
    /// Target width in pixels
    #[arg(long, value_parser = parse_width)]
    width: Option<u32>,
    /// Target height in pixels
    #[arg(long, value_parser = parse_height)]
    height: Option<u32>,
    /// How to fit into target dimensions (contain, cover, fill, inside)
    #[arg(long, value_parser = parse_fit)]
    fit: Option<Fit>,
    /// Crop anchor for cover mode
    #[arg(long, value_parser = parse_position)]
    position: Option<Position>,
    /// Output format (jpeg, png, webp, avif, bmp, svg)
    #[arg(long, value_parser = parse_media_type)]
    format: Option<MediaType>,
    /// Encoding quality for lossy formats (1-100)
    #[arg(long, value_parser = parse_quality)]
    quality: Option<u8>,
    /// Optimization mode (none, auto, lossless, lossy)
    #[arg(long, value_parser = parse_optimize_mode)]
    optimize: Option<OptimizeMode>,
    /// Perceptual target for lossy optimization
    #[arg(long = "target-quality", value_parser = parse_target_quality)]
    target_quality: Option<TargetQuality>,
    /// Background color as RRGGBB or RRGGBBAA hex
    #[arg(long, value_parser = parse_background)]
    background: Option<Rgba8>,
    /// Rotate clockwise by whole degrees; negative turns counter-clockwise
    ///
    /// `allow_hyphen_values` is what lets `--rotate -90` through: without it clap reads
    /// the leading `-` as the start of another flag and rejects the value.
    #[arg(long, value_parser = parse_rotation, allow_hyphen_values = true)]
    rotate: Option<Rotation>,
    /// Apply EXIF orientation and reset tag
    #[arg(long)]
    auto_orient: bool,
    /// Skip EXIF orientation correction
    #[arg(long)]
    no_auto_orient: bool,
    /// Remove all metadata
    #[arg(long)]
    strip_metadata: bool,
    /// Preserve EXIF, ICC, and other supported metadata
    #[arg(long)]
    keep_metadata: bool,
    /// Preserve EXIF only (strip ICC and others)
    #[arg(long)]
    preserve_exif: bool,
    /// Explicit crop region as x,y,width,height
    #[arg(long, value_parser = parse_crop)]
    crop: Option<CropRegion>,
    /// Apply Gaussian blur (sigma: 0.1-100.0)
    #[arg(long, value_parser = parse_blur)]
    blur: Option<f32>,
    /// Apply sharpen filter (sigma: 0.1-100.0)
    #[arg(long, value_parser = parse_sharpen)]
    sharpen: Option<f32>,
    /// Desaturate the image to grayscale
    #[arg(long)]
    grayscale: bool,
    /// Never scale an image up to reach the requested size
    #[arg(long)]
    without_enlargement: bool,
    /// Watermark image file path
    #[arg(long)]
    watermark: Option<PathBuf>,
    /// Watermark position (default: bottom-right)
    #[arg(long, value_parser = parse_position)]
    watermark_position: Option<Position>,
    /// Watermark opacity 1-100 (default: 50)
    #[arg(long, value_parser = parse_watermark_opacity)]
    watermark_opacity: Option<u8>,
    /// Watermark margin in pixels (default: 10)
    #[arg(long, value_parser = parse_watermark_margin)]
    watermark_margin: Option<u32>,
    /// Show help for convert
    #[arg(short = 'h', long = "help")]
    help: bool,
}

#[derive(clap::Args)]
struct ClapOptimizeArgs {
    /// Input file path, or - for stdin
    #[arg(allow_hyphen_values = true)]
    input: Option<PathBuf>,
    /// Output file path, or - for stdout
    #[arg(short = 'o', long = "output", allow_hyphen_values = true)]
    output: Option<PathBuf>,
    /// Fetch input from an HTTP(S) URL
    #[arg(long)]
    url: Option<String>,
    /// Output format (jpeg, png, webp, avif)
    #[arg(long, value_parser = parse_optimizable_media_type)]
    format: Option<MediaType>,
    /// Quality cap for lossy optimization (1-100)
    #[arg(long, value_parser = parse_quality)]
    quality: Option<u8>,
    /// Optimization mode (auto, lossless, lossy)
    #[arg(long = "mode", value_parser = parse_optimizing_mode)]
    mode: Option<OptimizeMode>,
    /// Perceptual target for lossy optimization
    #[arg(long = "target-quality", value_parser = parse_target_quality)]
    target_quality: Option<TargetQuality>,
    /// Apply EXIF orientation and reset tag
    #[arg(long)]
    auto_orient: bool,
    /// Skip EXIF orientation correction
    #[arg(long)]
    no_auto_orient: bool,
    /// Remove all metadata
    #[arg(long)]
    strip_metadata: bool,
    /// Preserve EXIF, ICC, and other supported metadata
    #[arg(long)]
    keep_metadata: bool,
    /// Preserve EXIF only (strip ICC and others)
    #[arg(long)]
    preserve_exif: bool,
    /// Show help for optimize
    #[arg(short = 'h', long = "help")]
    help: bool,
}

#[derive(clap::Args)]
struct ClapInspectArgs {
    /// Input file path, or - for stdin
    #[arg(allow_hyphen_values = true)]
    input: Option<PathBuf>,
    /// Fetch input from an HTTP(S) URL
    #[arg(long)]
    url: Option<String>,
    /// Show help for inspect
    #[arg(short = 'h', long = "help")]
    help: bool,
}

#[derive(clap::Args)]
struct ClapServeArgs {
    /// Listen address (e.g. 0.0.0.0:8080)
    #[arg(long)]
    bind: Option<String>,
    /// Root directory for path-based sources
    #[arg(long)]
    storage_root: Option<PathBuf>,
    /// External base URL for signed URLs
    #[arg(long, value_parser = parse_url_value)]
    public_base_url: Option<String>,
    /// Key identifier for signed public URLs
    #[arg(long)]
    signed_url_key_id: Option<String>,
    /// Shared secret for HMAC verification
    #[arg(long)]
    signed_url_secret: Option<String>,
    /// Allow private-network URLs (dev/test only)
    #[arg(long)]
    allow_insecure_url_sources: bool,
    /// Show help for serve
    #[arg(short = 'h', long = "help")]
    help: bool,
}

#[derive(clap::Args)]
struct ClapValidateArgs {
    /// Show help for validate
    #[arg(short = 'h', long = "help")]
    help: bool,
}

#[derive(clap::Args)]
struct ClapSignArgs {
    /// CDN base URL for the signed request
    #[arg(long, value_parser = parse_url_value)]
    base_url: Option<String>,
    /// Image path on the server
    #[arg(long)]
    path: Option<String>,
    /// Remote image URL to transform
    #[arg(long, value_parser = parse_url_value)]
    url: Option<String>,
    /// Cache-busting version tag
    #[arg(long)]
    version: Option<String>,
    /// Signing key identifier
    #[arg(long)]
    key_id: Option<String>,
    /// HMAC shared secret
    #[arg(long)]
    secret: Option<String>,
    /// Expiration as Unix timestamp
    #[arg(long)]
    expires: Option<u64>,
    /// Target width in pixels
    #[arg(long, value_parser = parse_width)]
    width: Option<u32>,
    /// Target height in pixels
    #[arg(long, value_parser = parse_height)]
    height: Option<u32>,
    /// How to fit into target dimensions
    #[arg(long, value_parser = parse_fit)]
    fit: Option<Fit>,
    /// Crop anchor for cover mode
    #[arg(long, value_parser = parse_position)]
    position: Option<Position>,
    /// Output format
    #[arg(long, value_parser = parse_media_type)]
    format: Option<MediaType>,
    /// Encoding quality for lossy formats
    #[arg(long, value_parser = parse_quality)]
    quality: Option<u8>,
    /// Optimization mode (none, auto, lossless, lossy)
    #[arg(long, value_parser = parse_optimize_mode)]
    optimize: Option<OptimizeMode>,
    /// Perceptual target for lossy optimization
    #[arg(long = "target-quality", value_parser = parse_target_quality)]
    target_quality: Option<TargetQuality>,
    /// Background color as RRGGBB or RRGGBBAA hex
    #[arg(long, value_parser = parse_background)]
    background: Option<Rgba8>,
    /// Rotate clockwise by whole degrees; negative turns counter-clockwise
    ///
    /// `allow_hyphen_values` is what lets `--rotate -90` through: without it clap reads
    /// the leading `-` as the start of another flag and rejects the value.
    #[arg(long, value_parser = parse_rotation, allow_hyphen_values = true)]
    rotate: Option<Rotation>,
    /// Apply EXIF orientation
    #[arg(long)]
    auto_orient: bool,
    /// Skip EXIF orientation correction
    #[arg(long)]
    no_auto_orient: bool,
    /// Remove all metadata
    #[arg(long)]
    strip_metadata: bool,
    /// Preserve EXIF, ICC, and other metadata
    #[arg(long)]
    keep_metadata: bool,
    /// Preserve EXIF only
    #[arg(long)]
    preserve_exif: bool,
    /// Explicit crop region as x,y,width,height
    #[arg(long, value_parser = parse_crop)]
    crop: Option<CropRegion>,
    /// Apply Gaussian blur (sigma: 0.1-100.0)
    #[arg(long, value_parser = parse_blur)]
    blur: Option<f32>,
    /// Apply sharpen filter (sigma: 0.1-100.0)
    #[arg(long, value_parser = parse_sharpen)]
    sharpen: Option<f32>,
    /// Desaturate the image to grayscale
    #[arg(long)]
    grayscale: bool,
    /// Never scale an image up to reach the requested size
    #[arg(long)]
    without_enlargement: bool,
    /// Watermark image URL to composite onto the output
    #[arg(long, value_parser = parse_url_value)]
    watermark_url: Option<String>,
    /// Watermark placement (default: bottom-right)
    #[arg(long, value_parser = parse_position)]
    watermark_position: Option<Position>,
    /// Watermark opacity 1-100 (default: 50)
    #[arg(long, value_parser = parse_watermark_opacity)]
    watermark_opacity: Option<u8>,
    /// Watermark margin from edge in pixels (default: 10)
    #[arg(long, value_parser = parse_watermark_margin)]
    watermark_margin: Option<u32>,
    /// Named transform preset to apply
    #[arg(long)]
    preset: Option<String>,
    /// Show help for sign
    #[arg(short = 'h', long = "help")]
    help: bool,
}

// ---------------------------------------------------------------------------
// Clap value parsers for custom types
// ---------------------------------------------------------------------------

fn parse_fit(s: &str) -> Result<Fit, String> {
    Fit::from_str(s)
}

fn parse_position(s: &str) -> Result<Position, String> {
    Position::from_str(s)
}

/// Parses an output format, refusing formats truss can read but not write.
///
/// `--format gif` would otherwise parse cleanly and fail deep in the pipeline. Rejecting
/// it here puts the error next to the flag the user typed and names the alternatives.
fn parse_media_type(s: &str) -> Result<MediaType, String> {
    let media_type = MediaType::from_str(s)?;
    match media_type.unencodable_reason() {
        Some(reason) => Err(reason),
        None => Ok(media_type),
    }
}

fn parse_optimizable_media_type(s: &str) -> Result<MediaType, String> {
    let media_type = parse_media_type(s)?;
    if media_type.supports_optimization() {
        Ok(media_type)
    } else {
        Err(format!(
            "optimization is not supported for {} output",
            media_type.as_name()
        ))
    }
}

fn parse_optimize_mode(s: &str) -> Result<OptimizeMode, String> {
    OptimizeMode::from_str(s)
}

/// The modes `truss optimize` takes, which is every mode that optimizes.
///
/// `none` re-encodes without optimizing, which on a subcommand whose purpose is to shrink a
/// file is a way to make it bigger, and it skipped the format check the other three apply, so
/// a TIFF output passed when the format was inferred and failed when it was named.
/// `truss convert` is the command for a plain re-encode, and `OptimizeMode::None` stays the
/// default there and on the other three adapters, where it means what it says.
fn parse_optimizing_mode(s: &str) -> Result<OptimizeMode, String> {
    match OptimizeMode::from_str(s)? {
        OptimizeMode::None => Err(
            "`none` does not optimize; use `truss convert` for a plain re-encode, or one of auto, lossless, lossy"
                .to_string(),
        ),
        mode => Ok(mode),
    }
}

fn parse_target_quality(s: &str) -> Result<TargetQuality, String> {
    TargetQuality::from_str(s)
}

fn parse_rotation(s: &str) -> Result<Rotation, String> {
    Rotation::from_str(s)
}

fn parse_background(s: &str) -> Result<Rgba8, String> {
    Rgba8::from_hex(s)
}

fn parse_crop(s: &str) -> Result<CropRegion, String> {
    CropRegion::from_str(s)
}

fn parse_blur(s: &str) -> Result<f32, String> {
    let v: f32 = s
        .parse()
        .map_err(|_| format!("invalid blur value: '{s}'"))?;
    if !v.is_finite() || !(0.1..=100.0).contains(&v) {
        return Err("blur must be between 0.1 and 100.0".to_string());
    }
    Ok(v)
}

fn parse_sharpen(s: &str) -> Result<f32, String> {
    let v: f32 = s
        .parse()
        .map_err(|_| format!("invalid sharpen value: '{s}'"))?;
    if !v.is_finite() || !(0.1..=100.0).contains(&v) {
        return Err("sharpen must be between 0.1 and 100.0".to_string());
    }
    Ok(v)
}

/// Parses a quality, reporting the range truss documents whatever the number's width.
///
/// Parsed as an `i64` rather than as the `u8` the option is stored in, because clap would
/// otherwise refuse 256 with `256 is not in 0..=255`, a range that is the integer's and not
/// truss's, while 255 got `quality must be between 1 and 100`.
/// Parses a watermark margin the same way, so a number too large to be a count of pixels
/// says so rather than naming the integer it would be stored in.
fn parse_watermark_margin(s: &str) -> Result<u32, String> {
    parse_dimension(
        s,
        "watermark margin",
        crate::core::validate_watermark_margin_value,
    )
}

/// Parses a width, reporting the rule truss documents whatever the number's width.
///
/// Parsed as an `i64` rather than as the `u32` the option is stored in, because clap would
/// otherwise refuse 4294967296 with `4294967296 is not in 0..=4294967295`, the span of the
/// integer rather than anything truss publishes.
fn parse_width(s: &str) -> Result<u32, String> {
    parse_dimension(s, "width", crate::core::validate_width_value)
}

/// The same for a height. See [`parse_width`].
fn parse_height(s: &str) -> Result<u32, String> {
    parse_dimension(s, "height", crate::core::validate_height_value)
}

fn parse_dimension(
    s: &str,
    axis: &str,
    validate: fn(i64) -> Result<u32, &'static str>,
) -> Result<u32, String> {
    let value: i64 = s
        .parse()
        .map_err(|_| format!("{axis} must be a whole number of pixels, got '{s}'"))?;
    // A value the option can hold is handed on for the transform to judge, which keeps the
    // failure class the CLI reported before and the one the other adapters report.
    validate(value).map_err(str::to_string)
}

fn parse_quality(s: &str) -> Result<u8, String> {
    let value: i64 = s
        .parse()
        .map_err(|_| format!("quality must be a whole number, got '{s}'"))?;
    // A value the option can hold is handed on for `TransformOptions::normalize` to judge,
    // which keeps the failure class the CLI reported before and the one the server reports
    // for the same number. One that cannot be held is refused here, with the sentence that
    // check would have given rather than with the range of the integer holding it.
    u8::try_from(value).map_err(|_| {
        crate::core::validate_quality_value(value)
            .expect_err("a value outside u8 is outside 1..=100")
            .to_string()
    })
}

fn parse_watermark_opacity(s: &str) -> Result<u8, String> {
    let value: i64 = s
        .parse()
        .map_err(|_| format!("watermark opacity must be a whole number, got '{s}'"))?;
    crate::core::validate_watermark_opacity_value(value).map_err(str::to_string)
}

fn parse_url_value(s: &str) -> Result<String, String> {
    let parsed = url::Url::parse(s).map_err(|e| format!("invalid URL: {e}"))?;
    match parsed.scheme() {
        "http" | "https" => Ok(s.to_string()),
        _ => Err(format!("requires an http:// or https:// URL, got '{s}'")),
    }
}

// ---------------------------------------------------------------------------
// Usage strings (reused in errors)
// ---------------------------------------------------------------------------

fn convert_usage() -> &'static str {
    "usage: truss convert <INPUT> -o <OUTPUT> [OPTIONS]"
}

fn optimize_usage() -> &'static str {
    "usage: truss optimize <INPUT> -o <OUTPUT> [OPTIONS]"
}

fn inspect_usage() -> &'static str {
    "usage: truss inspect <FILE|--url URL|->"
}

fn serve_usage() -> &'static str {
    "usage: truss serve [--bind ADDR] [--storage-root PATH] [OPTIONS]"
}

fn sign_usage() -> &'static str {
    "usage: truss sign --base-url <URL> (--path <PATH>|--url <URL>) --key-id <ID> --secret <SECRET> --expires <UNIX_SECS>"
}

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Runs the command-line adapter and returns a process exit code.
///
/// This function is the stable entry point for the CLI adapter. It parses command-line
/// arguments, dispatches the selected subcommand, writes output to the process streams,
/// and converts adapter-specific failures into the documented numeric exit codes.
///
/// Standard output is flushed before returning, so a write that only fails when the
/// buffer drains — a full disk, a quota, a reader that closed the pipe — is reported as
/// exit code 5 instead of being discarded by the runtime's exit-time flush.
///
/// # Examples
///
/// ```no_run
/// use truss::adapters::cli;
///
/// let _ = cli::run(vec![
///     "truss".to_string(),
///     "input.png".to_string(),
///     "-o".to_string(),
///     "output.jpg".to_string(),
/// ]);
/// ```
///
/// ```no_run
/// use truss::adapters::cli;
///
/// let _ = cli::run(vec![
///     "truss".to_string(),
///     "--bind".to_string(),
///     "127.0.0.1:8080".to_string(),
/// ]);
/// ```
pub fn run<I>(args: I) -> ExitCode
where
    I: IntoIterator<Item: Into<OsString>>,
{
    let stdin = io::stdin();
    let stdout = io::stdout();
    let stderr = io::stderr();
    let mut stdin = stdin.lock();
    let mut stdout = stdout.lock();
    let mut stderr = stderr.lock();

    let code = run_with_io(args, &mut stdin, &mut stdout, &mut stderr);

    ExitCode::from(flush_stdout(code, &mut stdout, &mut stderr))
}

/// Flushes standard output and folds a flush failure into the exit code.
///
/// `StdoutLock` buffers. A payload that ends without a newline — a small WebP or AVIF
/// written to `-o -` — can sit entirely in that buffer, so the only write that reaches
/// the file descriptor is the runtime's flush after `main` returns, and nothing observes
/// its error. Flushing here turns that silent truncation into exit code 5 with a reason.
///
/// A command that already failed keeps its own exit code: the flush error is a
/// consequence of the first failure, not a second, more informative one.
fn flush_stdout<W, E>(code: u8, stdout: &mut W, stderr: &mut E) -> u8
where
    W: Write,
    E: Write,
{
    match stdout.flush() {
        Ok(()) => code,
        Err(error) if code == EXIT_SUCCESS => write_error(stderr, stdout_write_error(&error)),
        Err(_) => code,
    }
}

// ---------------------------------------------------------------------------
// Command types (internal)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
enum HelpTopic {
    TopLevel,
    Convert,
    Optimize,
    Inspect,
    Serve,
    Validate,
    Sign,
    Completions,
    Version,
}

#[derive(Debug, Clone, PartialEq)]
enum Command {
    Help(HelpTopic),
    Version,
    Serve(ServeCommand),
    Validate,
    Inspect(InspectCommand),
    Convert(ConvertCommand),
    Optimize(ConvertCommand),
    Sign(SignCommand),
    Completions(clap_complete::Shell),
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ServeCommand {
    bind_addr: Option<String>,
    storage_root: Option<PathBuf>,
    public_base_url: Option<String>,
    signed_url_key_id: Option<String>,
    signed_url_secret: Option<String>,
    allow_insecure_url_sources: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct InspectCommand {
    input: InputSource,
}

#[derive(Debug, Clone, PartialEq)]
struct ConvertCommand {
    input: InputSource,
    output: OutputTarget,
    options: TransformOptions,
    watermark_path: Option<PathBuf>,
    watermark_position: Option<Position>,
    watermark_opacity: Option<u8>,
    watermark_margin: Option<u32>,
}

#[derive(Debug, Clone, PartialEq)]
struct SignCommand {
    base_url: String,
    source: SignedUrlSource,
    key_id: String,
    secret: String,
    expires: u64,
    options: TransformOptions,
    watermark_url: Option<String>,
    watermark_position: Option<Position>,
    watermark_opacity: Option<u8>,
    watermark_margin: Option<u32>,
    preset: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum InputSource {
    Stdin,
    Path(PathBuf),
    Url(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum OutputTarget {
    Stdout,
    Path(PathBuf),
}

// ---------------------------------------------------------------------------
// Structured error
// ---------------------------------------------------------------------------

/// A failure on its way to standard error, carrying both halves of how the CLI reports one.
///
/// `exit_code` is the coarse signal a shell branches on, one of the five in CONTRIBUTING.md.
/// `class` is the same failure named the way the HTTP server and the Wasm package name it,
/// so a caller who moves a transform between the three adapters keeps one classification;
/// it is what `write_error` prints in parentheses.
#[derive(Debug, Clone, PartialEq, Eq)]
struct CliError {
    exit_code: u8,
    class: ErrorClass,
    message: String,
    usage: Option<String>,
    hint: Option<String>,
}

// ---------------------------------------------------------------------------
// Core dispatch
// ---------------------------------------------------------------------------

fn run_with_io<I, R, W, E>(args: I, stdin: &mut R, stdout: &mut W, stderr: &mut E) -> u8
where
    I: IntoIterator<Item: Into<OsString>>,
    R: Read,
    W: Write,
    E: Write,
{
    match parse_args(args) {
        Ok(Command::Help(topic)) => {
            let text = match topic {
                HelpTopic::TopLevel => help_top_level(),
                HelpTopic::Convert => HELP_CONVERT.to_string(),
                HelpTopic::Optimize => HELP_OPTIMIZE.to_string(),
                HelpTopic::Inspect => HELP_INSPECT.to_string(),
                HelpTopic::Serve => help_serve(),
                HelpTopic::Validate => HELP_VALIDATE.to_string(),
                HelpTopic::Sign => HELP_SIGN.to_string(),
                HelpTopic::Completions => HELP_COMPLETIONS.to_string(),
                HelpTopic::Version => HELP_VERSION.to_string(),
            };
            match stdout.write_all(text.as_bytes()) {
                Ok(()) => EXIT_SUCCESS,
                Err(error) => write_error(stderr, stdout_write_error(&error)),
            }
        }
        Ok(Command::Version) => match writeln!(stdout, "truss {}", env!("CARGO_PKG_VERSION")) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, stdout_write_error(&error)),
        },
        Ok(Command::Serve(command)) => match serve::execute_serve(command) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, error),
        },
        Ok(Command::Validate) => match serve::execute_validate(stdout) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, error),
        },
        Ok(Command::Inspect(command)) => match inspect::execute_inspect(command, stdin, stdout) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, error),
        },
        Ok(Command::Convert(command) | Command::Optimize(command)) => {
            match convert::execute_convert(command, stdin, stdout) {
                Ok(()) => EXIT_SUCCESS,
                Err(error) => write_error(stderr, error),
            }
        }
        Ok(Command::Sign(command)) => match sign::execute_sign(command, stdout) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, error),
        },
        Ok(Command::Completions(shell)) => match generate_completions(shell, stdout) {
            Ok(()) => EXIT_SUCCESS,
            Err(error) => write_error(stderr, error),
        },
        Err(error) => write_error(stderr, error),
    }
}

// ---------------------------------------------------------------------------
// Argument preprocessing for implicit convert / serve
// ---------------------------------------------------------------------------

const KNOWN_SUBCOMMANDS: &[&str] = &[
    "convert",
    "optimize",
    "inspect",
    "serve",
    "validate",
    "sign",
    "help",
    "completions",
    "version",
];

fn is_serve_flag(value: &str) -> bool {
    matches!(
        value,
        "--bind"
            | "--storage-root"
            | "--public-base-url"
            | "--signed-url-key-id"
            | "--signed-url-secret"
            | "--allow-insecure-url-sources"
    )
}

/// Returns `true` when a token looks like it was meant to be a subcommand name
/// (starts with a letter, no path separators, no file extension).
fn looks_like_unknown_subcommand(value: &str) -> bool {
    if value.starts_with('-') || value.starts_with('/') || value.starts_with('.') {
        return false;
    }
    if value.contains('.') || value.contains('/') || value.contains('\\') {
        return false;
    }
    value
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

/// Pre-processes raw args to handle implicit convert and implicit serve
/// before handing off to clap.
/// Reports whether a path argument is the single dash that names standard input or output.
///
/// The comparison is on the bytes rather than on a `str`, so a path that is not text
/// reaches the file system rather than being read as a stream.
fn is_dash(path: &Path) -> bool {
    path.as_os_str() == OsStr::new("-")
}

fn preprocess_args(args: Vec<OsString>) -> Vec<OsString> {
    if args.len() <= 1 {
        return args;
    }
    let first = &args[1];
    // A path is bytes on Unix, so an argument that is not text is still a file truss can
    // read. Only the routing decisions below need it as text, and every one of them is a
    // comparison against a name truss chose, which no such argument can match.
    let first_text = first.to_str();

    // -h / --help at top level → route to our help subcommand
    if first == "-h" || first == "--help" {
        let mut new = vec![args[0].clone(), OsString::from("help")];
        if args.len() > 2 {
            new.extend_from_slice(&args[2..]);
        }
        return new;
    }

    // -V / --version at top level → route to version subcommand
    if first == "-V" || first == "--version" {
        return vec![args[0].clone(), OsString::from("version")];
    }

    // If first arg is a serve flag, insert "serve" subcommand
    if first_text.is_some_and(is_serve_flag) {
        let mut new = vec![args[0].clone(), OsString::from("serve")];
        new.extend_from_slice(&args[1..]);
        return new;
    }

    // Known subcommand → pass through
    if first_text.is_some_and(|first| KNOWN_SUBCOMMANDS.contains(&first)) {
        return args;
    }

    // If the first argument refers to an existing file (even without an
    // extension), treat it as an implicit convert rather than an unknown
    // subcommand.  This handles `truss image -o out.jpg` where `image` is a
    // real file.
    if std::path::Path::new(first).is_file() {
        let mut new = vec![args[0].clone(), OsString::from("convert")];
        new.extend_from_slice(&args[1..]);
        return new;
    }

    // Looks like an unknown subcommand (alphabetic, no dots/slashes) →
    // let clap handle it for typo suggestions
    if first_text.is_some_and(looks_like_unknown_subcommand) {
        return args;
    }

    // Otherwise, treat as implicit convert
    let mut new = vec![args[0].clone(), OsString::from("convert")];
    new.extend_from_slice(&args[1..]);
    new
}

// ---------------------------------------------------------------------------
// Argument parsing — main entry
// ---------------------------------------------------------------------------

fn parse_args<I>(args: I) -> Result<Command, CliError>
where
    I: IntoIterator<Item: Into<OsString>>,
{
    let raw: Vec<OsString> = args.into_iter().map(Into::into).collect();

    // Bare invocation → top-level help (exit 0)
    if raw.len() <= 1 {
        return Ok(Command::Help(HelpTopic::TopLevel));
    }

    let preprocessed = preprocess_args(raw);
    let cli = Cli::try_parse_from(&preprocessed).map_err(map_clap_error)?;

    match cli.command {
        None => Ok(Command::Help(HelpTopic::TopLevel)),
        Some(CliSubcommand::Help { topic }) => parse_help_topic(topic),
        Some(CliSubcommand::Version) => Ok(Command::Version),
        Some(CliSubcommand::Completions { help: true, .. }) => {
            Ok(Command::Help(HelpTopic::Completions))
        }
        Some(CliSubcommand::Completions {
            shell: Some(shell), ..
        }) => Ok(Command::Completions(shell)),
        Some(CliSubcommand::Completions {
            shell: None,
            help: false,
        }) => Err(CliError {
            exit_code: EXIT_USAGE,
            class: ErrorClass::InvalidRequest,
            message: "'completions' requires a shell argument".to_string(),
            usage: None,
            hint: Some("try 'truss completions bash'".to_string()),
        }),
        Some(CliSubcommand::Convert(args)) => convert::convert_from_clap(args),
        Some(CliSubcommand::Optimize(args)) => convert::optimize_from_clap(args),
        Some(CliSubcommand::Inspect(args)) => inspect::inspect_from_clap(args),
        Some(CliSubcommand::Serve(args)) => serve::serve_from_clap(args),
        Some(CliSubcommand::Validate(args)) => serve::validate_from_clap(args),
        Some(CliSubcommand::Sign(args)) => sign::sign_from_clap(args),
    }
}

/// Maps a clap error into a structured `CliError`.
fn map_clap_error(err: clap::Error) -> CliError {
    let raw = err.to_string();
    // clap renders "error: ..." — strip that prefix since write_error adds its own
    let message = raw
        .strip_prefix("error: ")
        .unwrap_or(&raw)
        .trim()
        .to_string();

    CliError {
        exit_code: EXIT_USAGE,
        class: ErrorClass::InvalidRequest,
        message,
        usage: None,
        hint: Some("run 'truss --help' for available commands".to_string()),
    }
}

fn parse_help_topic(topic: Option<String>) -> Result<Command, CliError> {
    match topic.as_deref() {
        None => Ok(Command::Help(HelpTopic::TopLevel)),
        Some("convert") => Ok(Command::Help(HelpTopic::Convert)),
        Some("optimize") => Ok(Command::Help(HelpTopic::Optimize)),
        Some("inspect") => Ok(Command::Help(HelpTopic::Inspect)),
        Some("serve") => Ok(Command::Help(HelpTopic::Serve)),
        Some("validate") => Ok(Command::Help(HelpTopic::Validate)),
        Some("sign") => Ok(Command::Help(HelpTopic::Sign)),
        Some("completions") => Ok(Command::Help(HelpTopic::Completions)),
        Some("version") => Ok(Command::Help(HelpTopic::Version)),
        Some(other) => Err(CliError {
            exit_code: EXIT_USAGE,
            class: ErrorClass::InvalidRequest,
            message: format!("unknown help topic '{other}'"),
            usage: None,
            hint: Some(
                "available topics: convert, optimize, inspect, serve, validate, sign, completions, version"
                    .to_string(),
            ),
        }),
    }
}

// ---------------------------------------------------------------------------
// Shared transform fields
// ---------------------------------------------------------------------------

/// Collects shared transform fields from clap args into `TransformOptions`.
struct TransformFields {
    width: Option<u32>,
    height: Option<u32>,
    fit: Option<Fit>,
    position: Option<Position>,
    format: Option<MediaType>,
    quality: Option<u8>,
    optimize: Option<OptimizeMode>,
    target_quality: Option<TargetQuality>,
    background: Option<Rgba8>,
    rotate: Option<Rotation>,
    auto_orient: bool,
    no_auto_orient: bool,
    strip_metadata: bool,
    keep_metadata: bool,
    preserve_exif: bool,
    crop: Option<CropRegion>,
    blur: Option<f32>,
    sharpen: Option<f32>,
    grayscale: bool,
    without_enlargement: bool,
}

impl TransformFields {
    fn into_options(self) -> Result<TransformOptions, crate::TransformError> {
        let defaults = TransformOptions::default();
        let auto_orient = if self.no_auto_orient {
            false
        } else if self.auto_orient {
            true
        } else {
            defaults.auto_orient
        };
        let (strip_metadata, preserve_exif) = crate::resolve_metadata_flags(
            if self.strip_metadata {
                Some(true)
            } else {
                None
            },
            if self.keep_metadata { Some(true) } else { None },
            if self.preserve_exif { Some(true) } else { None },
        )?;
        Ok(TransformOptions {
            width: self.width,
            height: self.height,
            fit: self.fit,
            position: self.position,
            format: self.format,
            quality: self.quality,
            optimize: self.optimize.unwrap_or(defaults.optimize),
            target_quality: self.target_quality,
            background: self.background,
            rotate: self.rotate.unwrap_or(defaults.rotate),
            auto_orient,
            strip_metadata,
            preserve_exif,
            crop: self.crop,
            blur: self.blur,
            sharpen: self.sharpen,
            grayscale: self.grayscale,
            without_enlargement: self.without_enlargement,
            deadline: None,
        })
    }
}

fn validate_url(url: &str, flag: &str) -> Result<(), CliError> {
    let parsed = url::Url::parse(url).map_err(|e| CliError {
        exit_code: EXIT_USAGE,
        class: ErrorClass::InvalidRequest,
        message: format!("'{flag}' is not a valid URL: {e}"),
        usage: None,
        hint: Some(format!("got '{url}'")),
    })?;
    match parsed.scheme() {
        "http" | "https" => Ok(()),
        _ => Err(CliError {
            exit_code: EXIT_USAGE,
            class: ErrorClass::InvalidRequest,
            message: format!("'{flag}' requires an http:// or https:// URL"),
            usage: None,
            hint: Some(format!("got '{url}'")),
        }),
    }
}

/// Generates shell completion scripts for the given shell.
fn generate_completions<W: Write>(
    shell: clap_complete::Shell,
    stdout: &mut W,
) -> Result<(), CliError> {
    let mut cmd = Cli::command();

    // Add implicit-convert positional argument and common flags so that shell
    // completions expose the shorthand forms documented in the help text
    // (e.g. `truss photo.png -o out.jpg`, `truss --bind 0.0.0.0:8080`).
    cmd = cmd
        .arg(
            clap::Arg::new("INPUT")
                .help("Input image file (implicit convert)")
                .value_hint(clap::ValueHint::FilePath),
        )
        .arg(
            clap::Arg::new("output")
                .short('o')
                .long("output")
                .help("Output file path (implicit convert)")
                .value_hint(clap::ValueHint::FilePath),
        )
        .arg(
            clap::Arg::new("bind")
                .long("bind")
                .help("Listen address (implicit serve)"),
        )
        .arg(
            clap::Arg::new("storage-root")
                .long("storage-root")
                .help("Root directory for path-based sources (implicit serve)")
                .value_hint(clap::ValueHint::DirPath),
        );

    clap_complete::generate(shell, &mut cmd, "truss", stdout);
    Ok(())
}

// ---------------------------------------------------------------------------
// I/O helpers
// ---------------------------------------------------------------------------

fn read_input_bytes<R>(input: InputSource, stdin: &mut R) -> Result<Vec<u8>, CliError>
where
    R: Read,
{
    match input {
        InputSource::Stdin => {
            let mut bytes = Vec::new();
            stdin.read_to_end(&mut bytes).map_err(|error| {
                runtime_error(EXIT_IO, &format!("failed to read stdin: {error}"))
            })?;
            Ok(bytes)
        }
        InputSource::Path(path) => fs::read(&path).map_err(|error| {
            classified_error(
                class_for_io_error(&error),
                EXIT_IO,
                &format!("failed to read {}: {error}", path.display()),
            )
        }),
        InputSource::Url(url) => read_url_bytes(&url, MAX_REMOTE_BYTES),
    }
}

/// Names the class of a file system fault.
///
/// A source that is not there is `not-found`, the class the server gives the same miss and
/// the one `docs/problems.md` describes as an input file that is not there; anything else
/// about the file system is `internal-error`. Every path the command line names is read
/// through this, so a mistyped `--watermark` is classified like a mistyped input.
fn class_for_io_error(error: &io::Error) -> ErrorClass {
    if error.kind() == io::ErrorKind::NotFound {
        ErrorClass::NotFound
    } else {
        ErrorClass::InternalError
    }
}

/// Timeout for the TCP connect phase of a remote fetch.
const CLI_FETCH_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
/// Timeout for receiving the full response body from a remote source.
const CLI_FETCH_BODY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
/// How many redirects a `--url` fetch follows before giving up.
///
/// The same number the server allows, so a URL that works against one works against the
/// other.
const CLI_FETCH_MAX_REDIRECTS: u32 = 5;

/// Refuses a URL truss may not fetch, whichever hop of a redirect chain named it.
///
/// A command line is expected to fetch from the machine it runs on, so the private and
/// loopback ranges the server refuses are allowed here. The metadata endpoints are not:
/// `docs/configuration.md` calls them blocked whatever else is configured, no workflow
/// fetches an image from one, and the URL that reaches one is chosen by whatever server
/// answered the previous hop rather than by whoever typed the command.
///
/// The name is checked, and so are the addresses it resolves to, which covers a host that
/// points at a metadata address without spelling it. A host whose answer changes between
/// this lookup and the agent's own is not covered: the server closes that with DNS pinning,
/// which it can only do because it refuses every private address, and this adapter does not.
fn refuse_disallowed_fetch_target(url: &str) -> Result<(), CliError> {
    let parsed = url::Url::parse(url).map_err(|error| {
        classified_error(
            ErrorClass::BadGateway,
            EXIT_IO,
            &format!("failed to fetch {url}: not a valid URL: {error}"),
        )
    })?;
    if !matches!(parsed.scheme(), "http" | "https") {
        return Err(classified_error(
            ErrorClass::BadGateway,
            EXIT_IO,
            &format!(
                "failed to fetch {url}: a redirect to a `{}` URL is not followed",
                parsed.scheme()
            ),
        ));
    }
    let named_metadata = crate::core::remote_policy::is_cloud_metadata_host(&parsed);
    // A host name under someone else's control can resolve to a metadata address without
    // spelling it, so the addresses it resolves to are checked as well.
    let resolves_to_metadata = !named_metadata
        && parsed
            .socket_addrs(|| Some(if parsed.scheme() == "https" { 443 } else { 80 }))
            .map(|addrs| {
                addrs
                    .iter()
                    .any(|addr| crate::core::remote_policy::is_cloud_metadata_ip(addr.ip()))
            })
            .unwrap_or(false);
    if named_metadata || resolves_to_metadata {
        return Err(classified_error(
            ErrorClass::BadGateway,
            EXIT_IO,
            &format!("failed to fetch {url}: the URL points to a cloud metadata service"),
        ));
    }
    Ok(())
}

/// Fetches `--url` input.
///
/// Redirects are followed here rather than inside the agent so that every hop is checked:
/// the caller chooses the first URL and a remote server chooses the rest.
///
/// Every failure here is the remote end's, which the server names `bad-gateway`; the CLI
/// keeps its I/O exit code (2) and adds that class.
fn read_url_bytes(url: &str, max_bytes: u64) -> Result<Vec<u8>, CliError> {
    let fetch_failed =
        |message: String| classified_error(ErrorClass::BadGateway, EXIT_IO, &message);
    let config = ureq::config::Config::builder()
        .timeout_connect(Some(CLI_FETCH_CONNECT_TIMEOUT))
        .timeout_recv_body(Some(CLI_FETCH_BODY_TIMEOUT))
        .http_status_as_error(false)
        .max_redirects(0)
        .build();
    let agent = ureq::Agent::new_with_config(config);

    let mut current = url.to_string();
    let mut response = None;
    for _ in 0..=CLI_FETCH_MAX_REDIRECTS {
        refuse_disallowed_fetch_target(&current)?;
        let hop = agent
            .get(&current)
            .call()
            .map_err(|error| fetch_failed(format!("failed to fetch {current}: {error}")))?;
        let status = hop.status().as_u16();
        if matches!(status, 301 | 302 | 303 | 307 | 308) {
            let location = hop
                .headers()
                .get("Location")
                .and_then(|value: &ureq::http::HeaderValue| value.to_str().ok())
                .ok_or_else(|| {
                    fetch_failed(format!(
                        "failed to fetch {current}: HTTP {status} without a Location header"
                    ))
                })?;
            let base = url::Url::parse(&current).map_err(|error| {
                fetch_failed(format!(
                    "failed to fetch {current}: not a valid URL: {error}"
                ))
            })?;
            let next = base.join(location).map_err(|error| {
                fetch_failed(format!(
                    "failed to fetch {current}: redirect to an unusable URL: {error}"
                ))
            })?;
            current = next.into();
            continue;
        }
        response = Some((hop, status));
        break;
    }

    let Some((response, status)) = response else {
        return Err(fetch_failed(format!(
            "failed to fetch {url}: more than {CLI_FETCH_MAX_REDIRECTS} redirects"
        )));
    };

    if status >= 400 {
        return Err(fetch_failed(format!(
            "failed to fetch {current}: HTTP {status}"
        )));
    }

    if response
        .headers()
        .get("Content-Length")
        .and_then(|v: &ureq::http::HeaderValue| v.to_str().ok())
        .and_then(|value: &str| value.parse::<u64>().ok())
        .is_some_and(|len| len > max_bytes)
    {
        return Err(fetch_failed(format!(
            "failed to fetch {url}: response exceeds {max_bytes} bytes"
        )));
    }

    // The declared length is a claim; this is the measurement, so a response that declares
    // nothing, or declares a small number and sends more, is bounded by the same cap.
    let mut reader = response.into_body().into_reader().take(max_bytes + 1);
    let mut bytes = Vec::new();
    reader
        .read_to_end(&mut bytes)
        .map_err(|error| fetch_failed(format!("failed to fetch {url}: {error}")))?;

    if bytes.len() as u64 > max_bytes {
        return Err(fetch_failed(format!(
            "failed to fetch {url}: response exceeds {max_bytes} bytes"
        )));
    }

    Ok(bytes)
}

/// Maps a transform failure onto the exit code and the class that name it.
///
/// The class is [`crate::TransformError::class`], the table the HTTP server and the Wasm
/// package read too, so the three adapters classify one failure the same way. The exit code
/// is the CLI's own column of that table and `map_transform_error_matches_the_class_table`
/// is what keeps the two in step with `docs/problems.md`.
fn map_transform_error(error: crate::TransformError) -> CliError {
    let class = error.class();
    let (exit_code, message) = match error {
        crate::TransformError::InvalidOptions(reason) => (EXIT_USAGE, reason),
        crate::TransformError::InvalidInput(reason) => (EXIT_INPUT, reason),
        // An input truss cannot process is an input error (3), the same class the
        // documented exit-code table gives an unsupported format, not a transform
        // failure (4).
        crate::TransformError::UnsupportedInputMediaType(reason) => (EXIT_INPUT, reason),
        crate::TransformError::DecodeFailed(reason)
        | crate::TransformError::EncodeFailed(reason)
        | crate::TransformError::CapabilityMissing(reason)
        | crate::TransformError::LimitExceeded(reason) => (EXIT_TRANSFORM, reason),
        // The error's own Display names the rule that was hit (svg needs an svg input,
        // gif is never encoded), so the CLI does not restate it in different words.
        ref error @ crate::TransformError::UnsupportedOutputMediaType(_) => {
            (EXIT_TRANSFORM, error.to_string())
        }
    };
    classified_error(class, exit_code, &message)
}

// ---------------------------------------------------------------------------
// Error constructors
// ---------------------------------------------------------------------------

fn convert_error(message: &str) -> CliError {
    CliError {
        exit_code: EXIT_USAGE,
        class: ErrorClass::InvalidRequest,
        message: message.to_string(),
        usage: Some(convert_usage().to_string()),
        hint: Some("run 'truss convert --help' for convert options".to_string()),
    }
}

fn optimize_error(message: &str) -> CliError {
    CliError {
        exit_code: EXIT_USAGE,
        class: ErrorClass::InvalidRequest,
        message: message.to_string(),
        usage: Some(optimize_usage().to_string()),
        hint: Some("run 'truss optimize --help' for optimize options".to_string()),
    }
}

fn sign_error(message: &str) -> CliError {
    CliError {
        exit_code: EXIT_USAGE,
        class: ErrorClass::InvalidRequest,
        message: message.to_string(),
        usage: Some(sign_usage().to_string()),
        hint: Some("run 'truss sign --help' for sign options".to_string()),
    }
}

/// A command line that could not be understood or is contradictory: exit 1, the
/// `invalid-request` class, with no usage block of its own.
fn usage_error(message: &str) -> CliError {
    classified_error(ErrorClass::InvalidRequest, EXIT_USAGE, message)
}

/// A failure the process could not avoid: an I/O fault (exit 2) or a runtime fault such as
/// a port already in use or a closed standard output (exit 5).
///
/// Both are the `internal-error` class, which is what the server reports for the same
/// faults. The I/O paths that know more than that — a source that is not there, a fetch the
/// remote end refused — say so with [`classified_error`] instead.
fn runtime_error(exit_code: u8, message: &str) -> CliError {
    classified_error(ErrorClass::InternalError, exit_code, message)
}

/// Builds an error whose class the caller names.
fn classified_error(class: ErrorClass, exit_code: u8, message: &str) -> CliError {
    CliError {
        exit_code,
        class,
        message: message.to_string(),
        usage: None,
        hint: None,
    }
}

/// Builds the error a failed write to standard output reports.
///
/// `help` and `version` used to return exit code 5 with nothing on stderr, so a redirect
/// into a full disk looked like a silent failure while every other command explained
/// itself. They now report the same way `convert` and `inspect` do.
fn stdout_write_error(error: &io::Error) -> CliError {
    runtime_error(EXIT_RUNTIME, &format!("failed to write stdout: {error}"))
}

fn write_error<E>(stderr: &mut E, error: CliError) -> u8
where
    E: Write,
{
    let _ = writeln!(
        stderr,
        "error: {} ({})",
        crate::core::single_line(&error.message),
        error.class.slug()
    );
    if let Some(usage) = &error.usage {
        let _ = writeln!(stderr, "{usage}");
    }
    if let Some(hint) = &error.hint {
        let _ = writeln!(stderr, "hint: {hint}");
    }
    error.exit_code
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::serve::resolve_server_config;
    use super::{
        Command, ConvertCommand, EXIT_INPUT, EXIT_TRANSFORM, EXIT_USAGE, HelpTopic, InputSource,
        OutputTarget, ServeCommand, SignCommand, flush_stdout, parse_args, parse_optimize_mode,
        parse_optimizing_mode, preprocess_args, run_with_io,
    };
    use crate::{
        Fit, MediaType, OptimizeMode, RawArtifact, SignedUrlSource, TransformOptions,
        sniff_artifact,
    };
    use serial_test::serial;
    use std::env;
    use std::ffi::OsString;
    use std::fs;
    use std::io::{self, Cursor, Read, Write};
    use std::net::TcpListener;
    use std::path::PathBuf;
    use std::thread;

    fn png_bytes() -> Vec<u8> {
        crate::test_support::flat_png(4, 3)
    }

    fn temp_file_path(name: &str) -> PathBuf {
        crate::test_support::unique_temp_path(&format!("truss-{name}")).with_extension("bin")
    }

    fn temp_dir(name: &str) -> PathBuf {
        let path = crate::test_support::unique_temp_path(&format!("truss-{name}"));
        fs::create_dir_all(&path).expect("create temp dir");
        path
    }

    fn spawn_http_server(
        body: Vec<u8>,
        content_type: &'static str,
    ) -> (String, thread::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind http test server");
        let addr = listener.local_addr().expect("server addr");
        let url = format!("http://{addr}/image");

        let handle = thread::spawn(move || {
            let (mut stream, _) = listener.accept().expect("accept connection");
            let mut request = [0_u8; 1024];
            let _ = stream.read(&mut request);
            let header = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                body.len()
            );
            stream.write_all(header.as_bytes()).expect("write headers");
            stream.write_all(&body).expect("write body");
            stream.flush().expect("flush response");
        });

        (url, handle)
    }

    // ===== Mandatory test 1: bare invocation shows top-level help and succeeds =====

    #[test]
    fn bare_invocation_shows_top_level_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("COMMANDS:"));
        assert!(output.contains("convert"));
        assert!(output.contains("inspect"));
        assert!(output.contains("serve"));
        assert!(output.contains("sign"));
    }

    // ===== Mandatory test 2: --help shows top-level help =====

    #[test]
    fn dash_dash_help_shows_top_level_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "--help".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("COMMANDS:"));
        assert!(output.contains("EXIT CODES:"));
    }

    // ===== Mandatory test 3: `truss help` shows top-level help =====

    #[test]
    fn help_command_shows_top_level_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "help".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("COMMANDS:"));
    }

    // ===== Mandatory test 4: `truss help convert` shows convert help =====

    #[test]
    fn help_convert_shows_convert_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "help".to_string(),
                "convert".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss convert"));
        assert!(output.contains("--output"));
        assert!(output.contains("--width"));
        assert!(!output.contains("--bind")); // Should NOT contain serve options
    }

    #[test]
    fn help_optimize_shows_optimize_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "help".to_string(),
                "optimize".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss optimize"));
        assert!(output.contains("--mode"));
        assert!(output.contains("--target-quality"));
    }

    // ===== Mandatory test 5: `truss convert --help` shows convert help =====

    #[test]
    fn convert_dash_help_shows_convert_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "--help".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss convert"));
        assert!(output.contains("--output"));
    }

    // ===== Mandatory test 6: `truss serve --help` shows serve help =====

    #[test]
    fn serve_dash_help_shows_serve_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "serve".to_string(),
                "--help".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss serve"));
        assert!(output.contains("--bind"));
        assert!(output.contains("--storage-root"));
        assert!(output.contains("ENVIRONMENT VARIABLES:"));
        assert!(!output.contains("--width")); // Should NOT contain convert options
    }

    // ===== Mandatory test 7: `truss sign --help` shows sign help =====

    #[test]
    fn sign_dash_help_shows_sign_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "sign".to_string(),
                "--help".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss sign"));
        assert!(output.contains("--base-url"));
        assert!(output.contains("--key-id"));
        assert!(output.contains("--expires"));
    }

    // ===== Mandatory test 8: convert missing --output shows usage and hint =====

    #[test]
    fn convert_missing_output_shows_usage_and_hint() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "input.png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 1);
        let output = String::from_utf8(stderr).expect("utf8 stderr");
        assert!(output.contains("error:"), "should contain error: {output}");
        assert!(output.contains("usage:"), "should contain usage: {output}");
        assert!(output.contains("hint:"), "should contain hint: {output}");
    }

    // ===== Mandatory test 9: inspect missing input shows usage and hint =====

    #[test]
    fn inspect_missing_input_shows_usage_and_hint() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "inspect".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 1);
        let output = String::from_utf8(stderr).expect("utf8 stderr");
        assert!(output.contains("error:"), "should contain error: {output}");
        assert!(output.contains("usage:"), "should contain usage: {output}");
        assert!(output.contains("hint:"), "should contain hint: {output}");
    }

    // ===== Mandatory test 10: sign missing args shows usage and hint =====

    #[test]
    fn sign_missing_args_shows_usage_and_hint() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "sign".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 1);
        let output = String::from_utf8(stderr).expect("utf8 stderr");
        assert!(output.contains("error:"), "should contain error: {output}");
        assert!(output.contains("usage:"), "should contain usage: {output}");
        assert!(output.contains("hint:"), "should contain hint: {output}");
    }

    // ===== Mandatory test 11: -- allows -foo.png as input =====

    #[test]
    fn double_dash_allows_leading_dash_input() {
        let result = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "-o".to_string(),
            "out.jpg".to_string(),
            "--".to_string(),
            "-foo.png".to_string(),
        ]);

        assert_eq!(
            result.unwrap(),
            Command::Convert(ConvertCommand {
                input: InputSource::Path(PathBuf::from("-foo.png")),
                output: OutputTarget::Path(PathBuf::from("out.jpg")),
                options: TransformOptions::default(),
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    // ===== Mandatory test 12: implicit convert with leading-dash output =====
    // Note: With clap, `-o -- -out.jpg` is not supported the same way.
    // Instead, use `-o=-out.jpg` or `--output=-out.jpg`.

    // ===== Mandatory test 13: top-level serve flags still work =====

    #[test]
    fn top_level_serve_flags_parse_correctly() {
        let command = parse_args(vec![
            "truss".to_string(),
            "--storage-root".to_string(),
            "fixtures".to_string(),
            "--public-base-url".to_string(),
            "https://assets.example.com".to_string(),
            "--allow-insecure-url-sources".to_string(),
        ])
        .expect("parse implicit serve");

        assert_eq!(
            command,
            Command::Serve(ServeCommand {
                bind_addr: None,
                storage_root: Some(PathBuf::from("fixtures")),
                public_base_url: Some("https://assets.example.com".to_string()),
                signed_url_key_id: None,
                signed_url_secret: None,
                allow_insecure_url_sources: true,
            })
        );
    }

    // ===== Mandatory test 14: implicit convert still works =====

    #[test]
    fn implicit_convert_still_works() {
        let command = parse_args(vec![
            "truss".to_string(),
            "input.png".to_string(),
            "-o".to_string(),
            "output.jpg".to_string(),
            "--width".to_string(),
            "100".to_string(),
            "--fit".to_string(),
            "contain".to_string(),
        ])
        .expect("parse implicit convert");

        assert_eq!(
            command,
            Command::Convert(ConvertCommand {
                input: InputSource::Path(PathBuf::from("input.png")),
                output: OutputTarget::Path(PathBuf::from("output.jpg")),
                options: TransformOptions {
                    width: Some(100),
                    fit: Some(Fit::Contain),
                    ..TransformOptions::default()
                },
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    // ===== Mandatory test 15: exit codes are consistent =====

    #[test]
    fn exit_code_help_is_zero() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec!["truss".to_string(), "--help".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 0);
    }

    #[test]
    fn exit_code_usage_error_is_one() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "input.png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 1);
    }

    #[test]
    fn exit_code_io_error() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                "missing-file.png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 2);
    }

    #[test]
    fn exit_code_input_error() {
        let mut stdin = Cursor::new(vec![1, 2, 3, 4]);
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec!["truss".to_string(), "inspect".to_string(), "-".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 3);
    }

    /// The class the CLI names alongside each exit code, and the anchor
    /// `docs/problems.md` gives it. The exit codes are the CLI's column of that page's
    /// table, so this is what keeps the page honest: a class that changes exit code, or an
    /// exit code that changes class, fails here.
    #[test]
    fn map_transform_error_matches_the_class_table() {
        const PROBLEM_DOCS: &str = include_str!("../../../docs/problems.md");
        // A Windows checkout has CRLF line endings, so the anchors are matched against
        // the text with the carriage returns taken out.
        let problem_docs = PROBLEM_DOCS.replace('\r', "");
        let cases: [(crate::TransformError, &str, u8); 8] = [
            (
                crate::TransformError::InvalidOptions("x".into()),
                "invalid-options",
                EXIT_USAGE,
            ),
            (
                crate::TransformError::InvalidInput("x".into()),
                "invalid-input",
                EXIT_INPUT,
            ),
            (
                crate::TransformError::UnsupportedInputMediaType("x".into()),
                "unsupported-input-media-type",
                EXIT_INPUT,
            ),
            (
                crate::TransformError::DecodeFailed("x".into()),
                "decode-failed",
                EXIT_TRANSFORM,
            ),
            (
                crate::TransformError::EncodeFailed("x".into()),
                "encode-failed",
                EXIT_TRANSFORM,
            ),
            (
                crate::TransformError::CapabilityMissing("x".into()),
                "capability-missing",
                EXIT_TRANSFORM,
            ),
            (
                crate::TransformError::LimitExceeded("x".into()),
                "limit-exceeded",
                EXIT_TRANSFORM,
            ),
            (
                crate::TransformError::UnsupportedOutputMediaType(MediaType::Gif),
                "unsupported-output-media-type",
                EXIT_TRANSFORM,
            ),
        ];

        for (error, slug, exit_code) in cases {
            let mapped = super::map_transform_error(error.clone());
            assert_eq!(mapped.class.slug(), slug, "{error:?}");
            assert_eq!(mapped.exit_code, exit_code, "{error:?}");
            assert!(
                problem_docs.contains(&format!("### {slug}\n")),
                "docs/problems.md should document the {slug} class"
            );
        }
    }

    /// stderr carries the class in parentheses after the message, which is how a caller
    /// reads the same classification the server puts in `type` and the browser in `kind`.
    #[test]
    fn stderr_names_the_class_after_the_message() {
        let mut stderr = Vec::new();
        let code = super::write_error(
            &mut stderr,
            super::map_transform_error(crate::TransformError::LimitExceeded(
                "output image would have 400000000 pixels, limit is 67108864".to_string(),
            )),
        );

        assert_eq!(code, EXIT_TRANSFORM);
        assert_eq!(
            String::from_utf8(stderr).expect("utf-8 stderr"),
            "error: output image would have 400000000 pixels, limit is 67108864 (limit-exceeded)\n"
        );
    }

    /// A usage fault names the request, not the transform, and keeps its usage and hint
    /// lines below the classified first line.
    #[test]
    fn stderr_names_the_class_on_a_usage_error() {
        let mut stderr = Vec::new();
        let code = super::write_error(&mut stderr, super::sign_error("'sign' requires --key-id"));

        assert_eq!(code, EXIT_USAGE);
        let rendered = String::from_utf8(stderr).expect("utf-8 stderr");
        assert!(
            rendered.starts_with("error: 'sign' requires --key-id (invalid-request)\n"),
            "{rendered}"
        );
        assert!(rendered.ends_with("hint: run 'truss sign --help' for sign options\n"));
    }

    /// A source that is not there is `not-found`, the class the server gives the same miss,
    /// while the exit code stays 2.
    #[test]
    fn missing_input_reports_the_not_found_class() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                "missing-file.png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(code, 2);
        let rendered = String::from_utf8(stderr).expect("utf-8 stderr");
        assert!(rendered.contains("(not-found)"), "{rendered}");
    }

    /// A decode failure is exit 4 wherever it is raised. The sniff that runs before the
    /// transform used to report it as an input error (3), so one truncated file was a
    /// transform error to `convert` and an input error to `inspect`.
    #[test]
    fn a_decode_failure_from_the_sniff_is_a_transform_error() {
        // A PNG signature with nothing after it: recognised as PNG, too short to decode.
        let truncated = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
        for command in ["inspect", "convert"] {
            let mut stdin = Cursor::new(truncated.clone());
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();

            let mut args = vec!["truss".to_string(), command.to_string(), "-".to_string()];
            if command == "convert" {
                args.push("--output".to_string());
                args.push("-".to_string());
            }
            let code = run_with_io(args, &mut stdin, &mut stdout, &mut stderr);

            let rendered = String::from_utf8(stderr).expect("utf-8 stderr");
            assert_eq!(code, EXIT_TRANSFORM, "{command}: {rendered}");
            assert!(
                rendered.contains("(decode-failed)"),
                "{command}: {rendered}"
            );
        }
    }

    // ===== Additional test: unknown subcommand =====

    #[test]
    fn unknown_subcommand_exits_with_usage_error() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "converrt".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 1);
        let output = String::from_utf8(stderr).expect("utf8 stderr");
        assert!(output.contains("error:"), "should contain error: {output}");
    }

    // ===== Additional test: inspect --help =====

    #[test]
    fn inspect_dash_help_shows_inspect_help() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                "--help".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("truss inspect"));
        assert!(output.contains("--url"));
    }

    // ===== Additional test: help inspect =====

    #[test]
    fn help_inspect_shows_inspect_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "inspect".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Inspect));
    }

    // ===== Additional test: help serve =====

    /// Every environment variable in the serve help sits in one column.
    ///
    /// The rows are written in blocks behind feature gates, and a block opened with a
    /// backslash line continuation loses the indentation of its first line along with
    /// the newline, so the first name in each gated block printed flush left in exactly
    /// the builds the release ships. Asserting the shape of the whole section rather
    /// than four names catches a block added later.
    #[test]
    fn help_serve_keeps_every_environment_variable_in_one_column() {
        let help = super::help_serve();
        let misaligned: Vec<&str> = help
            .lines()
            .skip_while(|line| !line.starts_with("ENVIRONMENT VARIABLES:"))
            .filter(|line| {
                let trimmed = line.trim_start();
                trimmed.starts_with("TRUSS_")
                    || trimmed.starts_with("AWS_")
                    || trimmed.starts_with("AZURE_")
                    || trimmed.starts_with("GOOGLE_")
            })
            .filter(|line| !line.starts_with("  ") || line.starts_with("   "))
            .collect();

        assert!(
            misaligned.is_empty(),
            "every environment variable row is indented two spaces, found: {misaligned:#?}"
        );
    }

    #[test]
    fn help_serve_shows_serve_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "serve".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Serve));
    }

    // ===== Additional test: help validate =====

    #[test]
    fn help_validate_shows_validate_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "validate".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Validate));
    }

    #[test]
    fn parse_args_validate() {
        let result =
            parse_args(vec!["truss".to_string(), "validate".to_string()]).expect("parse validate");
        assert_eq!(result, Command::Validate);
    }

    #[test]
    fn validate_help_flag() {
        let result = parse_args(vec![
            "truss".to_string(),
            "validate".to_string(),
            "--help".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Validate));
    }

    #[test]
    #[serial]
    fn validate_invalid_config() {
        // SAFETY: test-only, single-threaded access to this env var.
        unsafe { env::set_var("TRUSS_MAX_CONCURRENT_TRANSFORMS", "invalid") };
        let mut stdout = Vec::new();
        let result = super::serve::execute_validate(&mut stdout);
        unsafe { env::remove_var("TRUSS_MAX_CONCURRENT_TRANSFORMS") };
        assert!(result.is_err());
    }

    #[test]
    #[serial]
    fn validate_valid_config() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let mut stdout = Vec::new();
        // SAFETY: test-only, single-threaded access to this env var.
        unsafe { env::set_var("TRUSS_STORAGE_ROOT", dir.path().to_str().unwrap()) };
        let result = super::serve::execute_validate(&mut stdout);
        unsafe { env::remove_var("TRUSS_STORAGE_ROOT") };
        assert!(result.is_ok());
        let output = String::from_utf8(stdout).expect("valid utf-8");
        assert!(output.contains("configuration is valid"));
        assert!(output.contains("storage root:"));
    }

    // ===== Additional test: help sign =====

    #[test]
    fn help_sign_shows_sign_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "sign".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Sign));
    }

    // ===== Additional test: -h works as --help =====

    #[test]
    fn dash_h_shows_top_level_help() {
        let result = parse_args(vec!["truss".to_string(), "-h".to_string()]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::TopLevel));
    }

    // ===== Existing test updates =====

    #[test]
    fn parse_args_supports_serve_bind() {
        let command = parse_args(vec![
            "truss".to_string(),
            "serve".to_string(),
            "--bind".to_string(),
            "127.0.0.1:9000".to_string(),
        ])
        .expect("parse serve bind");

        assert_eq!(
            command,
            Command::Serve(ServeCommand {
                bind_addr: Some("127.0.0.1:9000".to_string()),
                storage_root: None,
                public_base_url: None,
                signed_url_key_id: None,
                signed_url_secret: None,
                allow_insecure_url_sources: false,
            })
        );
    }

    #[test]
    fn parse_args_supports_serve_runtime_options() {
        let command = parse_args(vec![
            "truss".to_string(),
            "serve".to_string(),
            "--storage-root".to_string(),
            "fixtures".to_string(),
            "--public-base-url".to_string(),
            "https://assets.example.com".to_string(),
            "--signed-url-key-id".to_string(),
            "public-dev".to_string(),
            "--signed-url-secret".to_string(),
            "secret-value".to_string(),
            "--allow-insecure-url-sources".to_string(),
        ])
        .expect("parse serve runtime options");

        assert_eq!(
            command,
            Command::Serve(ServeCommand {
                bind_addr: None,
                storage_root: Some(PathBuf::from("fixtures")),
                public_base_url: Some("https://assets.example.com".to_string()),
                signed_url_key_id: Some("public-dev".to_string()),
                signed_url_secret: Some("secret-value".to_string()),
                allow_insecure_url_sources: true,
            })
        );
    }

    #[test]
    fn parse_args_rejects_partial_signed_url_credentials() {
        let error = parse_args(vec![
            "truss".to_string(),
            "serve".to_string(),
            "--signed-url-key-id".to_string(),
            "public-dev".to_string(),
        ])
        .expect("parse serve args first");

        let error = match error {
            Command::Serve(command) => {
                resolve_server_config(command).expect_err("partial credentials should fail")
            }
            _ => panic!("expected serve command"),
        };

        assert_eq!(error.exit_code, 1);
        assert!(
            error
                .message
                .contains("--signed-url-key-id and --signed-url-secret must be provided together")
        );
    }

    #[test]
    fn parse_args_rejects_invalid_public_base_url() {
        let error = parse_args(vec![
            "truss".to_string(),
            "serve".to_string(),
            "--public-base-url".to_string(),
            "ftp://assets.example.com".to_string(),
        ])
        .expect_err("invalid public base URL should fail");

        assert_eq!(error.exit_code, 1);
        assert!(
            error
                .message
                .contains("requires an http:// or https:// URL"),
            "message: {}",
            error.message,
        );
    }

    #[test]
    #[serial]
    fn resolve_server_config_applies_serve_overrides() {
        let storage_root = temp_dir("serve-config");
        let expected_storage_root = storage_root.canonicalize().expect("canonicalize temp dir");
        let config = resolve_server_config(ServeCommand {
            bind_addr: Some("127.0.0.1:0".to_string()),
            storage_root: Some(storage_root.clone()),
            public_base_url: Some("https://assets.example.com".to_string()),
            signed_url_key_id: Some("public-dev".to_string()),
            signed_url_secret: Some("secret-value".to_string()),
            allow_insecure_url_sources: true,
        })
        .expect("resolve server config");

        let _ = fs::remove_dir_all(storage_root);

        assert_eq!(config.storage_root, expected_storage_root);
        assert_eq!(
            config.public_base_url.as_deref(),
            Some("https://assets.example.com")
        );
        assert_eq!(config.signed_url_key_id.as_deref(), Some("public-dev"));
        assert_eq!(config.signed_url_secret.as_deref(), Some("secret-value"));
        assert!(config.allow_insecure_url_sources);
    }

    #[test]
    fn parse_args_supports_inspect_path() {
        let command = parse_args(vec![
            "truss".to_string(),
            "inspect".to_string(),
            "input.png".to_string(),
        ])
        .expect("parse inspect path");

        assert_eq!(
            command,
            Command::Inspect(super::InspectCommand {
                input: InputSource::Path(PathBuf::from("input.png"))
            })
        );
    }

    #[test]
    fn parse_args_supports_inspect_url() {
        let command = parse_args(vec![
            "truss".to_string(),
            "inspect".to_string(),
            "--url".to_string(),
            "http://example.com/image.png".to_string(),
        ])
        .expect("parse inspect url");

        assert_eq!(
            command,
            Command::Inspect(super::InspectCommand {
                input: InputSource::Url("http://example.com/image.png".to_string())
            })
        );
    }

    #[test]
    fn parse_args_supports_convert_path_and_output() {
        let command = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "input.png".to_string(),
            "-o".to_string(),
            "output.jpg".to_string(),
            "--width".to_string(),
            "100".to_string(),
            "--fit".to_string(),
            "contain".to_string(),
        ])
        .expect("parse convert");

        assert_eq!(
            command,
            Command::Convert(ConvertCommand {
                input: InputSource::Path(PathBuf::from("input.png")),
                output: OutputTarget::Path(PathBuf::from("output.jpg")),
                options: TransformOptions {
                    width: Some(100),
                    fit: Some(Fit::Contain),
                    ..TransformOptions::default()
                },
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    #[test]
    fn parse_args_supports_optimize_subcommand() {
        let command = parse_args(vec![
            "truss".to_string(),
            "optimize".to_string(),
            "input.png".to_string(),
            "-o".to_string(),
            "output.png".to_string(),
            "--mode".to_string(),
            "lossless".to_string(),
        ])
        .expect("parse optimize");

        assert_eq!(
            command,
            Command::Optimize(ConvertCommand {
                input: InputSource::Path(PathBuf::from("input.png")),
                output: OutputTarget::Path(PathBuf::from("output.png")),
                options: TransformOptions {
                    optimize: OptimizeMode::Lossless,
                    ..TransformOptions::default()
                },
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    /// `truss optimize --mode none` re-encodes without optimizing, so it made files larger
    /// on the one subcommand that promises the opposite, and it was the only mode that
    /// reached an output format the others refuse. It is `truss convert` under a name that
    /// says the reverse, so it is not a mode of this command.
    #[test]
    fn optimize_refuses_a_mode_that_does_not_optimize() {
        let message =
            parse_optimizing_mode("none").expect_err("`none` is not an optimization mode");
        assert!(
            message.contains("truss convert"),
            "the refusal should name the command that does a plain re-encode: {message}"
        );

        for mode in ["auto", "lossless", "lossy"] {
            assert!(
                parse_optimizing_mode(mode).is_ok(),
                "{mode} is an optimization mode"
            );
        }

        // The value keeps its meaning everywhere else, including on `truss convert`.
        assert_eq!(parse_optimize_mode("none"), Ok(OptimizeMode::None));
    }

    #[test]
    fn parse_args_optimize_defaults_to_auto_mode() {
        let command = parse_args(vec![
            "truss".to_string(),
            "optimize".to_string(),
            "input.png".to_string(),
            "-o".to_string(),
            "output.webp".to_string(),
        ])
        .expect("parse optimize");

        assert_eq!(
            command,
            Command::Optimize(ConvertCommand {
                input: InputSource::Path(PathBuf::from("input.png")),
                output: OutputTarget::Path(PathBuf::from("output.webp")),
                options: TransformOptions {
                    optimize: OptimizeMode::Auto,
                    ..TransformOptions::default()
                },
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    #[test]
    fn parse_args_rejects_non_optimizable_optimize_format() {
        let error = parse_args(vec![
            "truss".to_string(),
            "optimize".to_string(),
            "input.png".to_string(),
            "-o".to_string(),
            "output.svg".to_string(),
            "--format".to_string(),
            "svg".to_string(),
        ])
        .expect_err("svg optimize output should be rejected");

        assert!(
            error
                .message
                .contains("optimization is not supported for svg output")
        );
    }

    #[test]
    fn parse_args_supports_convert_url_and_output() {
        let command = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "--url".to_string(),
            "http://example.com/image.png".to_string(),
            "-o".to_string(),
            "output.jpg".to_string(),
        ])
        .expect("parse convert url");

        assert_eq!(
            command,
            Command::Convert(ConvertCommand {
                input: InputSource::Url("http://example.com/image.png".to_string()),
                output: OutputTarget::Path(PathBuf::from("output.jpg")),
                options: TransformOptions::default(),
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    #[test]
    fn parse_args_supports_sign_for_path_sources() {
        let command = parse_args(vec![
            "truss".to_string(),
            "sign".to_string(),
            "--base-url".to_string(),
            "https://cdn.example.com".to_string(),
            "--path".to_string(),
            "/image.png".to_string(),
            "--key-id".to_string(),
            "public-dev".to_string(),
            "--secret".to_string(),
            "secret-value".to_string(),
            "--expires".to_string(),
            "4102444800".to_string(),
            "--format".to_string(),
            "jpeg".to_string(),
        ])
        .expect("parse sign path");

        assert_eq!(
            command,
            Command::Sign(SignCommand {
                base_url: "https://cdn.example.com".to_string(),
                source: SignedUrlSource::Path {
                    path: "/image.png".to_string(),
                    version: None
                },
                key_id: "public-dev".to_string(),
                secret: "secret-value".to_string(),
                expires: 4_102_444_800,
                options: TransformOptions {
                    format: Some(MediaType::Jpeg),
                    ..TransformOptions::default()
                },
                watermark_url: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
                preset: None,
            })
        );
    }

    #[test]
    fn parse_args_supports_sign_for_url_sources() {
        let command = parse_args(vec![
            "truss".to_string(),
            "sign".to_string(),
            "--base-url".to_string(),
            "https://cdn.example.com".to_string(),
            "--url".to_string(),
            "https://origin.example.com/image.png".to_string(),
            "--version".to_string(),
            "v2".to_string(),
            "--key-id".to_string(),
            "public-dev".to_string(),
            "--secret".to_string(),
            "secret-value".to_string(),
            "--expires".to_string(),
            "4102444800".to_string(),
            "--width".to_string(),
            "120".to_string(),
        ])
        .expect("parse sign url");

        assert_eq!(
            command,
            Command::Sign(SignCommand {
                base_url: "https://cdn.example.com".to_string(),
                source: SignedUrlSource::Url {
                    url: "https://origin.example.com/image.png".to_string(),
                    version: Some("v2".to_string())
                },
                key_id: "public-dev".to_string(),
                secret: "secret-value".to_string(),
                expires: 4_102_444_800,
                options: TransformOptions {
                    width: Some(120),
                    ..TransformOptions::default()
                },
                watermark_url: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
                preset: None,
            })
        );
    }

    #[test]
    fn parse_args_supports_sign_with_preset() {
        let command = parse_args(vec![
            "truss".to_string(),
            "sign".to_string(),
            "--base-url".to_string(),
            "https://cdn.example.com".to_string(),
            "--path".to_string(),
            "/hero.jpg".to_string(),
            "--key-id".to_string(),
            "mykey".to_string(),
            "--secret".to_string(),
            "s3cret".to_string(),
            "--expires".to_string(),
            "1700000000".to_string(),
            "--preset".to_string(),
            "thumbnail".to_string(),
        ])
        .expect("parse sign with preset");

        match command {
            Command::Sign(s) => assert_eq!(s.preset.as_deref(), Some("thumbnail")),
            _ => panic!("expected Sign command"),
        }
    }

    #[test]
    fn parse_args_rejects_missing_convert_output() {
        let error = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "input.png".to_string(),
        ])
        .expect_err("missing output should fail");

        assert_eq!(error.exit_code, 1);
        assert!(error.message.contains("requires -o"));
    }

    #[test]
    fn parse_args_supports_inspect_https_url() {
        let command = parse_args(vec![
            "truss".to_string(),
            "inspect".to_string(),
            "--url".to_string(),
            "https://example.com/image.png".to_string(),
        ])
        .expect("inspect https url should parse");

        assert_eq!(
            command,
            Command::Inspect(super::InspectCommand {
                input: InputSource::Url("https://example.com/image.png".to_string())
            })
        );
    }

    #[test]
    fn parse_args_rejects_invalid_convert_url_scheme() {
        let error = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "--url".to_string(),
            "ftp://example.com/image.png".to_string(),
            "-o".to_string(),
            "out.png".to_string(),
        ])
        .expect_err("convert invalid scheme should fail");

        assert_eq!(error.exit_code, 1);
        assert!(
            error
                .message
                .contains("requires an http:// or https:// URL")
        );
    }

    #[test]
    fn run_with_io_converts_without_explicit_subcommand() {
        let input_path = temp_file_path("implicit-convert-input");
        let output_path = temp_file_path("implicit-convert-output").with_extension("jpg");
        fs::write(&input_path, png_bytes()).expect("write input file");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                input_path.display().to_string(),
                "-o".to_string(),
                output_path.display().to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let output_bytes = fs::read(&output_path).expect("read output file");
        let artifact = sniff_artifact(RawArtifact::new(output_bytes, None)).expect("sniff output");

        let _ = fs::remove_file(&input_path);
        let _ = fs::remove_file(&output_path);

        assert_eq!(exit_code, 0);
        assert!(stdout.is_empty());
        assert!(stderr.is_empty());
        assert_eq!(artifact.media_type, MediaType::Jpeg);
    }

    #[test]
    fn run_with_io_inspects_a_file() {
        let path = temp_file_path("inspect");
        fs::write(&path, png_bytes()).expect("write temp file");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                path.display().to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let _ = fs::remove_file(&path);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());

        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains("\"format\": \"png\""));
        assert!(output.contains("\"mime\": \"image/png\""));
        assert!(output.contains("\"width\": 4"));
        assert!(output.contains("\"height\": 3"));
        assert!(output.contains("\"hasAlpha\": true"));
        assert!(output.contains("\"isAnimated\": false"));
    }

    #[test]
    fn run_with_io_inspects_a_url() {
        let (url, handle) = spawn_http_server(png_bytes(), "image/png");
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                "--url".to_string(),
                url,
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        handle.join().expect("join server thread");

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        assert!(
            String::from_utf8(stdout)
                .expect("utf8 stdout")
                .contains("\"format\": \"png\"")
        );
    }

    #[test]
    fn run_with_io_converts_a_file_and_infers_output_format_from_extension() {
        let input_path = temp_file_path("convert-input");
        let output_path = temp_file_path("convert-output").with_extension("jpg");
        fs::write(&input_path, png_bytes()).expect("write input file");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                input_path.display().to_string(),
                "-o".to_string(),
                output_path.display().to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let output_bytes = fs::read(&output_path).expect("read output file");
        let artifact = sniff_artifact(RawArtifact::new(output_bytes, None)).expect("sniff output");

        let _ = fs::remove_file(&input_path);
        let _ = fs::remove_file(&output_path);

        assert_eq!(exit_code, 0);
        assert!(stdout.is_empty());
        assert!(stderr.is_empty());
        assert_eq!(artifact.media_type, MediaType::Jpeg);
    }

    #[test]
    fn run_with_io_optimizes_a_png_file() {
        let input_path = temp_file_path("optimize-input");
        let output_path = temp_file_path("optimize-output").with_extension("png");
        fs::write(&input_path, png_bytes()).expect("write input file");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "optimize".to_string(),
                input_path.display().to_string(),
                "-o".to_string(),
                output_path.display().to_string(),
                "--mode".to_string(),
                "lossless".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let output_bytes = fs::read(&output_path).expect("read output file");
        let artifact = sniff_artifact(RawArtifact::new(output_bytes, None)).expect("sniff output");

        let _ = fs::remove_file(&input_path);
        let _ = fs::remove_file(&output_path);

        assert_eq!(exit_code, 0);
        assert!(stdout.is_empty());
        assert!(stderr.is_empty());
        assert_eq!(artifact.media_type, MediaType::Png);
    }

    #[test]
    fn run_with_io_converts_stdin_to_stdout() {
        let mut stdin = Cursor::new(png_bytes());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "-".to_string(),
                "-o".to_string(),
                "-".to_string(),
                "--format".to_string(),
                "png".to_string(),
                "--width".to_string(),
                "8".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(
            exit_code,
            0,
            "stderr was: {}",
            String::from_utf8_lossy(&stderr)
        );
        assert!(stderr.is_empty());

        let artifact = sniff_artifact(RawArtifact::new(stdout, None)).expect("sniff stdout output");

        assert_eq!(artifact.media_type, MediaType::Png);
        assert_eq!(artifact.metadata.width, Some(8));
    }

    #[test]
    fn run_with_io_converts_a_url_to_a_file() {
        let (url, handle) = spawn_http_server(png_bytes(), "image/png");
        let output_path = temp_file_path("convert-url-output").with_extension("png");
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "--url".to_string(),
                url,
                "-o".to_string(),
                output_path.display().to_string(),
                "--width".to_string(),
                "8".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        handle.join().expect("join server thread");

        let output_bytes = fs::read(&output_path).expect("read output file");
        let artifact = sniff_artifact(RawArtifact::new(output_bytes, None)).expect("sniff output");
        let _ = fs::remove_file(&output_path);

        assert_eq!(exit_code, 0);
        assert!(stdout.is_empty());
        assert!(stderr.is_empty());
        assert_eq!(artifact.media_type, MediaType::Png);
        assert_eq!(artifact.metadata.width, Some(8));
    }

    #[test]
    fn run_with_io_reports_input_errors() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "inspect".to_string(),
                "missing-file.png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 2);
        assert!(stdout.is_empty());
        assert!(
            String::from_utf8(stderr)
                .expect("utf8 stderr")
                .contains("failed to read missing-file.png")
        );
    }

    #[test]
    fn run_with_io_reports_decode_errors() {
        let mut stdin = Cursor::new(vec![1, 2, 3, 4]);
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "inspect".to_string(), "-".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 3);
        assert!(stdout.is_empty());
        assert!(
            String::from_utf8(stderr)
                .expect("utf8 stderr")
                .contains("unknown file signature")
        );
    }

    // ===== Additional tests for -- with convert =====

    #[test]
    fn double_dash_input_with_options_before() {
        // truss convert -o out.jpg --width 100 -- --leading-dash.png
        let result = parse_args(vec![
            "truss".to_string(),
            "convert".to_string(),
            "-o".to_string(),
            "out.jpg".to_string(),
            "--width".to_string(),
            "100".to_string(),
            "--".to_string(),
            "--leading-dash.png".to_string(),
        ]);

        assert_eq!(
            result.unwrap(),
            Command::Convert(ConvertCommand {
                input: InputSource::Path(PathBuf::from("--leading-dash.png")),
                output: OutputTarget::Path(PathBuf::from("out.jpg")),
                options: TransformOptions {
                    width: Some(100),
                    ..TransformOptions::default()
                },
                watermark_path: None,
                watermark_position: None,
                watermark_opacity: None,
                watermark_margin: None,
            })
        );
    }

    // ===== Additional test: inspect -- allows leading dash path =====

    #[test]
    fn inspect_double_dash_allows_leading_dash() {
        let result = parse_args(vec![
            "truss".to_string(),
            "inspect".to_string(),
            "--".to_string(),
            "-weird-name.png".to_string(),
        ]);

        assert_eq!(
            result.unwrap(),
            Command::Inspect(super::InspectCommand {
                input: InputSource::Path(PathBuf::from("-weird-name.png"))
            })
        );
    }

    // ===== Completions subcommand =====

    #[test]
    fn completions_bash_produces_output() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "completions".to_string(),
                "bash".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        // Bash completions should contain the program name
        assert!(
            output.contains("truss"),
            "bash completions should mention truss"
        );
    }

    #[test]
    fn completions_zsh_produces_output() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "completions".to_string(),
                "zsh".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(!stdout.is_empty());
    }

    #[test]
    fn completions_fish_produces_output() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "completions".to_string(),
                "fish".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        assert!(!stdout.is_empty());
    }

    // ===== Version subcommand =====

    #[test]
    fn dash_dash_version_prints_version() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "--version".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(
            output.starts_with("truss "),
            "version output should start with 'truss ': {output}"
        );
        assert!(
            output.contains(env!("CARGO_PKG_VERSION")),
            "should contain package version: {output}"
        );
    }

    #[test]
    fn dash_v_prints_version() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "-V".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(output.contains(env!("CARGO_PKG_VERSION")));
    }

    #[test]
    fn help_includes_version_and_sponsor() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec!["truss".to_string(), "--help".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(
            output.contains(env!("CARGO_PKG_VERSION")),
            "help should include version: {output}"
        );
        assert!(
            output.contains("Sponsor:"),
            "help should include sponsor link: {output}"
        );
        assert!(
            output.contains("github.com/sponsors/nao1215"),
            "help should include GitHub Sponsors URL: {output}"
        );
    }

    // ===== Fix: extensionless file treated as implicit convert =====

    #[test]
    fn preprocess_args_extensionless_file_is_implicit_convert() {
        // Create a temp file without extension
        let dir = temp_dir("extensionless");
        let file_path = dir.join("image");
        fs::write(&file_path, png_bytes()).expect("write extensionless fixture");

        // Use bare filename and set cwd to the temp dir so preprocess_args
        // sees a relative name without path separators.
        let original_dir = std::env::current_dir().expect("get cwd");
        std::env::set_current_dir(&dir).expect("set cwd to temp dir");

        let args = vec![
            OsString::from("truss"),
            OsString::from("image"),
            OsString::from("-o"),
            OsString::from("out.jpg"),
        ];
        let result = preprocess_args(args);

        std::env::set_current_dir(&original_dir).expect("restore cwd");

        assert_eq!(
            result[1], "convert",
            "extensionless file should trigger implicit convert"
        );
        assert_eq!(result[2], "image", "bare file name should follow convert");

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn preprocess_args_nonexistent_extensionless_is_unknown_subcommand() {
        // A name that doesn't exist on disk should pass through (clap handles typo suggestion)
        let args = vec![
            OsString::from("truss"),
            OsString::from("nonexistent_subcommand_xyz"),
        ];
        let result = preprocess_args(args.clone());
        assert_eq!(
            result, args,
            "non-existent extensionless name should pass through unchanged"
        );
    }

    // ===== Exit code: InvalidOptions maps to EXIT_USAGE (1) =====

    #[test]
    fn exit_code_invalid_options_is_usage_error() {
        // quality=0 triggers InvalidOptions via normalize()
        let png_bytes = {
            let mut img = image::RgbaImage::new(1, 1);
            img.put_pixel(0, 0, image::Rgba([255, 0, 0, 255]));
            let mut buf = Vec::new();
            let encoder = image::codecs::png::PngEncoder::new(&mut buf);
            image::ImageEncoder::write_image(
                encoder,
                img.as_raw(),
                1,
                1,
                image::ColorType::Rgba8.into(),
            )
            .unwrap();
            buf
        };
        let mut stdin = Cursor::new(png_bytes);
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "-".to_string(),
                "-o".to_string(),
                "-".to_string(),
                "--format".to_string(),
                "jpeg".to_string(),
                "--quality".to_string(),
                "0".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 1, "InvalidOptions should exit with code 1 (usage)");
    }

    // ===== Help: completions topic =====

    #[test]
    fn help_completions_shows_completions_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "completions".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Completions));
    }

    #[test]
    fn help_version_shows_version_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "version".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Version));
    }

    #[test]
    fn completions_dash_help_shows_completions_help() {
        let result = parse_args(vec![
            "truss".to_string(),
            "completions".to_string(),
            "--help".to_string(),
        ]);
        assert_eq!(result.unwrap(), Command::Help(HelpTopic::Completions));
    }

    #[test]
    fn completions_without_shell_exits_with_usage_error() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec!["truss".to_string(), "completions".to_string()],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 1, "completions without shell arg should exit 1");
    }

    // ===== Completions: implicit args are present =====

    #[test]
    fn completions_bash_includes_implicit_args() {
        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let code = run_with_io(
            vec![
                "truss".to_string(),
                "completions".to_string(),
                "bash".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );
        assert_eq!(code, 0);
        let output = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(
            output.contains("--output"),
            "bash completions should include --output for implicit convert"
        );
        assert!(
            output.contains("--bind"),
            "bash completions should include --bind for implicit serve"
        );
    }

    // ===== Help text: exit code 5 is documented =====

    #[test]
    fn help_exit_codes_includes_runtime() {
        let text = super::help_top_level();
        assert!(
            text.contains("5  Runtime error"),
            "help text should document exit code 5"
        );
    }

    // ===== Unknown help topic hint lists all topics =====

    #[test]
    fn unknown_help_topic_hint_lists_all_topics() {
        let result = parse_args(vec![
            "truss".to_string(),
            "help".to_string(),
            "nonexistent".to_string(),
        ]);
        let err = result.unwrap_err();
        let hint = err.hint.unwrap();
        assert!(hint.contains("completions"), "hint should list completions");
        assert!(hint.contains("version"), "hint should list version");
    }
    // ===== Standard output is flushed before the process exits =====

    /// A writer that accepts every write and fails only when it is flushed, which is how
    /// a full disk or a closed pipe behaves against a buffered `StdoutLock`.
    struct FlushFails;

    impl Write for FlushFails {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            Ok(buf.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Err(io::Error::new(io::ErrorKind::StorageFull, "no space left"))
        }
    }

    #[test]
    fn flush_stdout_turns_a_failed_flush_into_a_runtime_error() {
        let mut stderr = Vec::new();
        let code = flush_stdout(0, &mut FlushFails, &mut stderr);

        assert_eq!(code, 5, "a lost payload must not exit 0");
        let text = String::from_utf8(stderr).expect("utf8 stderr");
        assert!(
            text.contains("error: failed to write stdout"),
            "stderr should name the failure, got: {text}"
        );
    }

    #[test]
    fn flush_stdout_keeps_the_original_exit_code_when_the_command_already_failed() {
        let mut stderr = Vec::new();
        let code = flush_stdout(2, &mut FlushFails, &mut stderr);

        assert_eq!(code, 2, "the first failure is the one worth reporting");
        assert!(stderr.is_empty(), "the flush error should not add noise");
    }

    #[test]
    fn flush_stdout_passes_the_exit_code_through_when_the_flush_succeeds() {
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        assert_eq!(flush_stdout(0, &mut stdout, &mut stderr), 0);
        assert_eq!(flush_stdout(3, &mut stdout, &mut stderr), 3);
        assert!(stderr.is_empty());
    }

    // ===== help and version explain a failed write like every other command =====

    /// A writer that fails on the first write, standing in for a redirect into a full disk.
    struct WriteFails;

    impl Write for WriteFails {
        fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::StorageFull, "no space left"))
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn help_and_version_report_a_failed_write_on_stderr() {
        for args in [vec!["truss", "--version"], vec!["truss", "help", "convert"]] {
            let mut stdin = Cursor::new(Vec::new());
            let mut stderr = Vec::new();
            let code = run_with_io(
                args.iter().map(|value| (*value).to_string()),
                &mut stdin,
                &mut WriteFails,
                &mut stderr,
            );

            assert_eq!(
                code, 5,
                "{args:?} should exit 5 when stdout cannot be written"
            );
            let text = String::from_utf8(stderr).expect("utf8 stderr");
            assert!(
                text.contains("error: failed to write stdout"),
                "{args:?} should explain itself, got: {text}"
            );
        }
    }

    // ===== An output format truss never encodes is a usage error either way =====

    #[test]
    fn a_gif_output_extension_is_rejected_like_the_gif_flag() {
        for args in [
            vec!["truss", "convert", "in.png", "-o", "out.gif"],
            vec!["truss", "convert", "in.png", "-o", "out.GIF"],
            vec!["truss", "optimize", "in.png", "-o", "out.gif"],
            vec![
                "truss", "convert", "in.png", "-o", "out.png", "--format", "gif",
            ],
        ] {
            let error = parse_args(args.iter().map(|value| (*value).to_string()))
                .expect_err("gif output should not parse");

            assert_eq!(error.exit_code, 1, "{args:?} should be a usage error");
            assert!(
                error.message.contains("input-only format")
                    && error.message.contains("png, jpeg, webp, or avif"),
                "{args:?} should name the alternatives, got: {}",
                error.message
            );
        }
    }

    #[test]
    fn an_explicit_format_overrides_an_unencodable_output_extension() {
        let command = parse_args(
            [
                "truss", "convert", "in.png", "-o", "out.gif", "--format", "png",
            ]
            .iter()
            .map(|value| (*value).to_string()),
        )
        .expect("an explicit format decides the encoder, whatever the extension says");

        match command {
            Command::Convert(convert) => assert_eq!(convert.options.format, Some(MediaType::Png)),
            other => panic!("expected a convert command, got {other:?}"),
        }
    }

    // ===== A path argument that is not valid UTF-8 =====

    /// A file name on Linux is a byte string, so `truss convert` has to take one whatever
    /// bytes it holds. Reading the arguments as `String` panicked before the command line
    /// was parsed, which no adapter could report and no exit code covered.
    ///
    /// Only Linux runs this: APFS refuses to create a name that is not valid UTF-8, with
    /// `EILSEQ`, so the fixture cannot exist on macOS, and a Windows name is UTF-16. The
    /// panic itself was not filesystem-specific, and the flag-value case below covers the
    /// platforms this one skips.
    #[cfg(target_os = "linux")]
    #[test]
    fn a_non_utf8_input_path_is_converted_rather_than_refused() {
        use std::os::unix::ffi::OsStringExt;

        let dir = temp_dir("non-utf8-input");
        let mut name = OsString::from_vec(b"caf\xe9".to_vec());
        name.push(".png");
        let input_path = dir.join(name);
        fs::write(&input_path, png_bytes()).expect("write input file");
        let output_path = dir.join("out.png");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                OsString::from("truss"),
                OsString::from("convert"),
                input_path.clone().into_os_string(),
                OsString::from("-o"),
                output_path.clone().into_os_string(),
                OsString::from("--width"),
                OsString::from("2"),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let output_exists = output_path.is_file();
        let _ = fs::remove_dir_all(&dir);

        assert_eq!(
            exit_code,
            0,
            "stderr was: {}",
            String::from_utf8_lossy(&stderr)
        );
        assert!(
            output_exists,
            "the conversion should have written an output"
        );
    }

    /// The bytes have to survive on the way out as well: a lossy conversion of the path
    /// would write to a name nobody asked for and report success. Linux only, for the
    /// reason the test above gives.
    #[cfg(target_os = "linux")]
    #[test]
    fn a_non_utf8_output_path_is_written_under_exactly_those_bytes() {
        use std::os::unix::ffi::OsStringExt;

        let dir = temp_dir("non-utf8-output");
        let input_path = dir.join("in.png");
        fs::write(&input_path, png_bytes()).expect("write input file");
        let mut name = OsString::from_vec(b"sortie-\xe9".to_vec());
        name.push(".png");
        let output_path = dir.join(name);

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                OsString::from("truss"),
                OsString::from("convert"),
                input_path.into_os_string(),
                OsString::from("-o"),
                output_path.clone().into_os_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let output_exists = output_path.is_file();
        let _ = fs::remove_dir_all(&dir);

        assert_eq!(
            exit_code,
            0,
            "stderr was: {}",
            String::from_utf8_lossy(&stderr)
        );
        assert!(
            output_exists,
            "the output should exist under the bytes that were asked for"
        );
    }

    /// A flag whose value is genuinely text keeps refusing bytes that are not text, with
    /// the usage error every other bad flag value gets rather than a panic.
    #[cfg(unix)]
    #[test]
    fn a_non_utf8_value_for_a_text_flag_is_a_usage_error() {
        use std::os::unix::ffi::OsStringExt;

        let mut stdin = Cursor::new(png_bytes());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                OsString::from("truss"),
                OsString::from("convert"),
                OsString::from("-"),
                OsString::from("-o"),
                OsString::from("-"),
                OsString::from("--format"),
                OsString::from_vec(vec![0xff]),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        assert_eq!(exit_code, EXIT_USAGE, "a value that is not text is usage");
        assert!(stdout.is_empty(), "nothing should be written on a refusal");
    }

    // ===== The class of a file system fault =====

    /// The watermark is a source the command line named, so a missing one is the same
    /// class as a missing input. `internal-error` says the fault is truss's own.
    #[test]
    fn a_missing_watermark_is_not_found_like_a_missing_input() {
        let dir = temp_dir("watermark-class");
        let input_path = dir.join("in.png");
        fs::write(&input_path, png_bytes()).expect("write input file");
        let output_path = dir.join("out.png");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                input_path.display().to_string(),
                "-o".to_string(),
                output_path.display().to_string(),
                "--watermark".to_string(),
                dir.join("absent.png").display().to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let message = String::from_utf8(stderr).expect("utf8 stderr");
        let _ = fs::remove_dir_all(&dir);

        assert_eq!(exit_code, 2, "a file system fault is exit 2");
        assert!(
            message.contains("(not-found)"),
            "a missing watermark should be not-found, got: {message}"
        );
    }

    /// A watermark that is there and cannot be read is the other class, which is what
    /// keeps the shared rule honest rather than replacing one blanket answer with another.
    #[cfg(unix)]
    #[test]
    fn an_unreadable_watermark_is_an_internal_error() {
        use std::os::unix::fs::PermissionsExt;

        let dir = temp_dir("watermark-unreadable");
        let input_path = dir.join("in.png");
        fs::write(&input_path, png_bytes()).expect("write input file");
        let watermark_path = dir.join("wm.png");
        fs::write(&watermark_path, png_bytes()).expect("write watermark file");
        fs::set_permissions(&watermark_path, fs::Permissions::from_mode(0o000))
            .expect("make the watermark unreadable");
        let output_path = dir.join("out.png");

        let mut stdin = Cursor::new(Vec::<u8>::new());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                input_path.display().to_string(),
                "-o".to_string(),
                output_path.display().to_string(),
                "--watermark".to_string(),
                watermark_path.display().to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let message = String::from_utf8(stderr).expect("utf8 stderr");
        let _ = fs::set_permissions(&watermark_path, fs::Permissions::from_mode(0o644));
        let _ = fs::remove_dir_all(&dir);

        if message.contains("(not-found)") {
            // Running as root reads it anyway, and the case has nothing to assert.
            return;
        }
        assert_eq!(exit_code, 2, "a file system fault is exit 2");
        assert!(
            message.contains("(internal-error)"),
            "a watermark that cannot be read is not a missing one, got: {message}"
        );
    }

    // ===== stderr is one line per failure =====

    /// The failure a real decoder raises, end to end.
    ///
    /// `truncated.jpg` is a JPEG whose bytes stop early, which is what an upload cut off
    /// by a dropped connection looks like, and the decoder's own wording for it ends with
    /// a newline.
    #[test]
    fn a_truncated_jpeg_is_reported_on_one_line() {
        const TRUNCATED_JPEG: &[u8] = include_bytes!("../../../integration/fixtures/truncated.jpg");

        let mut stdin = Cursor::new(TRUNCATED_JPEG.to_vec());
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();

        let exit_code = run_with_io(
            vec![
                "truss".to_string(),
                "convert".to_string(),
                "-".to_string(),
                "-o".to_string(),
                "-".to_string(),
                "--format".to_string(),
                "png".to_string(),
            ],
            &mut stdin,
            &mut stdout,
            &mut stderr,
        );

        let rendered = String::from_utf8(stderr).expect("utf8 stderr");

        assert_eq!(exit_code, EXIT_TRANSFORM);
        assert_eq!(
            rendered.lines().count(),
            1,
            "one failure is one line, got: {rendered:?}"
        );
        assert!(
            rendered.trim_end().ends_with("(decode-failed)"),
            "the class ends the line, got: {rendered:?}"
        );
    }

    /// The class is the last thing on the line, so a message carrying a newline puts it on
    /// a line of its own where a caller reading the last line cannot find the failure.
    #[test]
    fn write_error_keeps_a_message_with_a_newline_on_one_line() {
        let mut stderr = Vec::new();
        let code = super::write_error(
            &mut stderr,
            super::classified_error(
                crate::core::error_class::ErrorClass::DecodeFailed,
                EXIT_TRANSFORM,
                "decoding failed\n",
            ),
        );

        let rendered = String::from_utf8(stderr).expect("utf8 stderr");

        assert_eq!(code, EXIT_TRANSFORM);
        assert_eq!(
            rendered.lines().count(),
            1,
            "one failure is one line, got: {rendered:?}"
        );
        assert!(
            rendered.ends_with("(decode-failed)\n"),
            "the class ends the line, got: {rendered:?}"
        );
    }

    #[test]
    fn svg_output_from_a_raster_input_names_the_rule_it_broke() {
        let error = crate::TransformError::UnsupportedOutputMediaType(MediaType::Svg).to_string();

        assert!(
            error.contains("requires an svg input"),
            "the message should say why svg was refused, got: {error}"
        );
    }
}