utf8-forge 0.2.3

A focused, idempotent, bidirectional charset transcoder for source trees. Default output: UTF-8 without BOM.
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
// Copyright 2026 houseme
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! `utf8-forge` — a focused, idempotent, bidirectional charset transcoder for
//! source trees (default output: UTF-8 without BOM).
//!
//! This crate primarily ships the [`utf8-forge` CLI](cli::Cli); the library
//! surface is public so integration tests and downstream pipelines can drive
//! the same detection and transcoding machinery the binary uses. Detection is
//! rule-based (BOM prefixes, then strict UTF-8, then strict GB18030) and
//! never guesses; transcoding is strict on both ends, so mojibake is never
//! written.
//!
//! ```rust
//! use utf8_forge::detector::detect;
//! use utf8_forge::transcoder::{TargetEncoding, transcode};
//!
//! let bytes = [0xEF, 0xBB, 0xBF, b'h', b'i'];
//! assert_eq!(detect(&bytes).tag(), "utf-8-bom");
//!
//! let converted = transcode(&bytes, TargetEncoding::Utf8).unwrap();
//! match converted {
//!     utf8_forge::transcoder::TranscodeOutcome::Changed(out) => assert_eq!(out, b"hi"),
//!     utf8_forge::transcoder::TranscodeOutcome::Unchanged => panic!("BOM must be stripped"),
//! }
//! ```
//!
//! # CLI orchestration
//! Per-file pipelines, report assembly, and the [`run()`](fn@run) entry point
//! with exit-code mapping:
//! - `0` success
//! - `1` non-fatal per-file issues (`unknown`, `decode_error`, `encode_error`,
//!   `io_error`, `fetch_error`)
//! - `2` fatal (bad args, missing root, I/O setup failure) — also emitted by
//!   clap on parse errors
//! - `3` `verify`/`fix` gate failure (non-UTF-8 whitelisted files; fix's
//!   verify gate wins over convert-side issues)

// Public modules (lib target so integration tests can exercise the
// pipeline directly; the binary in `main.rs` stays a thin wrapper).
pub mod cli;
pub mod config;
pub mod detector;
pub mod differ;
pub mod error;
#[cfg(feature = "remote")]
pub mod fetcher;
pub mod filter;
pub mod ignorefile;
pub mod reporter;
pub mod transcoder;
pub mod walker;
pub mod writer;

use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Instant;

use anyhow::{Context, bail};
use rayon::prelude::*;

use crate::cli::{Cli, Commands, ConvertArgs, DiffArgs, FixArgs, TreeArgs};
use crate::detector::{DetectedEncoding, detect};
use crate::differ::DiffInput;
use crate::error::ForgeError;
use crate::filter::{FilterConfig, FilterOptions};
use crate::reporter::{
    ConsoleCfg, ConvertSummary, FileRecord, FixSummary, OptionsBlock, Report, ScanSummary, Summary,
    VerifySummary, print_header, print_problem, print_verbose, write_report,
};
use crate::transcoder::{TargetEncoding, TranscodeOutcome, transcode_detected};
use crate::walker::collect_files;

/// Dispatch a parsed CLI and map the outcome to a process exit code.
/// Fatal setup errors print to stderr and yield exit code 2.
pub fn run(cli: Cli) -> i32 {
    let outcome = match cli.command {
        Commands::Scan(args) => cmd_scan(&args),
        Commands::Convert(args) => cmd_convert(&args),
        Commands::Verify(args) => cmd_verify(&args),
        Commands::Fix(args) => cmd_fix(&args),
        Commands::Diff(args) => cmd_diff(&args),
    };
    outcome.unwrap_or_else(|err| {
        eprintln!("[utf8-forge] fatal: {err:#}");
        2
    })
}

// ---------------------------------------------------------------------------
// Per-file processors
// ---------------------------------------------------------------------------

/// Read at most one byte beyond the cap so callers can distinguish an exact
/// fit from oversized content even when the file grows after the metadata
/// check. The extra byte is only used for the size verdict, never transcoded.
fn read_file_limited(path: &Path, max_file_size: u64) -> std::io::Result<Vec<u8>> {
    let mut bytes = Vec::new();
    fs::File::open(path)?
        .take(max_file_size.saturating_add(1))
        .read_to_end(&mut bytes)?;
    Ok(bytes)
}

/// Deferred `FileRecord` builder that stamps per-file duration at completion.
struct RecBuilder<'a> {
    path: &'a Path,
    started: Instant,
}

