confers 0.4.1

Production-ready Rust configuration library with zero boilerplate
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
//! Error handling for confers configuration library.
//!
//! This module provides comprehensive error types with:
//! - Precise error location (file, line, column)
//! - Sanitized messages for user display
//! - Audit-safe messages for logging
//! - Retryable error classification
//!
//! # BrickArchitecture: Phase Separation
//!
//! This module follows BrickArchitecture error phase separation:
//!
//! - **`ConfigConfigError`** — Configuration phase errors (initialization time)
//!   - See [`config_error`] module for full documentation
//!   - Returned by factory functions and builders
//!
//! - **`ConfersError`** — Runtime errors (use time)
//!   - Defined in this module
//!   - Returned by trait methods during configuration use
//!
//! # Backward Compatibility
//!
//! The following aliases are provided for backward compatibility:
//! - `ConfigError` → `ConfersError`
//! - `ConfigResult<T>` → `ConfersResult<T>`
//! - `ErrorCode` → Runtime error codes

// Configuration phase errors (initialization time)
pub mod config_error;

// Re-export configuration phase error types
pub use config_error::ConfigConfigError;
pub use config_error::{ConfigErrorCode, InitResult};

use std::path::PathBuf;
use std::sync::LazyLock;
use thiserror::Error;

// Re-export SourceLocation as ParseLocation for backward compatibility
pub use crate::types::SourceLocation as ParseLocation;

// Precompiled regex patterns for sanitization (avoid recompiling on each call)

/// Regex pattern for matching file paths (Unix and Windows style)
static PATH_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"/[a-zA-Z0-9_\-./]+|[a-zA-Z]:\\[a-zA-Z0-9_\-./\\]+")
        .expect("PATH_RE regex is valid")
});

/// Regex pattern for matching IP addresses
static IP_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b").expect("IP_RE regex is valid")
});

/// Regex pattern for matching potential key material (long hex strings)
static HEX_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"\b[0-9a-fA-F]{16,}\b").expect("HEX_RE regex is valid"));

/// Regex pattern for matching URLs with embedded credentials
static URL_WITH_CREDS_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"https?://[^/]+:[^@]+@[^/\s]+[/\s]?")
        .expect("URL_WITH_CREDS_RE regex is valid")
});

/// Regex pattern for matching JWT tokens
static JWT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"eyJ[A-Za-z0-9_=-]+\.[A-Za-z0-9_=-]*\.?[A-Za-z0-9_=-]*")
        .expect("JWT_RE regex is valid")
});

/// Regex pattern for matching AWS access key IDs
static AWS_AK_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"\bAKIA[0-9A-Z]{16}\b").expect("AWS_AK_RE regex is valid"));

/// Regex pattern for matching AWS secret access keys (40-char alphanumeric)
static AWS_SAK_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r"\b[A-Za-z0-9/+=]{40}\b").expect("AWS_SAK_RE regex is valid")
});

/// Stable numeric error codes for programmatic handling.
/// Values follow category-based ranges per dev-v2.md spec:
///   1-9 = File/IO, 100-199 = Validation, 200-299 = Crypto,
///   300-399 = Remote, 400-499 = Reference/Processing,
///   500-599 = Size/Watcher, 600-699 = Version/Migration,
///   700-799 = Modules, 800-899 = Multi-source,
///   900-999 = Timeout/Concurrency/Other
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum ErrorCode {
    FileNotFound = 1,
    FilePermission = 2,
    FileParseError = 3,
    IoError = 10,
    ValidationFailed = 100,
    TypeMismatch = 101,
    InvalidValue = 102,
    SchemaValidationFailed = 103,
    DecryptionFailed = 200,
    KeyNotFound = 201,
    KeyTooWeak = 202,
    KeyRotationFailed = 203,
    RemoteUnavailable = 300,
    RemoteTimeout = 301,
    CircularReference = 400,
    OverrideBlocked = 401,
    InterpolationError = 402,
    SizeLimitExceeded = 500,
    WatcherError = 501,
    VersionMismatch = 600,
    MigrationFailed = 601,
    ModuleNotFound = 700,
    ReloadRolledBack = 701,
    MultipleSources = 800,
    Timeout = 900,
    ConcurrencyConflict = 901,
    LockPoisoned = 902,
    HealthCheckFailed = 903,
    Unknown = 999,
}

impl std::fmt::Display for ErrorCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorCode::FileNotFound => write!(f, "FILE_NOT_FOUND"),
            ErrorCode::FilePermission => write!(f, "FILE_PERMISSION"),
            ErrorCode::FileParseError => write!(f, "FILE_PARSE_ERROR"),
            ErrorCode::IoError => write!(f, "IO_ERROR"),
            ErrorCode::ValidationFailed => write!(f, "VALIDATION_FAILED"),
            ErrorCode::TypeMismatch => write!(f, "TYPE_MISMATCH"),
            ErrorCode::InvalidValue => write!(f, "INVALID_VALUE"),
            ErrorCode::SchemaValidationFailed => write!(f, "SCHEMA_VALIDATION_FAILED"),
            ErrorCode::DecryptionFailed => write!(f, "DECRYPTION_FAILED"),
            ErrorCode::KeyNotFound => write!(f, "KEY_NOT_FOUND"),
            ErrorCode::KeyTooWeak => write!(f, "KEY_TOO_WEAK"),
            ErrorCode::KeyRotationFailed => write!(f, "KEY_ROTATION_FAILED"),
            ErrorCode::RemoteUnavailable => write!(f, "REMOTE_UNAVAILABLE"),
            ErrorCode::RemoteTimeout => write!(f, "REMOTE_TIMEOUT"),
            ErrorCode::CircularReference => write!(f, "CIRCULAR_REFERENCE"),
            ErrorCode::OverrideBlocked => write!(f, "OVERRIDE_BLOCKED"),
            ErrorCode::InterpolationError => write!(f, "INTERPOLATION_ERROR"),
            ErrorCode::SizeLimitExceeded => write!(f, "SIZE_LIMIT_EXCEEDED"),
            ErrorCode::WatcherError => write!(f, "WATCHER_ERROR"),
            ErrorCode::VersionMismatch => write!(f, "VERSION_MISMATCH"),
            ErrorCode::MigrationFailed => write!(f, "MIGRATION_FAILED"),
            ErrorCode::ModuleNotFound => write!(f, "MODULE_NOT_FOUND"),
            ErrorCode::ReloadRolledBack => write!(f, "RELOAD_ROLLED_BACK"),
            ErrorCode::MultipleSources => write!(f, "MULTIPLE_SOURCES"),
            ErrorCode::Timeout => write!(f, "TIMEOUT"),
            ErrorCode::ConcurrencyConflict => write!(f, "CONCURRENCY_CONFLICT"),
            ErrorCode::LockPoisoned => write!(f, "LOCK_POISONED"),
            ErrorCode::HealthCheckFailed => write!(f, "HEALTH_CHECK_FAILED"),
            ErrorCode::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Comprehensive configuration error type.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConfigError {
    /// Configuration file not found.
    #[error("Configuration file not found: {filename}")]
    FileNotFound {
        /// The filename that was not found
        filename: PathBuf,
        /// Optional source error
        source: Option<std::io::Error>,
    },

    /// Parse error in configuration file.
    #[error("Failed to parse {format}{}: {message}", .location.as_ref().map(|l| format!(" at {}", l)).unwrap_or_default())]
    ParseError {
        /// Format being parsed (toml, json, yaml)
        format: String,
        /// Human-readable error message
        message: String,
        /// Optional precise location
        location: Option<ParseLocation>,
        /// Source error
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    },

    /// Validation failed for a field.
    #[error("Validation failed for field '{field}': {message} (rule: {rule})")]
    ValidationFailed {
        /// Field path that failed validation
        field: String,
        /// Validation rule that failed
        rule: String,
        /// Human-readable error message
        message: String,
    },

    /// Schema validation failed with error count.
    #[error("schema validation failed with {count} error(s)")]
    SchemaValidationFailed {
        /// Number of schema validation errors
        count: usize,
    },

    /// Decryption failed.
    #[error("Decryption failed: {message}")]
    DecryptionFailed {
        /// Sanitized error message (no sensitive data)
        message: String,
    },

    /// Remote configuration source unavailable.
    #[error("Remote configuration source unavailable")]
    RemoteUnavailable {
        /// Sanitized error type
        error_type: String,
        /// Whether the error is retryable
        retryable: bool,
    },

    /// Configuration version mismatch.
    #[error("Configuration version mismatch: found {found}, expected {expected}")]
    VersionMismatch {
        /// Version found in configuration
        found: u32,
        /// Expected version
        expected: u32,
    },

    /// Migration failed.
    #[error("Migration failed from v{from} to v{to}: {reason}")]
    MigrationFailed {
        /// Source version
        from: u32,
        /// Target version
        to: u32,
        /// Failure reason
        reason: String,
        /// Source error
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    },

    /// Module not found in a group.
    #[error("Module '{module}' not found in group '{group}'")]
    ModuleNotFound {
        /// Group name
        group: String,
        /// Module name
        module: String,
    },

    /// Reload was rolled back.
    #[error("Configuration reload rolled back: {reason}")]
    ReloadRolledBack {
        /// Reason for rollback
        reason: String,
    },

    /// IO error.
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    /// Invalid configuration value.
    #[error("Invalid configuration value for '{key}': {message}")]
    InvalidValue {
        /// Configuration key
        key: String,
        /// Expected type
        expected_type: String,
        /// Error message
        message: String,
    },

    /// Source chain error.
    #[error("Source chain error: {message}")]
    SourceChainError {
        /// Error message
        message: String,
        /// Source index
        source_index: usize,
    },

    /// Timeout error.
    #[error("Operation timed out after {duration_ms}ms")]
    Timeout {
        /// Timeout duration in milliseconds
        duration_ms: u64,
    },

    /// Size limit exceeded.
    #[error("Configuration size limit exceeded: {actual} bytes (limit: {limit})")]
    SizeLimitExceeded {
        /// Actual size in bytes
        actual: usize,
        /// Configured limit
        limit: usize,
    },

    /// Interpolation error.
    #[error("Interpolation error for '{variable}': {message}")]
    InterpolationError {
        /// Variable name
        variable: String,
        /// Error message
        message: String,
    },

    /// Encryption key error.
    #[error("Encryption key error: {message}")]
    KeyError {
        /// Sanitized error message
        message: String,
    },

    /// Circular reference detected.
    #[error("Circular reference detected: {path}")]
    CircularReference {
        /// Reference path
        path: String,
    },

    #[error("Lock poisoned for resource '{resource}'")]
    LockPoisoned { resource: String },

    /// Multi-source error.
    #[error("Multiple sources failed")]
    MultiSource {
        /// The wrapped multi-source error
        #[source]
        source: MultiSourceError,
    },

    /// Concurrency conflict during configuration access.
    #[error("Concurrency conflict on key '{key}': {message}")]
    ConcurrencyConflict {
        /// The key that had the conflict
        key: String,
        /// Conflict description
        message: String,
        /// Expected value type
        expected_type: Option<String>,
    },

    /// Key rotation failed.
    #[error("Key rotation failed from '{from_version}' to '{to_version}': {reason}")]
    KeyRotationFailed {
        /// Source key version
        from_version: String,
        /// Target key version
        to_version: String,
        /// Failure reason
        reason: String,
    },

    /// Watcher error.
    #[error("Configuration watcher error: {message}")]
    WatcherError {
        /// Error message
        message: String,
        /// Source path being watched
        path: Option<PathBuf>,
        /// Whether the error is recoverable
        recoverable: bool,
    },

    /// Override blocked by protection rules.
    #[error("Override blocked for key '{key}': {reason}")]
    OverrideBlocked {
        /// The key that was blocked
        key: String,
        /// Reason for blocking
        reason: String,
        /// Source attempting the override
        override_source: Option<String>,
    },

    /// Health check failed.
    #[error("Health check failed: {reason}")]
    HealthCheckFailed {
        /// Reason for health check failure
        reason: String,
    },
}