impl<'a> RecBuilder<'a> {
    fn new(path: &'a Path) -> RecBuilder<'a> {
        RecBuilder {
            path,
            started: Instant::now(),
        }
    }

    fn done(
        self,
        detected: Option<&'static str>,
        action: &'static str,
        bytes_before: Option<u64>,
        bytes_after: Option<u64>,
        error: Option<String>,
    ) -> FileRecord {
        FileRecord {
            path: self.path.to_string_lossy().into_owned(),
            detected,
            action,
            bytes_before,
            bytes_after,
            duration_us: self.started.elapsed().as_micros() as u64,
            error,
        }
    }
}

/// Detect only. Oversized files are never read.
pub fn scan_file(path: &Path, max_file_size: u64) -> FileRecord {
    let b = RecBuilder::new(path);
    let size = fs::metadata(path).map(|m| m.len()).ok();
    if let Some(sz) = size
        && sz > max_file_size
    {
        return b.done(None, "skipped_too_large", Some(sz), None, None);
    }
    let bytes = match read_file_limited(path, max_file_size) {
        Ok(bytes) => bytes,
        Err(e) => return b.done(None, "error", size, None, Some(format!("io_error: {e}"))),
    };
    let sz = bytes.len() as u64;
    if sz > max_file_size {
        return b.done(None, "skipped_too_large", Some(sz), None, None);
    }
    let det = detect(&bytes);
    b.done(Some(det.tag()), "detected", Some(sz), None, None)
}

/// Shared context for tree-mode conversion.
#[derive(Debug, Clone)]
pub struct ConvertCtx {
    pub root: std::path::PathBuf,
    pub target: TargetEncoding,
    pub dry_run: bool,
    pub backup_dir: Option<std::path::PathBuf>,
    pub max_file_size: u64,
}

/// Detect + transcode + (unless dry-run) backup + atomic write.
///
/// Byte-identical outcomes are recorded as `unchanged` and never touch the
/// filesystem, so unchanged files keep their mtime. Every failure is
/// contained in this one record (per-file error isolation).
pub fn convert_file(path: &Path, ctx: &ConvertCtx) -> FileRecord {
    let b = RecBuilder::new(path);
    let size = fs::metadata(path).map(|m| m.len()).ok();
    if let Some(sz) = size
        && sz > ctx.max_file_size
    {
        return b.done(None, "skipped_too_large", Some(sz), None, None);
    }
    let bytes = match read_file_limited(path, ctx.max_file_size) {
        Ok(bytes) => bytes,
        Err(e) => return b.done(None, "error", size, None, Some(format!("io_error: {e}"))),
    };
    let sz = bytes.len() as u64;
    if sz > ctx.max_file_size {
        return b.done(None, "skipped_too_large", Some(sz), None, None);
    }
    let det = detect(&bytes);
    let tag = det.tag();

    match transcode_detected(&bytes, det, ctx.target) {
        Err(ForgeError::UnknownEncoding) => {
            b.done(Some(tag), "skipped_unknown", Some(sz), None, None)
        }
        Err(e) => b.done(
            Some(tag),
            "error",
            Some(sz),
            None,
            Some(format!("{}: {e}", e.category())),
        ),
        Ok(TranscodeOutcome::Unchanged) => b.done(Some(tag), "unchanged", Some(sz), Some(sz), None),
        Ok(TranscodeOutcome::Changed(new_bytes)) => {
            if ctx.dry_run {
                return b.done(
                    Some(tag),
                    "would_convert",
                    Some(sz),
                    Some(new_bytes.len() as u64),
                    None,
                );
            }
            // Refuse to clobber write-protected files: POSIX rename would
            // silently bypass the permission bits, but the operator locked
            // that file on purpose.
            let write_protected = fs::metadata(path)
                .map(|m| m.permissions().readonly())
                .unwrap_or(false);
            if write_protected {
                return b.done(
                    Some(tag),
                    "error",
                    Some(sz),
                    None,
                    Some(
                        "io_error: file is write-protected (read-only); chmod +w to convert".into(),
                    ),
                );
            }
            // Backup the original bytes before the rename, so the backup
            // always holds the pre-conversion content.
            if let Some(backup_dir) = &ctx.backup_dir
                && let Err(e) = writer::backup_original(&ctx.root, path, backup_dir, &bytes)
            {
                return b.done(
                    Some(tag),
                    "error",
                    Some(sz),
                    None,
                    Some(format!("{}: backup failed: {e}", e.category())),
                );
            }
            match writer::atomic_write(path, &new_bytes) {
                Ok(()) => b.done(
                    Some(tag),
                    "converted",
                    Some(sz),
                    Some(new_bytes.len() as u64),
                    None,
                ),
                Err(e) => b.done(
                    Some(tag),
                    "error",
                    Some(sz),
                    None,
                    Some(format!("{}: write failed: {e}", e.category())),
                ),
            }
        }
    }
}

/// Verify a file is UTF-8 without BOM. Unreadable / oversized files fail
/// closed (a gate must not wave through what it cannot inspect). The size is
/// re-checked after the read, mirroring the other processors, so a file that
/// grows between stat and read cannot slip past the cap.
pub fn verify_file(path: &Path, max_file_size: u64) -> FileRecord {
    let b = RecBuilder::new(path);
    let size = fs::metadata(path).map(|m| m.len()).ok();
    if let Some(sz) = size
        && sz > max_file_size
    {
        return b.done(
            None,
            "failed",
            Some(sz),
            None,
            Some("too_large: exceeds --max-file-size".to_string()),
        );
    }
    let bytes = match read_file_limited(path, max_file_size) {
        Ok(bytes) => bytes,
        Err(e) => return b.done(None, "failed", size, None, Some(format!("io_error: {e}"))),
    };
    let sz = bytes.len() as u64;
    if sz > max_file_size {
        return b.done(
            None,
            "failed",
            Some(sz),
            None,
            Some("too_large: exceeds --max-file-size".to_string()),
        );
    }
    let det = detect(&bytes);
    let tag = det.tag();
    if det == DetectedEncoding::Utf8 {
        b.done(Some(tag), "passed", Some(sz), None, None)
    } else {
        b.done(Some(tag), "failed", Some(sz), None, None)
    }
}

// ---------------------------------------------------------------------------
// Report assembly
// ---------------------------------------------------------------------------

/// Run-level facts that flow into the report document unchanged.
struct RunFacts {
    root: Option<std::path::PathBuf>,
    url: Option<String>,
    options: OptionsBlock,
    started_at: String,
    verify_failed: Option<Vec<String>>,
    warnings: Vec<String>,
}

/// Sort records by path (reports stay reproducible run to run), compute the
/// mode-specific summary, and assemble the final report document.
fn finalize_report(
    mode: &'static str,
    facts: RunFacts,
    mut records: Vec<FileRecord>,
    elapsed: std::time::Duration,
) -> Report {
    records.sort_by(|a, b| a.path.cmp(&b.path));
    let total = records.len() as u64;

    let summary = match mode {
        "scan" => {
            let mut s = ScanSummary {
                total_files: total,
                ..ScanSummary::default()
            };
            for r in &records {
                if r.action == "skipped_too_large" {
                    s.too_large += 1;
                }
                if r.action == "error" {
                    s.errors += 1;
                }
                match r.detected {
                    Some("utf-8") => s.utf8 += 1,
                    Some("utf-8-bom") => s.utf8_bom += 1,
                    Some("utf-16le") => s.utf16le += 1,
                    Some("utf-16be") => s.utf16be += 1,
                    Some("utf-32le") => s.utf32le += 1,
                    Some("utf-32be") => s.utf32be += 1,
                    Some("gb18030") => s.gb18030 += 1,
                    Some("unknown") => s.unknown += 1,
                    _ => {}
                }
            }
            s.candidates = total - s.too_large;
            Summary::Scan(s)
        }
        "verify" => {
            let passed = records.iter().filter(|r| r.action == "passed").count() as u64;
            let failed = records.iter().filter(|r| r.action == "failed").count() as u64;
            Summary::Verify(VerifySummary {
                total_files: total,
                candidates: total,
                passed,
                failed,
            })
        }
        "fix" => {
            let mut s = FixSummary {
                total_files: total,
                ..FixSummary::default()
            };
            for r in &records {
                if r.action == "skipped_too_large" {
                    s.too_large += 1;
                }
                match r.action {
                    "unchanged" => s.already_utf8 += 1,
                    "converted" | "would_convert" => s.converted += 1,
                    "skipped_unknown" => s.unknown += 1,
                    "error" => {
                        let msg = r.error.as_deref().unwrap_or("");
                        if msg.starts_with("decode_error") {
                            s.decode_errors += 1;
                        } else if msg.starts_with("encode_error") {
                            s.encode_errors += 1;
                        } else {
                            s.io_errors += 1;
                        }
                    }
                    _ => {}
                }
            }
            s.candidates = total - s.too_large;
            Summary::Fix(s)
        }
        _ => {
            let mut s = ConvertSummary {
                total_files: total,
                ..ConvertSummary::default()
            };
            for r in &records {
                match r.action {
                    "unchanged" => s.already_utf8 += 1,
                    "converted" | "would_convert" => s.converted += 1,
                    "skipped_unknown" => s.unknown += 1,
                    "skipped_too_large" => s.too_large += 1,
                    "error" => {
                        let msg = r.error.as_deref().unwrap_or("");
                        if msg.starts_with("decode_error") {
                            s.decode_errors += 1;
                        } else if msg.starts_with("encode_error") {
                            s.encode_errors += 1;
                        } else if msg.starts_with("fetch_error") {
                            s.fetch_error += 1;
                        } else {
                            s.io_errors += 1;
                        }
                    }
                    _ => {}
                }
            }
            s.candidates = total - s.too_large;
            Summary::Convert(s)
        }
    };

    let unknown_files: Vec<String> = records
        .iter()
        .filter(|r| r.action == "skipped_unknown" || r.detected == Some("unknown"))
        .map(|r| r.path.clone())
        .collect();
    let error_files: Vec<String> = records
        .iter()
        .filter(|r| r.action == "error" || r.action == "failed")
        .map(|r| r.path.clone())
        .collect();
    // Fix mode's verify gate; empty (and omitted) in every other mode.

    Report {
        tool: "utf8-forge",
        version: env!("CARGO_PKG_VERSION"),
        mode,
        root: facts.root.map(|p| p.to_string_lossy().into_owned()),
        url: facts.url,
        started_at: facts.started_at,
        duration_ms: elapsed.as_millis() as u64,
        options: facts.options,
        summary,
        files: records,
        unknown_files,
        error_files,
        verify_failed_files: facts.verify_failed.unwrap_or_default(),
        warnings: facts.warnings,
    }
}

/// Snapshot of effective options for the report's `options` block.
/// Conversion-only fields are taken from `conv` (absent in scan/verify).
fn options_block(
    args: &TreeArgs,
    conv: Option<&dyn ConvertOptions>,
    filter: &FilterConfig,
    config_file: Option<&str>,
    ignore_file: Option<&str>,
) -> OptionsBlock {
    let mut extensions: Vec<String> = filter.extensions.iter().cloned().collect();
    extensions.sort();
    let mut exclude_dirs: Vec<String> = filter.exclude_dirs.iter().cloned().collect();
    exclude_dirs.sort();
    let mut exclude_ext: Vec<String> = args.exclude_ext.clone();
    exclude_ext.sort();
    OptionsBlock {
        target: conv.map(|c| c.effective_target()).unwrap_or("utf-8"),
        dry_run: conv.is_some_and(|c| c.is_dry_run()),
        extensions,
        exclude_dirs,
        exclude_ext,
        exclude_globs: args.exclude_glob.clone(),
        backup_dir: conv.and_then(|c| c.backup_dir().map(|p| p.to_string_lossy().into_owned())),
        config_file: config_file.map(str::to_string),
        ignore_file: ignore_file.map(str::to_string),
    }
}

// ---------------------------------------------------------------------------
// Console output
// ---------------------------------------------------------------------------

/// Print problem lines (stderr, always) and verbose detail lines (stdout, -v).
///
/// `target_tag` is the destination encoding shown in converted /
/// would_convert lines (convert/fetch modes); `None` for scan/verify whose
/// verbose lines carry no target.
fn print_records(records: &[FileRecord], console: ConsoleCfg, target_tag: Option<&'static str>) {
    for r in records {
        match r.action {
            "error" => print_problem(format!(
                "error {} ({})",
                r.path,
                r.error.as_deref().unwrap_or("unspecified")
            )),
            "skipped_unknown" => print_problem(format!("unknown {}", r.path)),
            // Scan records unknown files as `detected` with the `unknown` tag;
            // they are still per-file issues (scan exits 1) and get the same
            // stderr line as `skipped_unknown` in the other modes.
            "detected" if r.detected == Some("unknown") => {
                print_problem(format!("unknown {}", r.path))
            }
            "failed" => {
                let reason = r
                    .error
                    .clone()
                    .unwrap_or_else(|| format!("detected={}", r.detected.unwrap_or("?")));
                print_problem(format!("failed {} ({reason})", r.path));
            }
            _ => {}
        }
        match r.action {
            // Unknown files already got their stderr problem line; the
            // verbose classification line would repeat it verbatim under -v.
            "detected" if r.detected != Some("unknown") => {
                print_verbose(console, format!("{} {}", r.detected.unwrap_or("?"), r.path))
            }
            "unchanged" => print_verbose(
                console,
                format!("unchanged {} ({})", r.path, r.detected.unwrap_or("?")),
            ),
            "converted" | "would_convert" => print_verbose(
                console,
                format!(
                    "{} {} ({} → {}, {} → {} bytes)",
                    r.action,
                    r.path,
                    r.detected.unwrap_or("?"),
                    target_tag.unwrap_or("?"),
                    r.bytes_before.unwrap_or(0),
                    r.bytes_after.unwrap_or(0)
                ),
            ),
            "skipped_too_large" => print_verbose(
                console,
                format!(
                    "skipped_too_large {} ({} bytes)",
                    r.path,
                    r.bytes_before.unwrap_or(0)
                ),
            ),
            "passed" => print_verbose(console, format!("passed {}", r.path)),
            _ => {}
        }
    }
}

/// Final duration (+ report path) line.
fn print_duration(report: Option<&Path>, elapsed: std::time::Duration, console: ConsoleCfg) {
    if console.quiet {
        return;
    }
    match report {
        Some(p) => println!(
            "[utf8-forge] duration={:.1}s  report={}",
            elapsed.as_secs_f64(),
            p.display()
        ),
        None => println!("[utf8-forge] duration={:.1}s", elapsed.as_secs_f64()),
    }
}

/// `convert` / `fetch` console summary.
fn print_convert_summary(
    s: &ConvertSummary,
    report: Option<&Path>,
    elapsed: std::time::Duration,
    console: ConsoleCfg,
) {
    if !console.quiet {
        println!(
            "[utf8-forge] scanned={}  candidates={}",
            s.total_files, s.candidates
        );
        let errors = s.decode_errors + s.encode_errors + s.io_errors;
        println!(
            "[utf8-forge] already_utf8={}  converted={}  unknown={}  errors={}",
            s.already_utf8, s.converted, s.unknown, errors
        );
        if s.too_large > 0 {
            println!("[utf8-forge] too_large={}", s.too_large);
        }
        if s.fetch_error > 0 {
            println!("[utf8-forge] fetch_error={}", s.fetch_error);
        }
    }
    print_duration(report, elapsed, console);
}

/// `scan` console summary (encoding distribution).
fn print_scan_summary(
    s: &ScanSummary,
    report: Option<&Path>,
    elapsed: std::time::Duration,
    console: ConsoleCfg,
) {
    if !console.quiet {
        println!(
            "[utf8-forge] scanned={}  candidates={}",
            s.total_files, s.candidates
        );
        println!(
            "[utf8-forge] utf8={}  utf8_bom={}  utf16le={}  utf16be={}  utf32le={}  utf32be={}  gb18030={}  unknown={}",
            s.utf8, s.utf8_bom, s.utf16le, s.utf16be, s.utf32le, s.utf32be, s.gb18030, s.unknown
        );
        if s.errors > 0 {
            println!("[utf8-forge] errors={}", s.errors);
        }
        if s.too_large > 0 {
            println!("[utf8-forge] too_large={}", s.too_large);
        }
    }
    print_duration(report, elapsed, console);
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// Build the effective filter; conflicting flags / bad globs are fatal.
fn build_filter(args: &TreeArgs) -> anyhow::Result<FilterConfig> {
    let opts = FilterOptions {
        ext_override: args.ext.clone(),
        exclude_ext: args.exclude_ext.clone(),
        exclude_dirs: args.exclude.clone(),
        exclude_globs: args.exclude_glob.clone(),
        include_rc: args.include_rc,
        include_msbuild: args.include_msbuild,
        include_hidden: args.include_hidden,
    };
    FilterConfig::build(&opts).map_err(anyhow::Error::msg)
}

/// Load the TOML config (no-op default when `--config` absent) and merge it
/// into the shared tree args. CLI flags win over config values; lists replace
/// across layers (never concatenate) so the effective whitelist stays
/// auditable. Runs before any pre-flight so downstream `ensure_*` checks see
/// resolved values. Returns the parsed config for conversion-option merging.
fn merge_tree_config(
    common: &mut TreeArgs,
) -> anyhow::Result<(Option<String>, crate::config::ForgeConfig)> {
    let Some(path) = common.config.clone() else {
        return Ok((None, crate::config::ForgeConfig::default()));
    };
    let cfg = crate::config::load(&path)?;
    if common.ext.is_empty()
        && let Some(list) = &cfg.filter.extensions
    {
        // An explicitly empty whitelist would silently scan nothing — the
        // same fatal-input rule as `--ext ""`.
        if list.is_empty() {
            bail!("config [filter].extensions is empty: no usable extensions");
        }
        common.ext = list.clone();
    }
    if common.exclude.is_empty() {
        common.exclude = cfg.filter.exclude_dirs.clone();
    }
    if common.exclude_ext.is_empty() {
        common.exclude_ext = cfg.filter.exclude_ext.clone();
    }
    if common.exclude_glob.is_empty() {
        common.exclude_glob = cfg.filter.exclude_globs.clone();
    }
    common.include_rc |= cfg.filter.include_rc;
    common.include_msbuild |= cfg.filter.include_msbuild;
    common.include_hidden |= cfg.filter.include_hidden;
    if common.max_file_size.is_none() {
        common.max_file_size = cfg.options.max_file_size;
    }
    if common.threads.is_none() {
        common.threads = cfg.options.threads;
    }
    // Validate `[convert].target` even in modes that ignore the section, so
    // an invalid value is always a loud, key-naming error instead of being
    // silently dropped by scan/verify.
    cfg.target()?;
    Ok((Some(path.to_string_lossy().into_owned()), cfg))
}

/// Conversion-option surface shared by `convert` and `fix`, so the
/// `[convert]` config section merges identically into both.
trait ConvertOptions {
    fn common(&self) -> &TreeArgs;
    fn common_mut(&mut self) -> &mut TreeArgs;
    fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>);
    fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>);
    fn target(&self) -> Option<crate::transcoder::TargetEncoding>;
    fn backup_dir(&self) -> Option<&std::path::PathBuf>;
    fn is_dry_run(&self) -> bool;

    /// The target tag after CLI/config merge (built-in default: utf-8).
    fn effective_target(&self) -> &'static str {
        self.target()
            .unwrap_or(crate::transcoder::TargetEncoding::Utf8)
            .tag()
    }
}

impl ConvertOptions for ConvertArgs {
    fn common(&self) -> &TreeArgs {
        &self.common
    }
    fn target(&self) -> Option<crate::transcoder::TargetEncoding> {
        self.target
    }
    fn common_mut(&mut self) -> &mut TreeArgs {
        &mut self.common
    }
    fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>) {
        self.target = t;
    }
    fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>) {
        self.backup_dir = p;
    }
    fn backup_dir(&self) -> Option<&std::path::PathBuf> {
        self.backup_dir.as_ref()
    }
    fn is_dry_run(&self) -> bool {
        self.dry_run
    }
}

impl ConvertOptions for FixArgs {
    fn common(&self) -> &TreeArgs {
        &self.common
    }
    fn target(&self) -> Option<crate::transcoder::TargetEncoding> {
        self.target
    }
    fn common_mut(&mut self) -> &mut TreeArgs {
        &mut self.common
    }
    fn set_target(&mut self, t: Option<crate::transcoder::TargetEncoding>) {
        self.target = t;
    }
    fn set_backup_dir(&mut self, p: Option<std::path::PathBuf>) {
        self.backup_dir = p;
    }
    fn backup_dir(&self) -> Option<&std::path::PathBuf> {
        self.backup_dir.as_ref()
    }
    fn is_dry_run(&self) -> bool {
        self.dry_run
    }
}