impl ConfigError {
    /// Get the error code for this error.
    pub fn code(&self) -> ErrorCode {
        match self {
            ConfigError::FileNotFound { .. } => ErrorCode::FileNotFound,
            ConfigError::ParseError { .. } => ErrorCode::FileParseError,
            ConfigError::ValidationFailed { .. } => ErrorCode::ValidationFailed,
            ConfigError::SchemaValidationFailed { .. } => ErrorCode::SchemaValidationFailed,
            ConfigError::DecryptionFailed { .. } => ErrorCode::DecryptionFailed,
            ConfigError::RemoteUnavailable { .. } => ErrorCode::RemoteUnavailable,
            ConfigError::VersionMismatch { .. } => ErrorCode::VersionMismatch,
            ConfigError::MigrationFailed { .. } => ErrorCode::MigrationFailed,
            ConfigError::ModuleNotFound { .. } => ErrorCode::ModuleNotFound,
            ConfigError::ReloadRolledBack { .. } => ErrorCode::ReloadRolledBack,
            ConfigError::IoError(_) => ErrorCode::IoError,
            ConfigError::InvalidValue { .. } => ErrorCode::InvalidValue,
            ConfigError::SourceChainError { .. } => ErrorCode::MultipleSources,
            ConfigError::Timeout { .. } => ErrorCode::Timeout,
            ConfigError::SizeLimitExceeded { .. } => ErrorCode::SizeLimitExceeded,
            ConfigError::InterpolationError { .. } => ErrorCode::InterpolationError,
            ConfigError::KeyError { .. } => ErrorCode::KeyNotFound,
            ConfigError::CircularReference { .. } => ErrorCode::CircularReference,
            ConfigError::LockPoisoned { .. } => ErrorCode::LockPoisoned,
            ConfigError::MultiSource { .. } => ErrorCode::MultipleSources,
            ConfigError::ConcurrencyConflict { .. } => ErrorCode::ConcurrencyConflict,
            ConfigError::KeyRotationFailed { .. } => ErrorCode::KeyRotationFailed,
            ConfigError::WatcherError { .. } => ErrorCode::WatcherError,
            ConfigError::OverrideBlocked { .. } => ErrorCode::OverrideBlocked,
            ConfigError::HealthCheckFailed { .. } => ErrorCode::HealthCheckFailed,
        }
    }

    /// Create a validation error from a garde Report.
    #[cfg(feature = "validation")]
    pub fn validation_error(message: &str, report: garde::Report) -> Self {
        // Extract the first error for a more specific message
        let (field, rule, msg) = report
            .iter()
            .next()
            .map(|(path, error)| {
                let field = path.to_string();
                let error_msg = format!("{}", error);
                (field, "garde".to_string(), error_msg)
            })
            .unwrap_or_else(|| {
                (
                    "unknown".to_string(),
                    "garde".to_string(),
                    message.to_string(),
                )
            });

        ConfigError::ValidationFailed {
            field,
            rule,
            message: msg,
        }
    }

    /// Create a validation error with custom details.
    pub fn validation(
        field: impl Into<String>,
        rule: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        ConfigError::ValidationFailed {
            field: field.into(),
            rule: rule.into(),
            message: message.into(),
        }
    }

    /// Check if this error is retryable.
    pub fn is_retryable(&self) -> bool {
        match self {
            ConfigError::RemoteUnavailable { retryable, .. } => *retryable,
            ConfigError::Timeout { .. } => true,
            ConfigError::IoError(e) => {
                // Network-related IO errors are retryable
                matches!(
                    e.kind(),
                    std::io::ErrorKind::ConnectionRefused
                        | std::io::ErrorKind::ConnectionReset
                        | std::io::ErrorKind::ConnectionAborted
                        | std::io::ErrorKind::TimedOut
                        | std::io::ErrorKind::WouldBlock
                )
            }
            ConfigError::WatcherError { recoverable, .. } => *recoverable,
            ConfigError::ConcurrencyConflict { .. } => true,
            _ => false,
        }
    }

    /// Get a sanitized message for user display.
    pub fn user_message(&self) -> String {
        match self {
            ConfigError::FileNotFound { filename, .. } => {
                // Sanitize paths that may contain sensitive information
                let path_str = filename.display().to_string();
                // Truncate sensitive paths (.ssh, .aws, etc.) to just the filename
                let sanitized = if path_str.contains(".ssh")
                    || path_str.contains(".aws")
                    || path_str.contains(".gcloud")
                    || path_str.contains(".env")
                    || path_str.contains(".kube")
                {
                    filename
                        .file_name()
                        .map(|n| n.to_string_lossy().into_owned())
                        .unwrap_or_else(|| "<hidden>".to_string())
                } else {
                    path_str
                };
                format!("Configuration file '{}' not found", sanitized)
            }
            ConfigError::ParseError {
                format,
                location,
                message,
                ..
            } => {
                if let Some(loc) = location {
                    format!("Failed to parse {} at {}: {}", format, loc, message)
                } else {
                    format!("Failed to parse {}: {}", format, message)
                }
            }
            ConfigError::ValidationFailed { field, message, .. } => {
                format!("Field '{}' failed validation: {}", field, message)
            }
            ConfigError::SchemaValidationFailed { count } => {
                format!("Schema validation failed with {} error(s)", count)
            }
            ConfigError::DecryptionFailed { .. } => {
                "Failed to decrypt configuration value".to_string()
            }
            ConfigError::RemoteUnavailable { .. } => {
                "Remote configuration source is unavailable".to_string()
            }
            ConfigError::VersionMismatch { found, expected } => {
                format!(
                    "Configuration version mismatch: found {}, expected {}",
                    found, expected
                )
            }
            ConfigError::MigrationFailed {
                from, to, reason, ..
            } => {
                format!("Migration from v{} to v{} failed: {}", from, to, reason)
            }
            ConfigError::ModuleNotFound { group, module } => {
                format!("Module '{}' not found in group '{}'", module, group)
            }
            ConfigError::ReloadRolledBack { reason } => {
                format!("Configuration reload was rolled back: {}", reason)
            }
            ConfigError::IoError(e) => format!("IO error: {}", e),
            ConfigError::InvalidValue { key, message, .. } => {
                format!("Invalid value for '{}': {}", key, message)
            }
            ConfigError::SourceChainError { message, .. } => message.clone(),
            ConfigError::Timeout { duration_ms } => {
                format!("Operation timed out after {}ms", duration_ms)
            }
            ConfigError::SizeLimitExceeded { actual, limit } => {
                format!("Size limit exceeded: {} bytes (limit: {})", actual, limit)
            }
            ConfigError::InterpolationError { variable, message } => {
                format!("Interpolation error for '{}': {}", variable, message)
            }
            ConfigError::KeyError { .. } => "Encryption key error".to_string(),
            ConfigError::CircularReference { path } => {
                format!("Circular reference detected: {}", path)
            }
            ConfigError::LockPoisoned { resource } => {
                format!("Lock poisoned for resource '{}'", resource)
            }
            ConfigError::MultiSource { source } => {
                format!(
                    "Multiple sources failed: {}/{}",
                    source.failed_count, source.total_count
                )
            }
            ConfigError::ConcurrencyConflict { key, message, .. } => {
                format!("Concurrency conflict on key '{}': {}", key, message)
            }
            ConfigError::KeyRotationFailed {
                from_version,
                to_version,
                reason,
            } => {
                format!(
                    "Key rotation failed from '{}' to '{}': {}",
                    from_version, to_version, reason
                )
            }
            ConfigError::WatcherError {
                message,
                path,
                recoverable,
            } => {
                let path_str = path
                    .as_ref()
                    .map(|p| format!(" for '{}'", p.display()))
                    .unwrap_or_default();
                let recovery_str = if *recoverable { " (recoverable)" } else { "" };
                format!("Watcher error{}: {}{}", path_str, message, recovery_str)
            }
            ConfigError::OverrideBlocked {
                key,
                reason,
                override_source,
            } => {
                let source_str = override_source
                    .as_ref()
                    .map(|s| format!(" from '{}'", s))
                    .unwrap_or_default();
                format!(
                    "Override blocked for key '{}'{}: {}",
                    key, source_str, reason
                )
            }
            ConfigError::HealthCheckFailed { reason } => {
                format!("Health check failed: {}", reason)
            }
        }
    }