/// Apply the tree merge plus the `[convert]` section (convert / fix).
/// CLI flags win over config values; runs before any pre-flight. Both
/// sections use the same parsed snapshot of the file.
fn merge_convert_config(conv: &mut impl ConvertOptions) -> anyhow::Result<Option<String>> {
    let (recorded, cfg) = merge_tree_config(conv.common_mut())?;
    if conv.target().is_none()
        && let Some(t) = cfg.target()?
    {
        conv.set_target(Some(t));
    }
    if conv.backup_dir().is_none()
        && let Some(dir) = &cfg.convert.backup_dir
    {
        // An empty directory name is a fatal input error: it is not a usable
        // target anywhere, and the CLI form rejects it the same way.
        if dir.trim().is_empty() {
            bail!("config [convert].backup_dir is empty: name a real directory");
        }
        conv.set_backup_dir(Some(std::path::PathBuf::from(dir)));
    }
    Ok(recorded)
}

/// Root must be an existing directory, else exit code 2.
fn ensure_root(root: &Path) -> anyhow::Result<()> {
    if !root.is_dir() {
        bail!("root `{}` is not an existing directory", root.display());
    }
    Ok(())
}

/// Prepare the backup directory up front so a broken backup path aborts
/// before any source file is touched. A backup dir **inside** `--root` is
/// fatal: the next run would walk the backups and re-convert them, breaking
/// idempotence (a second run would no longer be a no-op) and silently
/// mutating the safety net. Placement is checked existence-independently
/// (from the deepest existing ancestor) *before* anything is created, so a
/// rejected configuration never leaves directories behind and a dry-run
/// never blesses what the real run would reject.
fn ensure_backup_dir(args: &impl ConvertOptions) -> anyhow::Result<()> {
    let Some(dir) = args.backup_dir() else {
        return Ok(());
    };
    let root_canonical = fs::canonicalize(&args.common().root)?;
    check_backup_placement(dir, &root_canonical)?;
    if args.is_dry_run() {
        return Ok(());
    }
    fs::create_dir_all(dir)
        .with_context(|| format!("cannot create backup dir {}", dir.display()))?;
    ensure_writable_dir(dir)?;
    Ok(())
}

/// Resolve `dir` to its would-be canonical path without requiring it to
/// exist: canonicalize the deepest existing ancestor, then re-attach the
/// non-existing tail. Fatal when the projected path lands inside `root`.
fn check_backup_placement(dir: &Path, root_canonical: &Path) -> anyhow::Result<()> {
    // A relative dir such as `backup` targets a location under the process
    // cwd; resolve it first so the ancestor walk starts from an existing
    // base instead of failing on a not-yet-created relative tail.
    let dir = if dir.is_absolute() {
        dir.to_path_buf()
    } else {
        std::env::current_dir()
            .with_context(|| format!("cannot resolve relative --backup-dir `{}`", dir.display()))?
            .join(dir)
    };
    let mut tail: Vec<std::ffi::OsString> = Vec::new();
    let mut ancestor = dir.to_path_buf();
    let base = loop {
        match fs::canonicalize(&ancestor) {
            Ok(c) => break c,
            Err(_) => {
                let Some(name) = ancestor.file_name() else {
                    bail!("--backup-dir `{}` has no existing ancestor", dir.display());
                };
                let Some(parent) = ancestor.parent() else {
                    bail!("--backup-dir `{}` has no existing ancestor", dir.display());
                };
                tail.push(name.to_os_string());
                ancestor = parent.to_path_buf();
            }
        }
    };
    let projected = tail.iter().rev().fold(base, |p, c| p.join(c));
    if projected.starts_with(root_canonical) {
        bail!(
            "--backup-dir `{}` is inside `--root` `{}`; backups would be re-processed on the next run. Keep the backup directory outside the scanned tree.",
            dir.display(),
            root_canonical.display()
        );
    }
    Ok(())
}

/// Pre-flight the report path so an unwritable report aborts (exit 2) before
/// any file is converted, and collect the canonical report path for
/// traversal exclusion (`--report` inside the tree must not pollute later
/// runs). A bare filename (no directory component) is probed against the
/// current directory. An existing write-protected report file is refused:
/// rename would silently bypass its permission bits (and re-stamp the new
/// report read-only), but the operator locked that file on purpose.
fn ensure_report_dir(report: &Path) -> anyhow::Result<()> {
    if report.is_dir() {
        bail!("--report `{}` is an existing directory", report.display());
    }
    if let Ok(meta) = fs::metadata(report)
        && meta.permissions().readonly()
    {
        bail!(
            "--report `{}` is write-protected (read-only); chmod +w to replace it",
            report.display()
        );
    }
    let parent = match report.parent() {
        Some(p) if !p.as_os_str().is_empty() => p.to_path_buf(),
        _ => std::path::PathBuf::from("."),
    };
    fs::create_dir_all(&parent)
        .with_context(|| format!("cannot create report directory {}", parent.display()))?;
    // create_dir_all is a no-op on an existing directory regardless of
    // permissions — probe writability so an existing read-only report
    // directory also aborts before the tree is touched.
    ensure_writable_dir(&parent)?;
    Ok(())
}

/// Refuse a report target that would overwrite the active ignore rule file —
/// the operator's filters must never be replaced by report JSON.
fn ensure_report_not_ignore(report: Option<&Path>, ignore: Option<&str>) -> anyhow::Result<()> {
    if let (Some(report), Some(ignore)) = (report, ignore)
        && report == std::path::Path::new(ignore)
    {
        bail!(
            "--report `{}` would overwrite the ignore file; pick another report path",
            report.display()
        );
    }
    Ok(())
}