    /// Get a detailed debug message for internal logging.
    ///
    /// Unlike `user_message()` which is safe to show to end users, this method
    /// may include file paths, IP addresses, and other diagnostic information
    /// useful for debugging. Do NOT expose this message to end users.
    ///
    /// For structured logging, prefer using the `error_code()` and field accessors.
    pub fn debug_message(&self) -> String {
        // Use the Display impl which gives full details
        let full = format!("{}", self);

        // Apply additional sanitization that still keeps some context
        let mut result = full;

        // Remove credentials from URLs but keep the URL structure
        result = URL_WITH_CREDS_RE
            .replace_all(&result, "https://<creds>@<host>/")
            .to_string();

        // Keep file paths but redact the directory part
        result = PATH_RE
            .replace_all(&result, |caps: &regex::Captures| {
                let full_path = &caps[0];
                full_path
                    .split('/')
                    .next_back()
                    .or_else(|| full_path.split('\\').next_back())
                    .map(|f| format!("<path>/{}", f))
                    .unwrap_or_else(|| "<path>".to_string())
            })
            .to_string();

        result
    }

    /// Check if this error may contain sensitive data.
    ///
    /// Returns `true` for errors that are likely to contain sensitive information
    /// such as keys, passwords, tokens, or credentials. Use this to determine
    /// whether to sanitize error messages before logging or displaying.
    ///
    /// Note: This is a heuristic check and may return `false` positives.
    /// Always prefer explicit sanitization via `sanitize_error_message()`.
    pub fn is_sensitive(&self) -> bool {
        // Check if the raw error message contains sensitive patterns
        let raw = format!("{}", self);

        // Check for sensitive patterns in the raw error
        JWT_RE.is_match(&raw)
            || AWS_AK_RE.is_match(&raw)
            || URL_WITH_CREDS_RE.is_match(&raw)
            || (HEX_RE.is_match(&raw) && raw.len() > 50) // Long hex strings are more likely keys
            || {
                // Check for common key/password field names
                let lower = raw.to_lowercase();
                lower.contains("secret")
                    || lower.contains("password")
                    || lower.contains("token")
                    || lower.contains("api_key")
                    || lower.contains("private_key")
                    || lower.contains("credential")
                    || lower.contains(" key ")  // standalone "key" word
                    || lower.ends_with("key")   // suffix "key" (e.g., "encryption key")
            }
    }

    /// Get the error chain with sensitive data removed.
    pub fn sanitized_chain(&self) -> Vec<String> {
        let mut chain = vec![self.user_message()];

        // Add source errors if present, but sanitize them
        match self {
            ConfigError::ParseError { source, .. }
            | ConfigError::MigrationFailed { source, .. } => {
                if let Some(e) = source {
                    chain.push(sanitize_error_message(&e.to_string()));
                }
            }
            _ => {}
        }

        chain
    }

    /// Get a message suitable for audit logging.
    pub fn audit_message(&self) -> String {
        format!(
            "operation=config error_code={} error_type={} message={}",
            self.code() as u16,
            self.code(),
            self.user_message()
        )
    }
}

/// Sanitize an error message by removing sensitive data.
///
/// This is the central sanitization function used by `user_message()` and
/// `sanitized_chain()`. It removes:
/// - File paths (replaced with `<path>/filename`)
/// - IP addresses (replaced with `<ip>`)
/// - Long hex strings / key material (replaced with `<redacted>`)
/// - URLs with embedded credentials
/// - JWT tokens
/// - AWS access key IDs
///
/// The user-facing message will not contain any of these sensitive patterns.
fn sanitize_error_message(msg: &str) -> String {
    let mut result = msg.to_string();

    // Remove URLs with embedded credentials first (before other replacements)
    result = URL_WITH_CREDS_RE
        .replace_all(&result, "<redacted_url>")
        .to_string();

    // Remove potential file paths (Unix and Windows style) using precompiled regex
    result = PATH_RE
        .replace_all(&result, |caps: &regex::Captures| {
            let full_path = &caps[0];
            // Keep only the filename for debugging
            if let Some(filename) = full_path
                .split('/')
                .next_back()
                .or_else(|| full_path.split('\\').next_back())
            {
                format!("<path>/{}", filename)
            } else {
                "<path>".to_string()
            }
        })
        .to_string();

    // Remove potential IP addresses using precompiled regex
    result = IP_RE.replace_all(&result, "<ip>").to_string();

    // Remove JWT tokens using precompiled regex
    result = JWT_RE.replace_all(&result, "<jwt_token>").to_string();

    // Remove AWS access key IDs using precompiled regex
    result = AWS_AK_RE
        .replace_all(&result, "<aws_access_key>")
        .to_string();

    // Remove AWS secret access keys (40-char strings near AWS context)
    // Only redact if surrounded by whitespace or common delimiters
    result = AWS_SAK_RE
        .replace_all(&result, "<aws_secret_key>")
        .to_string();

    // Remove potential key material (long hex strings) using precompiled regex
    result = HEX_RE.replace_all(&result, "<redacted>").to_string();

    result
}

/// Error from multiple failed sources.
#[derive(Debug, Error)]
#[error("multiple sources failed: {failed_count}/{total_count}")]
pub struct MultiSourceError {
    /// Errors from each failed source (source_name, error)
    pub errors: Vec<(String, ConfigError)>,
    /// Partially loaded configuration (if any), as JSON Value
    pub partial_config: Option<serde_json::Value>,
    /// Whether a fallback configuration was used
    pub fallback_used: bool,
    /// Number of failed sources
    pub failed_count: usize,
    /// Total number of sources attempted
    pub total_count: usize,
}

impl MultiSourceError {
    /// Create a new multi-source error.
    pub fn new<T: Into<String>>(total_count: usize, errors: Vec<(T, ConfigError)>) -> Self {
        let errors: Vec<(String, ConfigError)> =
            errors.into_iter().map(|(n, e)| (n.into(), e)).collect();
        let failed_count = errors.len();
        Self {
            errors,
            partial_config: None,
            fallback_used: false,
            failed_count,
            total_count,
        }
    }

    /// Create with partial configuration.
    pub fn with_partial<T: Into<String>>(
        total_count: usize,
        errors: Vec<(T, ConfigError)>,
        partial: serde_json::Value,
    ) -> Self {
        let errors: Vec<(String, ConfigError)> =
            errors.into_iter().map(|(n, e)| (n.into(), e)).collect();
        let failed_count = errors.len();
        Self {
            errors,
            partial_config: Some(partial),
            fallback_used: false,
            failed_count,
            total_count,
        }
    }

    /// Get the errors.
    pub fn errors(&self) -> &[(String, ConfigError)] {
        &self.errors
    }

    /// Get the partial configuration if available.
    pub fn partial_config(&self) -> Option<&serde_json::Value> {
        self.partial_config.as_ref()
    }

    /// Check if the build had partial success.
    pub fn has_partial_success(&self) -> bool {
        self.partial_config.is_some()
    }

    /// Count errors of a specific type.
    pub fn count_error_type(&self, code: ErrorCode) -> usize {
        self.errors.iter().filter(|(_, e)| e.code() == code).count()
    }
}

/// Result of a configuration build operation with diagnostic information.
#[derive(Debug)]
pub struct BuildResult<T> {
    /// The built configuration
    pub config: T,
    /// Warnings encountered during build
    pub warnings: Vec<SourceWarning>,
    /// Whether the build is in degraded mode
    pub degraded: bool,
    /// Reason for degraded mode (if applicable)
    pub degraded_reason: Option<String>,
}

impl<T> BuildResult<T> {
    /// Create a successful build result.
    pub fn ok(config: T) -> Self {
        Self {
            config,
            warnings: Vec::new(),
            degraded: false,
            degraded_reason: None,
        }
    }

    /// Create a build result with warnings.
    pub fn with_warnings(config: T, warnings: Vec<SourceWarning>) -> Self {
        Self {
            config,
            warnings,
            degraded: false,
            degraded_reason: None,
        }
    }

    /// Create a degraded build result.
    pub fn degraded(config: T, reason: impl Into<String>) -> Self {
        Self {
            config,
            warnings: Vec::new(),
            degraded: true,
            degraded_reason: Some(reason.into()),
        }
    }

    /// Check if there are any warnings.
    pub fn has_warnings(&self) -> bool {
        !self.warnings.is_empty()
    }

    /// Map the inner config to a new type.
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> BuildResult<U> {
        BuildResult {
            config: f(self.config),
            warnings: self.warnings,
            degraded: self.degraded,
            degraded_reason: self.degraded_reason,
        }
    }
}

/// Warning encountered during configuration build.
#[derive(Debug, Clone)]
pub struct SourceWarning {
    /// Warning message
    pub message: String,
    /// Warning source (file, source name, etc.)
    pub source: Option<String>,
    /// Warning code
    pub code: WarningCode,
}

/// Warning codes for build warnings.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarningCode {
    /// A source was skipped due to optional flag
    OptionalSourceSkipped,
    /// A deprecated configuration key was used
    DeprecatedKey,
    /// A default value was used for missing key
    DefaultUsed,
    /// A value was truncated
    ValueTruncated,
    /// A remote source fallback was used
    RemoteFallback,
    /// A sensitive field is unencrypted
    UnencryptedSensitive,
    /// A configuration key is unused
    UnusedKey,
}

impl std::fmt::Display for WarningCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WarningCode::OptionalSourceSkipped => write!(f, "OPTIONAL_SOURCE_SKIPPED"),
            WarningCode::DeprecatedKey => write!(f, "DEPRECATED_KEY"),
            WarningCode::DefaultUsed => write!(f, "DEFAULT_USED"),
            WarningCode::ValueTruncated => write!(f, "VALUE_TRUNCATED"),
            WarningCode::RemoteFallback => write!(f, "REMOTE_FALLBACK"),
            WarningCode::UnencryptedSensitive => write!(f, "UNENCRYPTED_SENSITIVE"),
            WarningCode::UnusedKey => write!(f, "UNUSED_KEY"),
        }
    }
}

/// Type alias for configuration results.
pub type ConfigResult<T> = Result<T, ConfigError>;

// ============== BrickArchitecture Error Types ==============

/// Runtime error type for BrickArchitecture compliance.
///
/// This is the canonical name for runtime configuration errors. It is
/// structurally identical to `ConfigError` (same enum), following the
/// BrickArchitecture convention that library users interact with `ConfersError`
/// while `ConfigError` serves as the internal implementation detail.
///
/// # Phase Distinction
///
/// | Phase | Error Type | When |
/// |-------|-----------|------|
/// | Configuration (init) | [`ConfigConfigError`] | Factory functions, builders |
/// | Runtime (use) | `ConfersError` | get/set/delete operations |
///
/// # Example
///
/// ```rust
/// use confers::ConfersError;
/// # fn main() {}
/// ```
pub type ConfersError = ConfigError;