/// Verify `dir` accepts new files by creating and immediately removing a
/// unique probe file. Catches read-only directories that `create_dir_all`
/// silently accepts because they already exist.
fn ensure_writable_dir(dir: &Path) -> anyhow::Result<()> {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let probe = dir.join(format!(
        ".utf8-forge-probe-{}-{}",
        std::process::id(),
        nanos
    ));
    match fs::File::create(&probe) {
        Ok(_) => {
            let _ = fs::remove_file(&probe);
            Ok(())
        }
        Err(e) => bail!("directory `{}` is not writable: {e}", dir.display()),
    }
}

/// Traversal skip list: the report path and the ignore file (so neither is
/// ever scanned as content) — canonicalized downstream by `collect_files`.
fn skip_paths(report: Option<&Path>, ignore: Option<&Path>) -> Vec<PathBuf> {
    let mut v = Vec::new();
    if let Some(p) = report {
        v.push(p.to_path_buf());
    }
    if let Some(p) = ignore {
        v.push(p.to_path_buf());
    }
    v
}

/// Resolve + load ignore rules per the flag precedence: `--no-ignore-file`
/// disables everything; an explicit `--ignore-file` is fatal when missing;
/// auto-discovery of `<root>/.utf8forgeignore` is silent when absent and a
/// warning when unreadable. Returns the rules plus the path to record in the
/// report (None = no ignore layer active).
fn load_ignore(
    args: &TreeArgs,
) -> anyhow::Result<(Option<ignorefile::IgnoreFile>, Option<String>, Vec<String>)> {
    if args.no_ignore_file {
        return Ok((None, None, Vec::new()));
    }
    match &args.ignore_file {
        Some(path) => {
            if !path.is_file() {
                bail!("ignore file `{}` not found", path.display());
            }
            let rules = ignorefile::IgnoreFile::load(path).map_err(anyhow::Error::msg)?;
            let recorded = path.to_string_lossy().into_owned();
            Ok((Some(rules), Some(recorded), Vec::new()))
        }
        None => {
            let path = args.root.join(".utf8forgeignore");
            if !path.is_file() {
                return Ok((None, None, Vec::new()));
            }
            match ignorefile::IgnoreFile::load(&path) {
                Ok(rules) => {
                    let recorded = path.to_string_lossy().into_owned();
                    Ok((Some(rules), Some(recorded), Vec::new()))
                }
                Err(e) => Ok((
                    None,
                    None,
                    vec![format!(
                        "warning: ignoring unreadable .utf8forgeignore: {e}"
                    )],
                )),
            }
        }
    }
}

/// RFC 3339 timestamp captured when per-file processing begins (the
/// traversal is excluded), so `started_at` + `duration_ms` line up.
fn wall_now() -> String {
    crate::reporter::rfc3339_now()
}

/// Upper bound for `--threads`: rayon pool startup cost grows quadratically,
/// and implausible values silently wedge the process for minutes of pure CPU.
/// Anything beyond 1024 hardware threads is not a real request, so it is a
/// fatal input error (symmetric with the loud-strict-input posture).
const MAX_THREADS: usize = 1024;

/// Validate the configured thread count, in every mode that accepts the flag
/// (fetch builds no pool but still refuses implausible values up front).
fn ensure_thread_bound(threads: Option<usize>) -> anyhow::Result<()> {
    match threads {
        Some(n) if n > MAX_THREADS => bail!(
            "--threads {n} exceeds the maximum of {MAX_THREADS}; build a smaller pool instead"
        ),
        _ => Ok(()),
    }
}

/// Run `f` on the configured rayon pool (default pool when `--threads` unset).
fn with_pool<R: Send>(threads: Option<usize>, f: impl FnOnce() -> R + Send) -> anyhow::Result<R> {
    ensure_thread_bound(threads)?;
    match threads {
        Some(n) => {
            let pool = rayon::ThreadPoolBuilder::new()
                .num_threads(n.max(1))
                .build()
                .context("failed to build rayon thread pool")?;
            Ok(pool.install(f))
        }
        None => Ok(f()),
    }
}

fn console_cfg(args: &TreeArgs) -> ConsoleCfg {
    ConsoleCfg {
        quiet: args.quiet,
        verbose: args.verbose,
    }
}

/// Print non-fatal traversal warnings (suppressed by `--quiet`).
fn print_warnings(warnings: &[String], console: ConsoleCfg) {
    if !console.quiet {
        for w in warnings {
            print_problem(w.clone());
        }
    }
}

// ---------------------------------------------------------------------------
// Subcommands
// ---------------------------------------------------------------------------

/// `scan`: detect only, no writes. Exit 1 when any file is `unknown` or
/// errored (I/O), matching the documented per-file issue contract.
fn cmd_scan(args: &TreeArgs) -> anyhow::Result<i32> {
    let mut args = args.clone();
    let (config_file, _) = merge_tree_config(&mut args)?;
    ensure_root(&args.root)?;
    let filter = build_filter(&args)?;
    if let Some(path) = &args.report {
        ensure_report_dir(path)?;
    }
    let console = console_cfg(&args);
    print_header(console, format!("mode=scan root={}", args.root.display()));

    let (ignore, ignore_recorded, ignore_warnings) = load_ignore(&args)?;
    ensure_report_not_ignore(args.report.as_deref(), ignore_recorded.as_deref())?;
    let mut warnings = Vec::new();
    let walk = {
        let skip = skip_paths(
            args.report.as_deref(),
            ignore_recorded
                .as_deref()
                .map(std::path::PathBuf::from)
                .as_deref(),
        );
        let walk = collect_files(&args.root, &filter, &skip, ignore.as_ref());
        warnings.extend(ignore_warnings);
        walk
    };
    warnings.extend(walk.warnings);
    print_warnings(&warnings, console);
    let started_at = wall_now();
    let started = Instant::now();
    let records = with_pool(args.threads, || {
        walk.files
            .par_iter()
            .map(|p| scan_file(p, args.effective_max_file_size()))
            .collect::<Vec<_>>()
    })?;
    print_records(&records, console, None);
    let elapsed = started.elapsed();

    let report = finalize_report(
        "scan",
        RunFacts {
            root: Some(args.root.clone()),
            url: None,
            options: options_block(
                &args,
                None,
                &filter,
                config_file.as_deref(),
                ignore_recorded.as_deref(),
            ),
            started_at,
            verify_failed: None,
            warnings,
        },
        records,
        elapsed,
    );
    let unknown = match &report.summary {
        Summary::Scan(s) => s.unknown,
        _ => 0,
    };
    // Per-file I/O failures also count as issues: the tree was not fully
    // inventoried, so "all classified" would be a lie.
    let file_errors = report.files.iter().filter(|r| r.action == "error").count() as u64;
    if let Some(path) = &args.report {
        write_report(&report, path)?;
    }
    if let Summary::Scan(s) = &report.summary {
        print_scan_summary(s, args.report.as_deref(), elapsed, console);
    }
    Ok(if unknown > 0 || file_errors > 0 { 1 } else { 0 })
}

/// `convert`: tree mode or remote fetch.
fn cmd_convert(args: &ConvertArgs) -> anyhow::Result<i32> {
    #[cfg(feature = "remote")]
    {
        if let Some(url) = &args.url {
            return cmd_convert_url(args, url);
        }
    }
    #[cfg(not(feature = "remote"))]
    {
        if args.url.is_some() {
            bail!("--url requires the `remote` feature; rebuild with default features");
        }
    }
    cmd_convert_tree(args)
}

/// `convert` tree mode: detect + transcode + atomic write. Exit 1 when any
/// per-file issue (`unknown` / decode / io) remains.
fn cmd_convert_tree(args: &ConvertArgs) -> anyhow::Result<i32> {
    let mut args = args.clone();
    let config_file = merge_convert_config(&mut args)?;
    let common = &args.common;
    let target = args.target_or_default();
    ensure_root(&common.root)?;
    let filter = build_filter(common)?;
    // The report pre-flight is side-effect-free, so it runs before the backup
    // directory is created — a rejected report path must not leave a fresh
    // backup dir behind.
    if let Some(path) = &common.report {
        ensure_report_dir(path)?;
    }
    ensure_backup_dir(&args)?;
    let console = console_cfg(common);
    let dry_tag = if args.dry_run { " dry_run=true" } else { "" };
    print_header(
        console,
        format!(
            "mode=convert target={} root={}{}",
            target,
            common.root.display(),
            dry_tag
        ),
    );

    let (ignore, ignore_recorded, ignore_warnings) = load_ignore(common)?;
    ensure_report_not_ignore(common.report.as_deref(), ignore_recorded.as_deref())?;
    // Ignore-layer problems stay advisory: a dropped ignore file only
    // widens the walk, so the quiet contract applies to them.
    print_warnings(&ignore_warnings, console);
    let mut warnings = ignore_warnings;
    let walk = {
        let skip = skip_paths(
            common.report.as_deref(),
            ignore_recorded
                .as_deref()
                .map(std::path::PathBuf::from)
                .as_deref(),
        );
        collect_files(&common.root, &filter, &skip, ignore.as_ref())
    };
    let walk_warning_count = walk.warnings.len();
    warnings.extend(walk.warnings);
    // Ignore-layer problems were already printed right after load_ignore;
    // only the traversal warnings belong to this line.
    print_warnings(&warnings[warnings.len() - walk_warning_count..], console);
    let ctx = ConvertCtx {
        root: common.root.clone(),
        target,
        dry_run: args.dry_run,
        backup_dir: args.backup_dir.clone(),
        max_file_size: common.effective_max_file_size(),
    };
    let started_at = wall_now();
    let started = Instant::now();
    let records = with_pool(common.threads, || {
        walk.files
            .par_iter()
            .map(|p| convert_file(p, &ctx))
            .collect::<Vec<_>>()
    })?;
    print_records(&records, console, Some(target.tag()));
    let elapsed = started.elapsed();

    let report = finalize_report(
        "convert",
        RunFacts {
            root: Some(common.root.clone()),
            url: None,
            options: options_block(
                common,
                Some(&args as &dyn ConvertOptions),
                &filter,
                config_file.as_deref(),
                ignore_recorded.as_deref(),
            ),
            started_at,
            verify_failed: None,
            warnings,
        },
        records,
        elapsed,
    );
    let summary = match &report.summary {
        Summary::Convert(s) => s.clone(),
        _ => ConvertSummary::default(),
    };
    if let Some(path) = &common.report {
        write_report(&report, path)?;
    }
    print_convert_summary(&summary, common.report.as_deref(), elapsed, console);
    Ok(
        if summary.unknown > 0
            || summary.decode_errors > 0
            || summary.encode_errors > 0
            || summary.io_errors > 0
        {
            1
        } else {
            0
        },
    )
}

/// `verify`: assert all whitelisted files are UTF-8 without BOM.
/// Exit 0 on success, 3 on any failure.
fn cmd_verify(args: &TreeArgs) -> anyhow::Result<i32> {
    let mut args = args.clone();
    let (config_file, _) = merge_tree_config(&mut args)?;
    ensure_root(&args.root)?;
    let filter = build_filter(&args)?;
    if let Some(path) = &args.report {
        ensure_report_dir(path)?;
    }
    let console = console_cfg(&args);
    print_header(console, format!("mode=verify root={}", args.root.display()));

    let (ignore, ignore_recorded, ignore_warnings) = load_ignore(&args)?;
    ensure_report_not_ignore(args.report.as_deref(), ignore_recorded.as_deref())?;
    // Ignore-layer problems stay advisory: a dropped ignore file only
    // widens the walk, so the quiet contract applies to them.
    print_warnings(&ignore_warnings, console);
    let mut warnings = Vec::new();
    let walk = {
        let skip = skip_paths(
            args.report.as_deref(),
            ignore_recorded
                .as_deref()
                .map(std::path::PathBuf::from)
                .as_deref(),
        );
        let walk = collect_files(&args.root, &filter, &skip, ignore.as_ref());
        warnings.extend(ignore_warnings);
        walk
    };
    let gate_warned = !walk.warnings.is_empty();
    warnings.extend(walk.warnings);
    // A gate must not wave through what it could not inspect: only traversal
    // warnings (an uninspectable subtree) fail the gate, and they are shown
    // even under --quiet.
    for w in warnings.iter().skip(warnings.len() - gate_warned as usize) {
        print_problem(w.clone());
    }
    let started_at = wall_now();
    let started = Instant::now();
    let records = with_pool(args.threads, || {
        walk.files
            .par_iter()
            .map(|p| verify_file(p, args.effective_max_file_size()))
            .collect::<Vec<_>>()
    })?;
    print_records(&records, console, None);
    let elapsed = started.elapsed();

    let report = finalize_report(
        "verify",
        RunFacts {
            root: Some(args.root.clone()),
            url: None,
            options: options_block(
                &args,
                None,
                &filter,
                config_file.as_deref(),
                ignore_recorded.as_deref(),
            ),
            started_at,
            verify_failed: None,
            warnings,
        },
        records,
        elapsed,
    );
    let summary = match &report.summary {
        Summary::Verify(s) => s.clone(),
        _ => VerifySummary::default(),
    };
    if let Some(path) = &args.report {
        write_report(&report, path)?;
    }
    if !console.quiet {
        println!(
            "[utf8-forge] checked={}  passed={}  failed={}",
            summary.total_files, summary.passed, summary.failed
        );
    }
    print_duration(args.report.as_deref(), elapsed, console);
    Ok(if summary.failed > 0 || gate_warned {
        3
    } else {
        0
    })
}