/// Result type alias for runtime operations.
///
/// Equivalent to `Result<T, ConfersError>`. Use this for all runtime
/// configuration operations (get/set/delete/health_check/shutdown).
pub type ConfersResult<T> = Result<T, ConfersError>;

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

    #[test]
    fn test_error_code() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("config.toml"),
            source: None,
        };
        assert_eq!(err.code(), ErrorCode::FileNotFound);
        assert_eq!(err.code() as u16, 1);
    }

    #[test]
    fn test_is_retryable() {
        let err = ConfigError::Timeout { duration_ms: 1000 };
        assert!(err.is_retryable());

        let err = ConfigError::ValidationFailed {
            field: "port".to_string(),
            rule: "range".to_string(),
            message: "out of range".to_string(),
        };
        assert!(!err.is_retryable());

        let err = ConfigError::RemoteUnavailable {
            error_type: "timeout".to_string(),
            retryable: true,
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_user_message() {
        let err = ConfigError::VersionMismatch {
            found: 1,
            expected: 2,
        };
        assert_eq!(
            err.user_message(),
            "Configuration version mismatch: found 1, expected 2"
        );
    }

    #[test]
    fn test_parse_location() {
        let loc = ParseLocation::new("config.toml", 15, 8);
        assert_eq!(loc.to_string(), "config.toml:15:8");

        let loc =
            ParseLocation::from_path(std::path::Path::new("/home/user/secret/config.toml"), 10, 5);
        assert_eq!(loc.source_name.as_ref(), "config.toml");
        assert_eq!(loc.line, 10);
        assert_eq!(loc.column, 5);
    }

    #[test]
    fn test_sanitized_chain() {
        let err = ConfigError::DecryptionFailed {
            message: "key mismatch".to_string(),
        };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 1);
        assert!(!chain[0].contains("key material"));
    }

    #[test]
    fn test_audit_message() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("config.toml"),
            source: None,
        };
        let audit = err.audit_message();
        assert!(audit.contains("error_code=1"));
        assert!(audit.contains("FILE_NOT_FOUND"));
    }

    #[test]
    fn test_multi_source_error() {
        let err = MultiSourceError::new(
            5,
            vec![
                (
                    "source_a".to_string(),
                    ConfigError::Timeout { duration_ms: 1000 },
                ),
                (
                    "source_b".to_string(),
                    ConfigError::RemoteUnavailable {
                        error_type: "connection".to_string(),
                        retryable: true,
                    },
                ),
            ],
        );
        assert_eq!(err.failed_count, 2);
        assert_eq!(err.total_count, 5);
    }

    #[test]
    fn test_build_result() {
        let result: BuildResult<i32> = BuildResult::ok(42);
        assert!(!result.degraded);
        assert!(!result.has_warnings());

        let result: BuildResult<i32> = BuildResult::degraded(42, "remote source unavailable");
        assert!(result.degraded);
        assert_eq!(
            result.degraded_reason,
            Some("remote source unavailable".to_string())
        );
    }

    #[test]
    fn test_non_exhaustive() {
        // This test ensures the #[non_exhaustive] attribute is present
        // by checking that we cannot exhaustively match outside this module
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("test"),
            source: None,
        };

        // We can match known variants
        if let ConfigError::FileNotFound { filename, .. } = &err {
            assert_eq!(filename, &PathBuf::from("test"));
        }
    }

    // =============================================================================
    // Error Sanitization Tests (9.3.6)
    // =============================================================================

    #[test]
    fn test_sanitize_error_message_full_path() {
        let msg = "Failed to load /home/user/project/config.toml";
        let sanitized = sanitize_error_message(msg);
        // Full paths should be converted to <path>/filename
        assert!(!sanitized.contains("/home/user/project/"));
        assert!(sanitized.contains("config.toml"));
        assert!(sanitized.contains("<path>"));
    }

    #[test]
    fn test_sanitize_error_message_url_with_credentials() {
        let msg = "Failed to fetch https://user:secret123@example.com/config.json"; // pragma: allowlist secret
        let sanitized = sanitize_error_message(msg);
        // Should not contain the credentials
        assert!(!sanitized.contains("user:secret123"));
        assert!(sanitized.contains("<redacted_url>") || sanitized.contains("<redacted>"));
    }

    #[test]
    fn test_sanitize_error_message_jwt_token() {
        let msg = "Validation failed for token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4ifQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; // pragma: allowlist secret
        let sanitized = sanitize_error_message(msg);
        assert!(!sanitized.contains("eyJ"));
        assert!(sanitized.contains("<jwt_token>"));
    }

    #[test]
    fn test_sanitize_error_message_aws_access_key() {
        let msg = "AWS error: AKIAIOSFODNN7EXAMPLE is invalid"; // pragma: allowlist secret
        let sanitized = sanitize_error_message(msg);
        assert!(!sanitized.contains("AKIAIOSFODNN7EXAMPLE"));
        assert!(sanitized.contains("<aws_access_key>"));
    }

    #[test]
    fn test_sanitize_error_message_ip_address() {
        let msg = "Connection refused from 192.168.1.100";
        let sanitized = sanitize_error_message(msg);
        assert!(!sanitized.contains("192.168.1.100"));
        assert!(sanitized.contains("<ip>"));
    }

    #[test]
    fn test_sanitize_error_message_hex_key() {
        let msg = "Key mismatch: abcdef0123456789abcdef0123456789";
        let sanitized = sanitize_error_message(msg);
        assert!(!sanitized.contains("abcdef0123456789abcdef0123456789"));
        assert!(sanitized.contains("<redacted>"));
    }

    #[test]
    fn test_user_message_does_not_leak_sensitive_data() {
        // FileNotFound with sensitive-looking path
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("/home/user/.ssh/id_rsa"),
            source: None,
        };
        let user_msg = err.user_message();
        // Should show filename but not full path
        assert!(!user_msg.contains("/home/user/.ssh/"));
        assert!(user_msg.contains("id_rsa"));
    }

    #[test]
    fn test_debug_message_contains_file_path() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("/home/user/project/config.toml"),
            source: None,
        };
        let debug = err.debug_message();
        // Debug message should contain the full path for diagnostics
        assert!(debug.contains("config.toml") || debug.contains("<path>"));
    }

    #[test]
    fn test_is_sensitive_decryption_error() {
        let err = ConfigError::DecryptionFailed {
            message: "key mismatch".to_string(),
        };
        assert!(err.is_sensitive()); // "key" in message
    }

    #[test]
    fn test_is_sensitive_file_not_found() {
        // Normal file not found should not be sensitive
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("config.toml"),
            source: None,
        };
        assert!(!err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_key_error() {
        let err = ConfigError::KeyError {
            message: "encryption key error".to_string(),
        };
        assert!(err.is_sensitive()); // "key" in message
    }

    #[test]
    fn test_is_sensitive_aws_key_in_message() {
        let err = ConfigError::InvalidValue {
            key: "aws_access_key".to_string(),
            expected_type: "string".to_string(),
            message: "AKIAIOSFODNN7EXAMPLE is invalid".to_string(), // pragma: allowlist secret
        };
        assert!(err.is_sensitive()); // Contains AWS access key
    }

    // =============================================================================
    // Additional coverage for ErrorCode Display impl
    // =============================================================================

    #[test]
    fn test_error_code_display_all_variants() {
        assert_eq!(ErrorCode::FileNotFound.to_string(), "FILE_NOT_FOUND");
        assert_eq!(ErrorCode::FilePermission.to_string(), "FILE_PERMISSION");
        assert_eq!(ErrorCode::FileParseError.to_string(), "FILE_PARSE_ERROR");
        assert_eq!(ErrorCode::IoError.to_string(), "IO_ERROR");
        assert_eq!(ErrorCode::ValidationFailed.to_string(), "VALIDATION_FAILED");
        assert_eq!(ErrorCode::TypeMismatch.to_string(), "TYPE_MISMATCH");
        assert_eq!(ErrorCode::InvalidValue.to_string(), "INVALID_VALUE");
        assert_eq!(
            ErrorCode::SchemaValidationFailed.to_string(),
            "SCHEMA_VALIDATION_FAILED"
        );
        assert_eq!(ErrorCode::DecryptionFailed.to_string(), "DECRYPTION_FAILED");
        assert_eq!(ErrorCode::KeyNotFound.to_string(), "KEY_NOT_FOUND");
        assert_eq!(ErrorCode::KeyTooWeak.to_string(), "KEY_TOO_WEAK");
        assert_eq!(
            ErrorCode::KeyRotationFailed.to_string(),
            "KEY_ROTATION_FAILED"
        );
        assert_eq!(
            ErrorCode::RemoteUnavailable.to_string(),
            "REMOTE_UNAVAILABLE"
        );
        assert_eq!(ErrorCode::RemoteTimeout.to_string(), "REMOTE_TIMEOUT");
        assert_eq!(
            ErrorCode::CircularReference.to_string(),
            "CIRCULAR_REFERENCE"
        );
        assert_eq!(ErrorCode::OverrideBlocked.to_string(), "OVERRIDE_BLOCKED");
        assert_eq!(
            ErrorCode::InterpolationError.to_string(),
            "INTERPOLATION_ERROR"
        );
        assert_eq!(
            ErrorCode::SizeLimitExceeded.to_string(),
            "SIZE_LIMIT_EXCEEDED"
        );
        assert_eq!(ErrorCode::WatcherError.to_string(), "WATCHER_ERROR");
        assert_eq!(ErrorCode::VersionMismatch.to_string(), "VERSION_MISMATCH");
        assert_eq!(ErrorCode::MigrationFailed.to_string(), "MIGRATION_FAILED");
        assert_eq!(ErrorCode::ModuleNotFound.to_string(), "MODULE_NOT_FOUND");
        assert_eq!(
            ErrorCode::ReloadRolledBack.to_string(),
            "RELOAD_ROLLED_BACK"
        );
        assert_eq!(ErrorCode::MultipleSources.to_string(), "MULTIPLE_SOURCES");
        assert_eq!(ErrorCode::Timeout.to_string(), "TIMEOUT");
        assert_eq!(
            ErrorCode::ConcurrencyConflict.to_string(),
            "CONCURRENCY_CONFLICT"
        );
        assert_eq!(ErrorCode::LockPoisoned.to_string(), "LOCK_POISONED");
        assert_eq!(
            ErrorCode::HealthCheckFailed.to_string(),
            "HEALTH_CHECK_FAILED"
        );
        assert_eq!(ErrorCode::Unknown.to_string(), "UNKNOWN");
    }

    // =============================================================================
    // ConfigError::code() mapping for all variants
    // =============================================================================

    #[test]
    fn test_config_error_code_mapping_all_variants() {
        let err = ConfigError::ParseError {
            format: "toml".into(),
            message: "bad".into(),
            location: None,
            source: None,
        };
        assert_eq!(err.code(), ErrorCode::FileParseError);

        let err = ConfigError::ValidationFailed {
            field: "f".into(),
            rule: "r".into(),
            message: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::ValidationFailed);

        let err = ConfigError::SchemaValidationFailed { count: 1 };
        assert_eq!(err.code(), ErrorCode::SchemaValidationFailed);

        let err = ConfigError::DecryptionFailed {
            message: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::DecryptionFailed);

        let err = ConfigError::RemoteUnavailable {
            error_type: "timeout".into(),
            retryable: false,
        };
        assert_eq!(err.code(), ErrorCode::RemoteUnavailable);

        let err = ConfigError::VersionMismatch {
            found: 1,
            expected: 2,
        };
        assert_eq!(err.code(), ErrorCode::VersionMismatch);

        let err = ConfigError::MigrationFailed {
            from: 1,
            to: 2,
            reason: "r".into(),
            source: None,
        };
        assert_eq!(err.code(), ErrorCode::MigrationFailed);

        let err = ConfigError::ModuleNotFound {
            group: "g".into(),
            module: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::ModuleNotFound);

        let err = ConfigError::ReloadRolledBack { reason: "r".into() };
        assert_eq!(err.code(), ErrorCode::ReloadRolledBack);

        let err = ConfigError::InvalidValue {
            key: "k".into(),
            expected_type: "t".into(),
            message: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::InvalidValue);

        let err = ConfigError::SourceChainError {
            message: "m".into(),
            source_index: 0,
        };
        assert_eq!(err.code(), ErrorCode::MultipleSources);

        let err = ConfigError::Timeout { duration_ms: 10 };
        assert_eq!(err.code(), ErrorCode::Timeout);

        let err = ConfigError::SizeLimitExceeded {
            actual: 10,
            limit: 5,
        };
        assert_eq!(err.code(), ErrorCode::SizeLimitExceeded);

        let err = ConfigError::InterpolationError {
            variable: "v".into(),
            message: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::InterpolationError);

        let err = ConfigError::KeyError {
            message: "m".into(),
        };
        assert_eq!(err.code(), ErrorCode::KeyNotFound);

        let err = ConfigError::CircularReference { path: "p".into() };
        assert_eq!(err.code(), ErrorCode::CircularReference);

        let err = ConfigError::LockPoisoned {
            resource: "r".into(),
        };
        assert_eq!(err.code(), ErrorCode::LockPoisoned);

        let err = ConfigError::ConcurrencyConflict {
            key: "k".into(),
            message: "m".into(),
            expected_type: None,
        };
        assert_eq!(err.code(), ErrorCode::ConcurrencyConflict);

        let err = ConfigError::KeyRotationFailed {
            from_version: "v1".into(),
            to_version: "v2".into(),
            reason: "r".into(),
        };
        assert_eq!(err.code(), ErrorCode::KeyRotationFailed);

        let err = ConfigError::WatcherError {
            message: "m".into(),
            path: None,
            recoverable: false,
        };
        assert_eq!(err.code(), ErrorCode::WatcherError);

        let err = ConfigError::OverrideBlocked {
            key: "k".into(),
            reason: "r".into(),
            override_source: None,
        };
        assert_eq!(err.code(), ErrorCode::OverrideBlocked);

        let err = ConfigError::HealthCheckFailed { reason: "r".into() };
        assert_eq!(err.code(), ErrorCode::HealthCheckFailed);

        // MultiSource wraps a MultiSourceError
        let inner = MultiSourceError::new(1, vec![("s", ConfigError::Timeout { duration_ms: 1 })]);
        let err = ConfigError::MultiSource { source: inner };
        assert_eq!(err.code(), ErrorCode::MultipleSources);
    }

    // =============================================================================
    // validation() helper
    // =============================================================================

    #[test]
    fn test_validation_helper_constructs_validation_failed() {
        let err = ConfigError::validation("email", "format", "not a valid email");
        match err {
            ConfigError::ValidationFailed {
                field,
                rule,
                message,
            } => {
                assert_eq!(field, "email");
                assert_eq!(rule, "format");
                assert_eq!(message, "not a valid email");
            }
            other => panic!("expected ValidationFailed, got {:?}", other),
        }
    }

    // =============================================================================
    // is_retryable for IO error kinds, watcher, concurrency, remote
    // =============================================================================

    #[test]
    fn test_is_retryable_io_error_connection_refused() {
        let err = ConfigError::IoError(std::io::Error::new(
            std::io::ErrorKind::ConnectionRefused,
            "refused",
        ));
        assert!(err.is_retryable());
    }

    #[test]
    fn test_is_retryable_io_error_connection_reset() {
        let err = ConfigError::IoError(std::io::Error::new(
            std::io::ErrorKind::ConnectionReset,
            "reset",
        ));
        assert!(err.is_retryable());
    }

    #[test]
    fn test_is_retryable_io_error_timed_out() {
        let err = ConfigError::IoError(std::io::Error::new(
            std::io::ErrorKind::TimedOut,
            "timed out",
        ));
        assert!(err.is_retryable());
    }

    #[test]
    fn test_is_retryable_io_error_not_retryable() {
        let err = ConfigError::IoError(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "not found",
        ));
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_is_retryable_watcher_error_recoverable() {
        let err = ConfigError::WatcherError {
            message: "transient".into(),
            path: None,
            recoverable: true,
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_is_retryable_watcher_error_not_recoverable() {
        let err = ConfigError::WatcherError {
            message: "fatal".into(),
            path: None,
            recoverable: false,
        };
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_is_retryable_concurrency_conflict() {
        let err = ConfigError::ConcurrencyConflict {
            key: "k".into(),
            message: "m".into(),
            expected_type: None,
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_is_retryable_remote_unavailable_not_retryable() {
        let err = ConfigError::RemoteUnavailable {
            error_type: "auth".into(),
            retryable: false,
        };
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_is_retryable_other_variants_not_retryable() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("x"),
            source: None,
        };
        assert!(!err.is_retryable());

        let err = ConfigError::ValidationFailed {
            field: "f".into(),
            rule: "r".into(),
            message: "m".into(),
        };
        assert!(!err.is_retryable());
    }

    // =============================================================================
    // From<std::io::Error> impl
    // =============================================================================

    #[test]
    fn test_from_io_error() {
        let io_err = std::io::Error::other("disk full");
        let config_err: ConfigError = io_err.into();
        assert!(matches!(config_err, ConfigError::IoError(_)));
        assert_eq!(config_err.code(), ErrorCode::IoError);
    }

    // =============================================================================
    // user_message for all variants
    // =============================================================================

    #[test]
    fn test_user_message_parse_error_with_location() {
        let loc = ParseLocation::new("config.toml", 10, 5);
        let err = ConfigError::ParseError {
            format: "toml".into(),
            message: "bad syntax".into(),
            location: Some(loc),
            source: None,
        };
        let msg = err.user_message();
        assert!(msg.contains("toml"));
        assert!(msg.contains("config.toml:10:5"));
        assert!(msg.contains("bad syntax"));
    }

    #[test]
    fn test_user_message_parse_error_without_location() {
        let err = ConfigError::ParseError {
            format: "json".into(),
            message: "bad".into(),
            location: None,
            source: None,
        };
        let msg = err.user_message();
        assert!(msg.contains("json"));
        assert!(msg.contains("bad"));
        assert!(!msg.contains("at"));
    }

    #[test]
    fn test_user_message_validation_failed() {
        let err = ConfigError::ValidationFailed {
            field: "port".into(),
            rule: "range".into(),
            message: "out of range".into(),
        };
        assert_eq!(
            err.user_message(),
            "Field 'port' failed validation: out of range"
        );
    }

    #[test]
    fn test_user_message_schema_validation_failed() {
        let err = ConfigError::SchemaValidationFailed { count: 3 };
        assert_eq!(
            err.user_message(),
            "Schema validation failed with 3 error(s)"
        );
    }

    #[test]
    fn test_user_message_decryption_failed_is_sanitized() {
        let err = ConfigError::DecryptionFailed {
            message: "key mismatch detail".into(),
        };
        // user_message returns a generic sanitized message, not the raw message
        assert_eq!(err.user_message(), "Failed to decrypt configuration value");
    }

    #[test]
    fn test_user_message_remote_unavailable() {
        let err = ConfigError::RemoteUnavailable {
            error_type: "timeout".into(),
            retryable: true,
        };
        assert_eq!(
            err.user_message(),
            "Remote configuration source is unavailable"
        );
    }

    #[test]
    fn test_user_message_migration_failed() {
        let err = ConfigError::MigrationFailed {
            from: 1,
            to: 2,
            reason: "schema change".into(),
            source: None,
        };
        assert_eq!(
            err.user_message(),
            "Migration from v1 to v2 failed: schema change"
        );
    }

    #[test]
    fn test_user_message_module_not_found() {
        let err = ConfigError::ModuleNotFound {
            group: "g1".into(),
            module: "m1".into(),
        };
        assert_eq!(err.user_message(), "Module 'm1' not found in group 'g1'");
    }

    #[test]
    fn test_user_message_reload_rolled_back() {
        let err = ConfigError::ReloadRolledBack {
            reason: "validation".into(),
        };
        assert_eq!(
            err.user_message(),
            "Configuration reload was rolled back: validation"
        );
    }

    #[test]
    fn test_user_message_io_error() {
        let err = ConfigError::IoError(std::io::Error::other("disk full"));
        let msg = err.user_message();
        assert!(msg.contains("IO error"));
        assert!(msg.contains("disk full"));
    }

    #[test]
    fn test_user_message_invalid_value() {
        let err = ConfigError::InvalidValue {
            key: "port".into(),
            expected_type: "u16".into(),
            message: "too large".into(),
        };
        assert_eq!(err.user_message(), "Invalid value for 'port': too large");
    }

    #[test]
    fn test_user_message_source_chain_error() {
        let err = ConfigError::SourceChainError {
            message: "chain broke".into(),
            source_index: 2,
        };
        assert_eq!(err.user_message(), "chain broke");
    }

    #[test]
    fn test_user_message_timeout() {
        let err = ConfigError::Timeout { duration_ms: 5000 };
        assert_eq!(err.user_message(), "Operation timed out after 5000ms");
    }

    #[test]
    fn test_user_message_size_limit_exceeded() {
        let err = ConfigError::SizeLimitExceeded {
            actual: 1024,
            limit: 512,
        };
        assert_eq!(
            err.user_message(),
            "Size limit exceeded: 1024 bytes (limit: 512)"
        );
    }

    #[test]
    fn test_user_message_interpolation_error() {
        let err = ConfigError::InterpolationError {
            variable: "HOME".into(),
            message: "not set".into(),
        };
        assert_eq!(
            err.user_message(),
            "Interpolation error for 'HOME': not set"
        );
    }

    #[test]
    fn test_user_message_key_error_is_sanitized() {
        let err = ConfigError::KeyError {
            message: "detailed key info".into(),
        };
        // user_message returns generic message, not the raw message
        assert_eq!(err.user_message(), "Encryption key error");
    }

    #[test]
    fn test_user_message_circular_reference() {
        let err = ConfigError::CircularReference {
            path: "a.b.c.a".into(),
        };
        assert_eq!(err.user_message(), "Circular reference detected: a.b.c.a");
    }

    #[test]
    fn test_user_message_lock_poisoned() {
        let err = ConfigError::LockPoisoned {
            resource: "config".into(),
        };
        assert_eq!(err.user_message(), "Lock poisoned for resource 'config'");
    }

    #[test]
    fn test_user_message_multi_source() {
        let inner = MultiSourceError::new(3, vec![("s1", ConfigError::Timeout { duration_ms: 1 })]);
        let err = ConfigError::MultiSource { source: inner };
        assert_eq!(err.user_message(), "Multiple sources failed: 1/3");
    }

    #[test]
    fn test_user_message_concurrency_conflict() {
        let err = ConfigError::ConcurrencyConflict {
            key: "k".into(),
            message: "m".into(),
            expected_type: Some("string".into()),
        };
        assert_eq!(err.user_message(), "Concurrency conflict on key 'k': m");
    }

    #[test]
    fn test_user_message_key_rotation_failed() {
        let err = ConfigError::KeyRotationFailed {
            from_version: "v1".into(),
            to_version: "v2".into(),
            reason: "invalid".into(),
        };
        assert_eq!(
            err.user_message(),
            "Key rotation failed from 'v1' to 'v2': invalid"
        );
    }

    #[test]
    fn test_user_message_watcher_error_with_path() {
        let err = ConfigError::WatcherError {
            message: "lost file".into(),
            path: Some(PathBuf::from("/etc/config.toml")),
            recoverable: true,
        };
        let msg = err.user_message();
        assert!(msg.contains("Watcher error"));
        assert!(msg.contains("config.toml"));
        assert!(msg.contains("(recoverable)"));
    }

    #[test]
    fn test_user_message_watcher_error_no_path_not_recoverable() {
        let err = ConfigError::WatcherError {
            message: "fatal error".into(),
            path: None,
            recoverable: false,
        };
        let msg = err.user_message();
        assert!(msg.contains("Watcher error"));
        assert!(!msg.contains("(recoverable)"));
    }

    #[test]
    fn test_user_message_override_blocked_with_source() {
        let err = ConfigError::OverrideBlocked {
            key: "k".into(),
            reason: "protected".into(),
            override_source: Some("cli".into()),
        };
        let msg = err.user_message();
        assert!(msg.contains("Override blocked for key 'k'"));
        assert!(msg.contains("from 'cli'"));
        assert!(msg.contains("protected"));
    }

    #[test]
    fn test_user_message_override_blocked_no_source() {
        let err = ConfigError::OverrideBlocked {
            key: "k".into(),
            reason: "protected".into(),
            override_source: None,
        };
        let msg = err.user_message();
        assert!(msg.contains("Override blocked for key 'k'"));
        assert!(!msg.contains("from '"));
    }

    #[test]
    fn test_user_message_health_check_failed() {
        let err = ConfigError::HealthCheckFailed {
            reason: "db down".into(),
        };
        assert_eq!(err.user_message(), "Health check failed: db down");
    }

    #[test]
    fn test_user_message_file_not_found_normal_path() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("config.toml"),
            source: None,
        };
        // Normal paths should be shown as-is (no sanitization)
        assert_eq!(
            err.user_message(),
            "Configuration file 'config.toml' not found"
        );
    }

    #[test]
    fn test_user_message_file_not_found_aws_path_sanitized() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("/home/user/.aws/credentials"),
            source: None,
        };
        let msg = err.user_message();
        assert!(!msg.contains("/home/user/.aws/"));
        assert!(msg.contains("credentials"));
    }

    #[test]
    fn test_user_message_file_not_found_kube_path_sanitized() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("/root/.kube/config"),
            source: None,
        };
        let msg = err.user_message();
        assert!(!msg.contains("/root/.kube/"));
        assert!(msg.contains("config"));
    }

    #[test]
    fn test_user_message_file_not_found_env_path_sanitized() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("/app/.env"),
            source: None,
        };
        let msg = err.user_message();
        assert!(!msg.contains("/app/"));
        assert!(msg.contains(".env") || msg.contains("env"));
    }

    // =============================================================================
    // debug_message with URL credentials
    // =============================================================================

    #[test]
    fn test_debug_message_redacts_url_credentials() {
        let err = ConfigError::DecryptionFailed {
            message: "fetch https://user:passw0rd123@example.com/keys failed".into(), // pragma: allowlist secret
        };
        let dbg = err.debug_message();
        assert!(!dbg.contains("user:passw0rd123"));
        assert!(dbg.contains("<creds>") || dbg.contains("<host>"));
    }

    // =============================================================================
    // is_sensitive for sensitive patterns
    // =============================================================================

    #[test]
    fn test_is_sensitive_url_with_credentials() {
        let err = ConfigError::DecryptionFailed {
            message: "fetch https://user:passw0rd123@example.com/keys failed".into(), // pragma: allowlist secret
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_jwt_token() {
        let err = ConfigError::DecryptionFailed {
            message: "token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload.sig invalid".into(), // pragma: allowlist secret
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_password_field() {
        let err = ConfigError::InvalidValue {
            key: "db.password".into(),
            expected_type: "string".into(),
            message: "too short".into(),
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_token_field() {
        let err = ConfigError::InvalidValue {
            key: "auth.token".into(),
            expected_type: "string".into(),
            message: "expired".into(),
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_api_key_field() {
        let err = ConfigError::InvalidValue {
            key: "service.api_key".into(),
            expected_type: "string".into(),
            message: "missing".into(),
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_credential_field() {
        let err = ConfigError::InvalidValue {
            key: "credential".into(),
            expected_type: "string".into(),
            message: "invalid".into(),
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_secret_field() {
        let err = ConfigError::InvalidValue {
            key: "client_secret".into(),
            expected_type: "string".into(),
            message: "missing".into(),
        };
        assert!(err.is_sensitive());
    }

    #[test]
    fn test_is_sensitive_clean_error_returns_false() {
        let err = ConfigError::Timeout { duration_ms: 100 };
        assert!(!err.is_sensitive());

        let err = ConfigError::VersionMismatch {
            found: 1,
            expected: 2,
        };
        assert!(!err.is_sensitive());
    }

    // =============================================================================
    // sanitized_chain with source errors
    // =============================================================================

    #[test]
    fn test_sanitized_chain_parse_error_with_source() {
        let source: Box<dyn std::error::Error + Send + Sync> =
            Box::new(std::io::Error::other("inner cause"));
        let err = ConfigError::ParseError {
            format: "toml".into(),
            message: "outer".into(),
            location: None,
            source: Some(source),
        };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 2);
        // First entry is the user_message (sanitized)
        assert!(chain[0].contains("toml"));
        // Second entry is the sanitized source message
        assert!(chain[1].contains("inner cause"));
    }

    #[test]
    fn test_sanitized_chain_parse_error_no_source() {
        let err = ConfigError::ParseError {
            format: "toml".into(),
            message: "outer".into(),
            location: None,
            source: None,
        };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_sanitized_chain_migration_failed_with_source() {
        let source: Box<dyn std::error::Error + Send + Sync> =
            Box::new(std::io::Error::other("migration cause"));
        let err = ConfigError::MigrationFailed {
            from: 1,
            to: 2,
            reason: "outer".into(),
            source: Some(source),
        };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 2);
        assert!(chain[1].contains("migration cause"));
    }

    #[test]
    fn test_sanitized_chain_other_variants_no_source() {
        // Variants without source only have a single entry
        let err = ConfigError::Timeout { duration_ms: 1 };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 1);

        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("x"),
            source: None,
        };
        let chain = err.sanitized_chain();
        assert_eq!(chain.len(), 1);
    }

    // =============================================================================
    // MultiSourceError additional methods
    // =============================================================================

    #[test]
    fn test_multi_source_error_with_partial() {
        let partial = serde_json::json!({ "host": "localhost" });
        let err = MultiSourceError::with_partial(
            4,
            vec![(
                "source_a".to_string(),
                ConfigError::Timeout { duration_ms: 100 },
            )],
            partial,
        );
        assert_eq!(err.failed_count, 1);
        assert_eq!(err.total_count, 4);
        assert!(err.has_partial_success());
        let partial = err.partial_config().expect("partial config present");
        assert_eq!(partial["host"], "localhost");
    }

    #[test]
    fn test_multi_source_error_no_partial_success() {
        let err = MultiSourceError::new(2, vec![("s", ConfigError::Timeout { duration_ms: 1 })]);
        assert!(!err.has_partial_success());
        assert!(err.partial_config().is_none());
    }

    #[test]
    fn test_multi_source_error_errors_accessor() {
        let err = MultiSourceError::new(
            3,
            vec![
                ("s1".to_string(), ConfigError::Timeout { duration_ms: 1 }),
                (
                    "s2".to_string(),
                    ConfigError::RemoteUnavailable {
                        error_type: "conn".into(),
                        retryable: false,
                    },
                ),
            ],
        );
        let errors = err.errors();
        assert_eq!(errors.len(), 2);
        assert_eq!(errors[0].0, "s1");
        assert_eq!(errors[1].0, "s2");
    }

    #[test]
    fn test_multi_source_error_count_error_type() {
        let err = MultiSourceError::new(
            4,
            vec![
                ("s1".to_string(), ConfigError::Timeout { duration_ms: 1 }),
                ("s2".to_string(), ConfigError::Timeout { duration_ms: 2 }),
                (
                    "s3".to_string(),
                    ConfigError::RemoteUnavailable {
                        error_type: "conn".into(),
                        retryable: false,
                    },
                ),
            ],
        );
        assert_eq!(err.count_error_type(ErrorCode::Timeout), 2);
        assert_eq!(err.count_error_type(ErrorCode::RemoteUnavailable), 1);
        assert_eq!(err.count_error_type(ErrorCode::FileNotFound), 0);
    }

    // =============================================================================
    // BuildResult additional methods
    // =============================================================================

    #[test]
    fn test_build_result_with_warnings() {
        let warnings = vec![SourceWarning {
            message: "deprecated".into(),
            source: Some("file.toml".into()),
            code: WarningCode::DeprecatedKey,
        }];
        let result: BuildResult<i32> = BuildResult::with_warnings(42, warnings);
        assert!(!result.degraded);
        assert!(result.has_warnings());
        assert_eq!(result.warnings.len(), 1);
        assert_eq!(result.warnings[0].code, WarningCode::DeprecatedKey);
    }

    #[test]
    fn test_build_result_ok_has_no_warnings() {
        let result: BuildResult<i32> = BuildResult::ok(0);
        assert!(!result.has_warnings());
        assert!(result.warnings.is_empty());
    }

    #[test]
    fn test_build_result_degraded_has_no_warnings() {
        let result: BuildResult<i32> = BuildResult::degraded(0, "reason");
        assert!(result.degraded);
        assert!(!result.has_warnings());
        assert_eq!(result.degraded_reason, Some("reason".into()));
    }

    #[test]
    fn test_build_result_map_preserves_metadata() {
        let warnings = vec![SourceWarning {
            message: "w".into(),
            source: None,
            code: WarningCode::DefaultUsed,
        }];
        let result: BuildResult<i32> = BuildResult::with_warnings(42, warnings);
        let mapped: BuildResult<String> = result.map(|n| format!("val={}", n));
        assert_eq!(mapped.config, "val=42");
        assert_eq!(mapped.warnings.len(), 1);
        assert!(!mapped.degraded);
    }

    // =============================================================================
    // WarningCode Display for all variants
    // =============================================================================

    #[test]
    fn test_warning_code_display_all_variants() {
        assert_eq!(
            WarningCode::OptionalSourceSkipped.to_string(),
            "OPTIONAL_SOURCE_SKIPPED"
        );
        assert_eq!(WarningCode::DeprecatedKey.to_string(), "DEPRECATED_KEY");
        assert_eq!(WarningCode::DefaultUsed.to_string(), "DEFAULT_USED");
        assert_eq!(WarningCode::ValueTruncated.to_string(), "VALUE_TRUNCATED");
        assert_eq!(WarningCode::RemoteFallback.to_string(), "REMOTE_FALLBACK");
        assert_eq!(
            WarningCode::UnencryptedSensitive.to_string(),
            "UNENCRYPTED_SENSITIVE"
        );
        assert_eq!(WarningCode::UnusedKey.to_string(), "UNUSED_KEY");
    }

    // =============================================================================
    // ConfersError / ConfersResult type aliases
    // =============================================================================

    #[test]
    fn test_confers_error_type_alias_matches_config_error() {
        let err: ConfersError = ConfigError::Timeout { duration_ms: 1 };
        // ConfersError is a type alias for ConfigError, so it should be usable
        assert_eq!(err.code(), ErrorCode::Timeout);
        // Verify it can be used where ConfigError is expected
        let _: &ConfigError = &err;
    }

    #[test]
    fn test_confers_result_type_alias() {
        // T-C-1 C1: old test asserted `Ok(42).is_ok()` which is tautological.
        // Now verify the type alias participates in error code mapping by
        // destructuring with match (avoids clippy unnecessary_literal_unwrap).
        let ok: ConfersResult<i32> = Ok(42);
        match ok {
            Ok(v) => assert_eq!(v, 42),
            Err(e) => panic!("expected Ok(42), got Err: {e:?}"),
        }

        let err: ConfersResult<i32> = Err(ConfigError::Timeout { duration_ms: 100 });
        match err {
            Ok(v) => panic!("expected Err, got Ok({v})"),
            Err(e) => assert_eq!(e.code(), ErrorCode::Timeout),
        }
    }

    #[test]
    fn test_config_result_type_alias() {
        // T-C-1 C2: old test asserted `Ok(0).is_ok()` which is tautological.
        // Now verify the type alias propagates error codes correctly.
        let ok: ConfigResult<i32> = Ok(0);
        match ok {
            Ok(v) => assert_eq!(v, 0),
            Err(e) => panic!("expected Ok(0), got Err: {e:?}"),
        }

        let err: ConfigResult<i32> = Err(ConfigError::FileNotFound {
            filename: PathBuf::from("missing"),
            source: None,
        });
        match err {
            Ok(v) => panic!("expected Err, got Ok({v})"),
            Err(e) => assert_eq!(e.code(), ErrorCode::FileNotFound),
        }
    }

    // =============================================================================
    // Display impl for ConfigError (exercises thiserror #[error(...)] formats)
    // =============================================================================

    #[test]
    fn test_config_error_display_file_not_found() {
        let err = ConfigError::FileNotFound {
            filename: PathBuf::from("config.toml"),
            source: None,
        };
        let s = format!("{}", err);
        assert!(s.contains("config.toml"));
        assert!(s.contains("not found"));
    }

    #[test]
    fn test_config_error_display_validation_failed() {
        let err = ConfigError::ValidationFailed {
            field: "port".into(),
            rule: "range".into(),
            message: "too big".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("port"));
        assert!(s.contains("range"));
        assert!(s.contains("too big"));
    }

    #[test]
    fn test_config_error_display_migration_failed() {
        let err = ConfigError::MigrationFailed {
            from: 1,
            to: 2,
            reason: "schema".into(),
            source: None,
        };
        let s = format!("{}", err);
        assert!(s.contains("v1"));
        assert!(s.contains("v2"));
        assert!(s.contains("schema"));
    }

    #[test]
    fn test_config_error_display_module_not_found() {
        let err = ConfigError::ModuleNotFound {
            group: "g".into(),
            module: "m".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("'m'"));
        assert!(s.contains("'g'"));
    }

    #[test]
    fn test_config_error_display_size_limit_exceeded() {
        let err = ConfigError::SizeLimitExceeded {
            actual: 100,
            limit: 50,
        };
        let s = format!("{}", err);
        assert!(s.contains("100"));
        assert!(s.contains("50"));
    }

    #[test]
    fn test_config_error_display_concurrency_conflict_with_expected_type() {
        let err = ConfigError::ConcurrencyConflict {
            key: "k".into(),
            message: "m".into(),
            expected_type: Some("string".into()),
        };
        let s = format!("{}", err);
        assert!(s.contains("k"));
        assert!(s.contains("m"));
    }

    #[test]
    fn test_config_error_display_key_rotation_failed() {
        let err = ConfigError::KeyRotationFailed {
            from_version: "v1".into(),
            to_version: "v2".into(),
            reason: "bad".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("v1"));
        assert!(s.contains("v2"));
        assert!(s.contains("bad"));
    }

    #[test]
    fn test_config_error_display_watcher_error() {
        let err = ConfigError::WatcherError {
            message: "lost".into(),
            path: Some(PathBuf::from("/x/y.toml")),
            recoverable: false,
        };
        let s = format!("{}", err);
        assert!(s.contains("lost"));
    }

    #[test]
    fn test_config_error_display_override_blocked() {
        let err = ConfigError::OverrideBlocked {
            key: "k".into(),
            reason: "r".into(),
            override_source: Some("cli".into()),
        };
        let s = format!("{}", err);
        assert!(s.contains("k"));
        assert!(s.contains("r"));
    }

    #[test]
    fn test_config_error_display_health_check_failed() {
        let err = ConfigError::HealthCheckFailed {
            reason: "down".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("down"));
    }

    #[test]
    fn test_config_error_display_multi_source() {
        let inner = MultiSourceError::new(2, vec![("s", ConfigError::Timeout { duration_ms: 1 })]);
        let err = ConfigError::MultiSource { source: inner };
        let s = format!("{}", err);
        assert!(s.contains("Multiple sources failed"));
    }

    #[test]
    fn test_config_error_display_lock_poisoned() {
        let err = ConfigError::LockPoisoned {
            resource: "config".into(),
        };
        let s = format!("{}", err);
        assert!(s.contains("config"));
        assert!(s.contains("Lock poisoned"));
    }

    // =============================================================================
    // Debug derive for ConfigError and MultiSourceError
    // =============================================================================

    #[test]
    fn test_config_error_debug_format() {
        let err = ConfigError::Timeout { duration_ms: 42 };
        let dbg = format!("{:?}", err);
        assert!(dbg.contains("Timeout"));
        assert!(dbg.contains("42"));
    }

    #[test]
    fn test_multi_source_error_debug_format() {
        let err = MultiSourceError::new(2, vec![("s", ConfigError::Timeout { duration_ms: 1 })]);
        let dbg = format!("{:?}", err);
        assert!(dbg.contains("MultiSourceError"));
        assert!(dbg.contains("failed_count"));
    }

    // =============================================================================
    // sanitize_error_message: AWS secret access key (40-char)
    // =============================================================================

    #[test]
    fn test_sanitize_error_message_aws_secret_key() {
        // Exactly 40 chars of [A-Za-z0-9/+=] surrounded by spaces
        let msg = " secret: abcdefghijklmnopqrstuvwxyz0123456789ABCD "; // pragma: allowlist secret
        let sanitized = sanitize_error_message(msg);
        assert!(
            sanitized.contains("<aws_secret_key>"),
            "expected AWS secret key to be redacted, got: {}",
            sanitized
        );
    }

    // =============================================================================
    // audit_message for additional variants
    // =============================================================================

    #[test]
    fn test_audit_message_timeout() {
        let err = ConfigError::Timeout { duration_ms: 100 };
        let audit = err.audit_message();
        assert!(audit.contains("error_code=900"));
        assert!(audit.contains("TIMEOUT"));
        assert!(audit.contains("operation=config"));
    }

    #[test]
    fn test_audit_message_concurrency_conflict() {
        let err = ConfigError::ConcurrencyConflict {
            key: "k".into(),
            message: "m".into(),
            expected_type: None,
        };
        let audit = err.audit_message();
        assert!(audit.contains("error_code=901"));
        assert!(audit.contains("CONCURRENCY_CONFLICT"));
    }
}