/// `convert --url`: fetch → detect → transcode → save.
///
/// The transcode result is always materialized unless the destination already
/// holds the identical bytes — in that case the record is `unchanged` and no
/// write happens (same no-churn contract as tree mode; a
/// missing destination is still written, so downloads always materialize).
/// Exit 1 on fetch/decode/unknown/too-large issues; the report is still
/// written for post-mortem.
#[cfg(feature = "remote")]
fn cmd_convert_url(args: &ConvertArgs, url: &str) -> anyhow::Result<i32> {
    let common = &args.common;
    if args.backup_dir.is_some() {
        bail!("--backup-dir is not supported with --url");
    }
    // Fetch reads no config file: the only mergeable key ([convert].target)
    // would silently change nothing here, so combining them is refused.
    if common.config.is_some() {
        bail!("--config is not supported with --url");
    }
    // Fetch builds no rayon pool (a single file), but the same implausible
    // thread counts the tree modes reject are refused here too.
    ensure_thread_bound(common.threads)?;
    // Same report pre-flight as the tree modes: an unwritable report aborts
    // before the download, so no output is produced for a run that cannot be
    // audited.
    if let Some(path) = &common.report {
        ensure_report_dir(path)?;
    }
    let console = console_cfg(common);
    print_header(
        console,
        format!("mode=fetch target={} url={url}", args.target_or_default()),
    );
    let started_at = wall_now();
    let started = Instant::now();
    let out_path = args
        .output
        .clone()
        .unwrap_or_else(|| fetcher::derive_output_path(url));

    let record = match fetcher::fetch_bytes(url, common.effective_max_file_size()) {
        Err(e) => {
            // An oversized payload is the documented `too_large` bucket; any
            // other fetch failure is a generic error row. The console line
            // must match the report action.
            let action = match e {
                ForgeError::TooLarge { .. } => "skipped_too_large",
                _ => "error",
            };
            // The oversized bucket keeps its immediate console line (its
            // report action has no problem print); generic errors are printed
            // exactly once, by `print_records`, against the output path.
            if action == "skipped_too_large" {
                print_problem(format!("skipped_too_large {url} ({}: {e})", e.category()));
            }
            RecBuilder::new(&out_path).done(
                None,
                action,
                None,
                None,
                Some(format!("{}: {e}", e.category())),
            )
        }
        Ok(bytes) => {
            let raw_len = bytes.len() as u64;
            let det = detect(&bytes);
            let tag = det.tag();
            match transcode_detected(&bytes, det, args.target_or_default()) {
                Err(ForgeError::UnknownEncoding) => {
                    // Same verdict as tree mode for identical bytes: recorded
                    // as `skipped_unknown` (summary `unknown` bucket,
                    // `unknown_files` list), printed once by `print_records`.
                    RecBuilder::new(&out_path).done(
                        Some(tag),
                        "skipped_unknown",
                        Some(raw_len),
                        None,
                        None,
                    )
                }
                Err(e) => {
                    // Printed exactly once, by `print_records`, against the
                    // output path — the same single-line contract as the
                    // generic fetch-error branch.
                    RecBuilder::new(&out_path).done(
                        Some(tag),
                        "error",
                        Some(raw_len),
                        None,
                        Some(format!("{}: {e}", e.category())),
                    )
                }
                Ok(outcome) => {
                    let new_bytes = match outcome {
                        TranscodeOutcome::Unchanged => bytes,
                        TranscodeOutcome::Changed(v) => v,
                    };
                    // Destination already holds the identical bytes → same
                    // no-churn contract as tree mode: `unchanged`, no write.
                    if read_file_limited(&out_path, new_bytes.len() as u64)
                        .is_ok_and(|existing| existing == new_bytes)
                    {
                        RecBuilder::new(&out_path).done(
                            Some(tag),
                            "unchanged",
                            Some(raw_len),
                            Some(new_bytes.len() as u64),
                            None,
                        )
                    } else if args.dry_run {
                        RecBuilder::new(&out_path).done(
                            Some(tag),
                            "would_convert",
                            Some(raw_len),
                            Some(new_bytes.len() as u64),
                            None,
                        )
                    } else {
                        // Same refusal as tree mode: POSIX rename would
                        // silently bypass the permission bits, but the
                        // operator locked that file on purpose.
                        let write_protected = fs::metadata(&out_path)
                            .map(|m| m.permissions().readonly())
                            .unwrap_or(false);
                        if write_protected {
                            RecBuilder::new(&out_path).done(
                                Some(tag),
                                "error",
                                Some(raw_len),
                                None,
                                Some(
                                    "io_error: file is write-protected (read-only); chmod +w to convert"
                                        .into(),
                                ),
                            )
                        } else {
                            if let Some(parent) = out_path.parent()
                                && !parent.as_os_str().is_empty()
                            {
                                fs::create_dir_all(parent).with_context(|| {
                                    format!("cannot create output dir {}", parent.display())
                                })?;
                            }
                            match writer::atomic_write(&out_path, &new_bytes) {
                                Ok(()) => RecBuilder::new(&out_path).done(
                                    Some(tag),
                                    "converted",
                                    Some(raw_len),
                                    Some(new_bytes.len() as u64),
                                    None,
                                ),
                                Err(e) => RecBuilder::new(&out_path).done(
                                    Some(tag),
                                    "error",
                                    Some(raw_len),
                                    None,
                                    Some(format!("{}: write failed: {e}", e.category())),
                                ),
                            }
                        }
                    }
                }
            }
        }
    };
    let elapsed = started.elapsed();

    let options = OptionsBlock {
        target: args.target_or_default().tag(),
        dry_run: args.dry_run,
        extensions: Vec::new(),
        exclude_dirs: Vec::new(),
        exclude_ext: Vec::new(),
        exclude_globs: Vec::new(),
        backup_dir: None,
        config_file: None,
        ignore_file: None,
    };
    let report = finalize_report(
        "fetch",
        RunFacts {
            root: None,
            url: Some(url.to_string()),
            options,
            started_at,
            verify_failed: None,
            warnings: Vec::new(),
        },
        vec![record],
        elapsed,
    );
    let summary = match &report.summary {
        Summary::Convert(s) => s.clone(),
        _ => ConvertSummary::default(),
    };
    print_records(&report.files, console, Some(args.target_or_default().tag()));
    if let Some(path) = &common.report {
        write_report(&report, path)?;
    }
    print_convert_summary(&summary, common.report.as_deref(), elapsed, console);
    // Unlike tree mode, an oversized payload fails the run: the single file
    // the user asked to save was not written.
    Ok(
        if summary.unknown > 0
            || summary.decode_errors > 0
            || summary.encode_errors > 0
            || summary.io_errors > 0
            || summary.fetch_error > 0
            || summary.too_large > 0
        {
            1
        } else {
            0
        },
    )
}

// ---------------------------------------------------------------------------
// fix / diff subcommands
// ---------------------------------------------------------------------------

/// `fix`: convert + verify in one pass over a single traversal.
///
/// Exit code describes the END state of the tree — the gate wins: `3` when
/// any file fails the verify pass (unknown files, oversized files, and I/O
/// errors all fail closed), else `1` for convert-side issues, else `0`.
/// With `--dry-run` the convert pass writes nothing but the verify pass still
/// runs read-only, so the preview reports the tree's current end state.
fn cmd_fix(args: &FixArgs) -> anyhow::Result<i32> {
    let mut args = args.clone();
    let config_file = merge_convert_config(&mut args)?;
    let common = &args.common;
    let target = args.target_or_default();
    // The verify gate asserts UTF-8 without BOM no matter what the convert
    // pass targeted, so any other target would guarantee exit 3 while having
    // rewritten the tree into a state the gate itself rejects. Refuse it up
    // front, covering both the CLI flag and the config file.
    if target != TargetEncoding::Utf8 {
        bail!(
            "fix requires --target utf-8 (the verify gate asserts UTF-8 without BOM); got `{}`",
            target
        );
    }
    ensure_root(&common.root)?;
    let filter = build_filter(common)?;
    // Same ordering as convert: probe the report path before creating the
    // backup directory, so a rejected report leaves nothing behind.
    if let Some(path) = &common.report {
        ensure_report_dir(path)?;
    }
    ensure_backup_dir(&args)?;
    let console = console_cfg(common);
    let dry_tag = if args.dry_run { " dry_run=true" } else { "" };
    print_header(
        console,
        format!(
            "mode=fix target={} root={}{}",
            target,
            common.root.display(),
            dry_tag
        ),
    );

    let (ignore, ignore_recorded, ignore_warnings) = load_ignore(common)?;
    ensure_report_not_ignore(common.report.as_deref(), ignore_recorded.as_deref())?;
    // Ignore-layer problems stay advisory: a dropped ignore file only
    // widens the walk, so the quiet contract applies to them.
    print_warnings(&ignore_warnings, console);
    let mut warnings = ignore_warnings;
    let walk = {
        let skip = skip_paths(
            common.report.as_deref(),
            ignore_recorded.as_deref().map(PathBuf::from).as_deref(),
        );
        collect_files(&common.root, &filter, &skip, ignore.as_ref())
    };
    let gate_warned = !walk.warnings.is_empty();
    warnings.extend(walk.warnings);
    // Gate modes surface traversal problems even under --quiet: an
    // uninspectable subtree fails the gate below.
    for w in warnings.iter().skip(warnings.len() - gate_warned as usize) {
        print_problem(w.clone());
    }

    let started_at = wall_now();
    let started = Instant::now();
    let ctx = ConvertCtx {
        root: common.root.clone(),
        target,
        dry_run: args.dry_run,
        backup_dir: args.backup_dir.clone(),
        max_file_size: common.effective_max_file_size(),
    };
    let convert_records = with_pool(common.threads, || {
        walk.files
            .par_iter()
            .map(|p| convert_file(p, &ctx))
            .collect::<Vec<_>>()
    })?;
    print_records(&convert_records, console, Some(target.tag()));

    // Verify pass over the SAME traversal — no re-walk, so both passes see an
    // identical candidate list.
    let verify_records = with_pool(common.threads, || {
        walk.files
            .par_iter()
            .map(|p| verify_file(p, common.effective_max_file_size()))
            .collect::<Vec<_>>()
    })?;
    let mut verify_failed: Vec<String> = verify_records
        .iter()
        .filter(|r| r.action == "failed")
        .map(|r| r.path.clone())
        .collect();
    verify_failed.sort();
    for r in &verify_records {
        if r.action == "failed" {
            let reason = r
                .error
                .clone()
                .unwrap_or_else(|| format!("detected={}", r.detected.unwrap_or("?")));
            print_problem(format!("failed {} ({reason})", r.path));
        }
    }
    let elapsed = started.elapsed();

    let mut report = finalize_report(
        "fix",
        RunFacts {
            root: Some(common.root.clone()),
            url: None,
            options: options_block(
                common,
                Some(&args as &dyn ConvertOptions),
                &filter,
                config_file.as_deref(),
                ignore_recorded.as_deref(),
            ),
            started_at,
            verify_failed: Some(verify_failed.clone()),
            warnings: warnings.clone(),
        },
        convert_records,
        elapsed,
    );
    // The verify pass counts ride on the fix summary; they are computed from
    // the gate records, not the convert records.
    let verify_failed_count = verify_failed.len() as u64;
    if let Summary::Fix(f) = &mut report.summary {
        f.passed = verify_records
            .iter()
            .filter(|r| r.action == "passed")
            .count() as u64;
        f.failed = verify_failed_count;
    }
    let convert_issues = match &report.summary {
        Summary::Fix(f) => f.unknown + f.decode_errors + f.encode_errors + f.io_errors,
        _ => 0,
    };
    if let Some(path) = &common.report {
        write_report(&report, path)?;
    }
    if let Summary::Fix(f) = &report.summary
        && !console.quiet
    {
        println!(
            "[utf8-forge] scanned={}  candidates={}",
            f.total_files, f.candidates
        );
        println!(
            "[utf8-forge] already_utf8={}  converted={}  unknown={}  errors={}",
            f.already_utf8,
            f.converted,
            f.unknown,
            f.decode_errors + f.encode_errors + f.io_errors
        );
        if f.too_large > 0 {
            println!("[utf8-forge] too_large={}", f.too_large);
        }
        println!(
            "[utf8-forge] verify: checked={}  passed={}  failed={}",
            f.passed + f.failed,
            f.passed,
            f.failed
        );
    }
    print_duration(common.report.as_deref(), elapsed, console);
    // Gate wins: the exit code describes the end state of the tree.
    Ok(if verify_failed_count > 0 || gate_warned {
        3
    } else if convert_issues > 0 {
        1
    } else {
        0
    })
}

/// `diff`: compare two report JSONs per path. Inspection tool — exit 0 by
/// default even when differences exist; `--fail-on-change` turns a non-empty
/// diff into exit 1 for CI gates.
fn cmd_diff(args: &DiffArgs) -> anyhow::Result<i32> {
    let load = |p: &Path, flag: &str| -> anyhow::Result<DiffInput> {
        let text = fs::read_to_string(p)
            .with_context(|| format!("cannot load {flag} report `{}`", p.display()))?;
        serde_json::from_str(&text)
            .with_context(|| format!("cannot load {flag} report `{}`", p.display()))
    };
    let before_raw = load(&args.before, "--before")?;
    let after_raw = load(&args.after, "--after")?;
    if let (Some(r1), Some(r2)) = (&before_raw.root, &after_raw.root)
        && r1 != r2
    {
        print_problem(format!(
            "warning: roots differ (`{r1}` vs `{r2}`); paths are compared relative to each root"
        ));
    }

    // Records are compared relative to each report's own root, so the same
    // tree scanned under two root directories diffs clean.
    let before = DiffInput {
        mode: before_raw.mode.clone(),
        root: before_raw.root.clone(),
        files: differ::relativize_files(before_raw.files, before_raw.root.as_deref()),
    };
    let after = DiffInput {
        mode: after_raw.mode.clone(),
        root: after_raw.root.clone(),
        files: differ::relativize_files(after_raw.files, after_raw.root.as_deref()),
    };
    let mut doc = differ::compare(&before, &after);
    doc.before = args.before.to_string_lossy().into_owned();
    doc.after = args.after.to_string_lossy().into_owned();
    let summary = &doc.summary;

    // The summary line goes to stderr; stdout carries only the JSON document
    // (when no --report), so piping the diff into other tools stays clean.
    if let Some(path) = &args.report {
        ensure_report_dir(path)?;
        let json = serde_json::to_string_pretty(&doc).context("diff serialization failed")?;
        writer::atomic_write(path, (json + "\n").as_bytes())
            .with_context(|| format!("cannot write diff report {}", path.display()))?;
        eprintln!(
            "[utf8-forge] diff: added={}  removed={}  changed={}  unchanged={}  report={}",
            summary.added,
            summary.removed,
            summary.action_changed,
            summary.unchanged,
            path.display()
        );
    } else {
        eprintln!(
            "[utf8-forge] diff: added={}  removed={}  changed={}  unchanged={}",
            summary.added, summary.removed, summary.action_changed, summary.unchanged
        );
        let json = serde_json::to_string_pretty(&doc).context("diff serialization failed")?;
        println!("{json}");
    }
    let drifted = summary.added + summary.removed + summary.action_changed;
    Ok(if args.fail_on_change && drifted > 0 {
        1
    } else {
        0
    })
}

#[cfg(test)]
mod bounded_read_tests {
    use super::read_file_limited;
    use std::fs;

    #[test]
    fn reads_only_cap_plus_one_bytes() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("growing.cpp");
        // Exercise the read independently of the stat pre-check, as when a
        // file grows between the pre-check and opening it.
        fs::write(&path, vec![b'x'; 65536]).unwrap();
        let bytes = read_file_limited(&path, 4096).unwrap();
        assert_eq!(bytes, vec![b'x'; 4097]);
        assert_eq!(read_file_limited(&path, 0).unwrap(), b"x");
    }

    #[test]
    fn handles_exact_limit_empty_files_and_unlimited_size() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("a.cpp");
        fs::write(&path, b"abc").unwrap();
        assert_eq!(read_file_limited(&path, 3).unwrap(), b"abc");
        assert_eq!(read_file_limited(&path, u64::MAX).unwrap(), b"abc");
        fs::write(&path, b"").unwrap();
        assert!(read_file_limited(&path, 0).unwrap().is_empty());
        assert_eq!(
            read_file_limited(&dir.path().join("missing.cpp"), 3)
                .unwrap_err()
                .kind(),
            std::io::ErrorKind::NotFound
        );
    }
}

#[cfg(test)]
mod backup_placement_tests {
    use super::check_backup_placement;
    use std::path::Path;

    #[test]
    fn relative_backup_dir_resolves_against_cwd() {
        // The root argument must be canonicalized, exactly as the real
        // caller passes it: on Windows canonicalize yields a `\\?\`
        // verbatim prefix, so a plain current_dir() would never compare
        // equal to the projected path.
        let cwd = std::fs::canonicalize(std::env::current_dir().unwrap()).unwrap();
        // A not-yet-created relative dir lands under the cwd, outside a root
        // elsewhere.
        check_backup_placement(Path::new("backup"), &cwd.join("elsewhere-root")).unwrap();
        // Deep non-existing relative tails resolve through the cwd too.
        check_backup_placement(Path::new("a/b/c"), &cwd.join("elsewhere-root")).unwrap();
        // The same relative dir is fatal when the cwd itself is the root.
        let err = check_backup_placement(Path::new("backup"), &cwd).unwrap_err();
        assert!(err.to_string().contains("inside"), "{err}");
    }
}