conferencier 0.1.0

Minimal, typed TOML configuration for Tokio applications
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
use std::fmt;
use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockError};

use tokio::fs;
use toml::value::Datetime;
use toml::{Table, Value};

use crate::error::{Error, IoOperation, Result};
use crate::value_conversion;

/// Shared handle to an in-memory TOML configuration document.
///
/// # Handle semantics
///
/// Clones share the same document; they do not copy it. A value set through one clone is
/// visible through all of them, and [`load_str`](Self::load_str) or
/// [`load_file`](Self::load_file) replaces the document for all of them. Share the store
/// between tasks with `store.clone()`; [`new`](Self::new) and [`Default`] create a new,
/// empty store.
///
/// # Synchronous and asynchronous methods
///
/// Only file I/O is async: [`from_file`](Self::from_file), [`load_file`](Self::load_file)
/// and [`save_file`](Self::save_file) run their file operations on Tokio's blocking thread
/// pool. Every other method works only on
/// the in-memory document and is synchronous, so it can also be called before the runtime
/// starts or from synchronous code on a runtime thread. Conferencier does not start a
/// Tokio runtime or choose its flavor; applications enable the Tokio features they need.
/// The file methods work on any Tokio runtime, including a current-thread runtime.
///
/// # Absence of sections and keys
///
/// - Peeks report absence in their type and treat a non-table top-level entry as absent:
///   [`get_value`](Self::get_value) and [`get_section`](Self::get_section) return
///   [`Option`], [`has_section`](Self::has_section) returns `bool`, and
///   [`list_sections`](Self::list_sections) lists only table entries.
/// - Typed getters such as [`get_integer`](Self::get_integer) require the key and fail with
///   [`Error::MissingKey`] or [`Error::TypeMismatch`].
/// - Operations that write into, replace or enumerate a section require the entry named
///   `section`, if present, to be a table and fail with [`Error::TypeMismatch`] otherwise:
///   [`set_value`](Self::set_value), the typed setters, [`set_section`](Self::set_section),
///   [`ensure_section`](Self::ensure_section), [`remove_key`](Self::remove_key) and
///   [`list_keys`](Self::list_keys).
///
/// `section` is always a literal top-level key, not a dotted path.
/// `key` is literal too: use [`get_section`](Self::get_section) to inspect a nested
/// table and [`set_section`](Self::set_section) to replace its owning section.
///
/// # Example
///
/// ```
/// use conferencier::ConfigStore;
///
/// let store = ConfigStore::from_str("[App]\nport = 8080")?;
/// let shared = store.clone();
/// shared.set_integer("App", "port", 9090)?;
/// assert_eq!(store.get_integer("App", "port")?, 9090);
///
/// let mut snapshot = store.get_section("App").expect("App is a table");
/// snapshot.insert("enabled".into(), conferencier::toml::Value::Boolean(true));
/// assert!(store.get_value("App", "enabled").is_none());
/// store.set_section("App", snapshot)?;
/// assert!(store.get_boolean("App", "enabled")?);
/// # Ok::<(), conferencier::Error>(())
/// ```
///
/// # Locking
///
/// The document is guarded by a [`std::sync::RwLock`]. The lock is held only for the
/// operation on the in-memory document: never across an `.await`, never during file I/O
/// and never while user code runs, and the store hands out no guards. Calling the store
/// again, also from another task, therefore cannot deadlock on it. A contended acquisition
/// blocks the worker thread, and the lock gives no reader/writer priority guarantee. If a
/// panic ever poisons the lock, later calls continue with the data, which every write
/// leaves consistent.
///
/// Typed setters finish all input conversions and iterator consumption and build the
/// complete TOML value before acquiring the write lock. They then validate the section
/// and replace the value in one update. Input preparation therefore runs even when the
/// section is invalid, but caller-provided code never runs under the lock and readers
/// never observe a partially built array.
///
/// Individual calls are synchronized, not sequences of calls: a read followed by
/// a write is not a transaction, and another task may change the document between them.
/// The [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#locking)
/// explains the design assumptions behind this lock.
///
/// # Debug output
///
/// The `Debug` implementation lists top-level key names only and never displays
/// configuration values. If the lock is unavailable, it reports that the keys could not
/// be read rather than waiting. Key names may themselves be sensitive, and the output
/// format is not stable or intended for parsing.
///
/// # Nesting depth
///
/// Parsing uses `toml`'s default recursion limit. Cargo features are additive, so
/// another dependency can enable `toml/unbounded` and remove that limit. Deeply
/// nested input without the limit can overflow the stack during parsing. Values
/// built in code are not limited: [`save_str`](Self::save_str) and
/// [`save_file`](Self::save_file) can succeed even if the resulting document cannot
/// be loaded again because it exceeds the parser's limit. Very deeply nested values
/// can overflow the stack when serialized, cloned or dropped. A stack overflow aborts
/// the process. See the
/// [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#nesting-limits).
#[derive(Clone, Default)]
pub struct ConfigStore {
    table: Arc<RwLock<Table>>,
}

impl fmt::Debug for ConfigStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let keys = match self.table.try_read() {
            Ok(table) => Some(table.keys().cloned().collect::<Vec<_>>()),
            Err(TryLockError::WouldBlock) => None,
            Err(TryLockError::Poisoned(error)) => {
                Some(error.into_inner().keys().cloned().collect::<Vec<_>>())
            }
        };

        let mut debug_struct = f.debug_struct("ConfigStore");
        match keys {
            Some(keys) => {
                debug_struct.field("keys", &keys);
            }
            None => {
                debug_struct.field("keys", &"<unavailable: locked>");
            }
        }
        debug_struct.finish_non_exhaustive()
    }
}

impl ConfigStore {
    /// Creates an empty configuration store.
    ///
    /// Equivalent to [`Default::default`]. Each call creates an independent document;
    /// use [`Clone::clone`] to share an existing one.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builds a store from a TOML string.
    ///
    /// `source.parse::<ConfigStore>()` does the same through [`FromStr`].
    /// This creates a new document without reading a file or requiring a Tokio runtime.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Parse`] when `source` is not valid TOML.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(source: &str) -> Result<Self> {
        Ok(Self::from_table(Self::parse_table(source)?))
    }

    /// Reads a TOML file asynchronously and builds a store from it.
    ///
    /// A failed read returns [`Error::Io`] with [`IoOperation::Read`] and `path`.
    /// This uses [`tokio::fs`] and must run within a Tokio runtime. There is no
    /// blocking variant; the [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#reading-a-file-before-the-runtime-starts)
    /// shows how to read a file before the runtime starts.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the file cannot be read as UTF-8, or [`Error::Parse`]
    /// if its contents are not valid TOML. Parse errors carry parser diagnostics
    /// and the file path.
    ///
    /// # Panics
    ///
    /// Panics if this future is polled without an active Tokio runtime when it reaches
    /// the Tokio file read.
    pub async fn from_file(path: impl AsRef<Path> + Send + Sync) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        let contents = read_file(&path).await?;
        Self::from_str(&contents).map_err(|error| error.with_parse_path(&path))
    }

    /// Replaces the in-memory document with the contents of the provided TOML string.
    ///
    /// This replaces all sections and top-level values, not just the keys in `source`.
    /// Every clone of this handle observes the replacement; existing scopes do not
    /// reload automatically.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Parse`] for invalid TOML, leaving the whole document unchanged.
    pub fn load_str(&self, source: &str) -> Result<()> {
        let table = Self::parse_table(source)?;
        *self.write() = table;
        Ok(())
    }

    /// Replaces the in-memory document with the contents of the TOML file at `path`.
    ///
    /// The file is read and parsed before the document is replaced; on error the document
    /// stays unchanged. All clones see the replacement, but existing scopes must be
    /// reloaded separately with [`ConfigScope::load`](crate::ConfigScope::load).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] with [`IoOperation::Read`] and `path` if reading
    /// fails, or [`Error::Parse`] with `path` for invalid TOML.
    ///
    /// # Panics
    ///
    /// Panics if this future is polled without an active Tokio runtime when it reaches
    /// the Tokio file read.
    pub async fn load_file(&self, path: impl AsRef<Path> + Send + Sync) -> Result<()> {
        let path = path.as_ref().to_path_buf();
        let contents = read_file(&path).await?;
        self.load_str(&contents)
            .map_err(|error| error.with_parse_path(&path))
    }

    /// Serializes the current document to a TOML string.
    ///
    /// This captures one consistent document while holding a read lock. It does not
    /// modify the store or include unsaved changes in a scope. Comments, formatting,
    /// key order and original value notation are not preserved.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Serialize`] if the in-memory values cannot be encoded as TOML.
    /// The error's `target_path` is `None`.
    pub fn save_str(&self) -> Result<String> {
        toml::to_string(&*self.read()).map_err(Error::from)
    }

    /// Writes a serialized snapshot of the document to the file at `path`.
    ///
    /// The document is serialized first; the store lock is not held during file I/O.
    /// Changes made after serialization are not part of this snapshot. The snapshot is
    /// written to a temporary file next to the target, which then replaces the target
    /// with a single rename, so comments and formatting are lost as with
    /// [`save_str`](Self::save_str). Parent directories are not created.
    ///
    /// Concurrent calls to the same target, including calls through cloned handles,
    /// each install one complete snapshot; which call wins is unspecified. Replacement
    /// is atomic only where the filesystem provides atomic rename, a failed rename is
    /// not retried, and crash durability is not promised.
    ///
    /// Dropping the returned future does not cancel a save whose file operations have
    /// started: it can still replace the target. A relative `path` depends on the
    /// current directory until those operations finish.
    ///
    /// Only a missing target or a regular file is accepted: a final-component symlink,
    /// a directory, another non-regular file or a Windows reparse point is rejected,
    /// while links in parent directories are allowed. Names beginning with
    /// `.conferencier.` are reserved for temporary files and must not be used as
    /// targets. On Unix, an existing target's `rwx` mode bits are preserved;
    /// ownership, ACLs, Windows access settings and other metadata are not.
    ///
    /// The [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/files/index.html)
    /// describes the replacement, cancellation, temporary files with all reserved
    /// names, accepted targets and preserved permissions in detail, including how to
    /// create a private configuration file.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Serialize`] with `target_path` before any file is written
    /// if serialization fails. Unsupported targets return [`Error::Io`] with
    /// [`IoOperation::InspectTarget`], [`ErrorKind::InvalidInput`], and the target
    /// path before creating a temporary file. Other file-operation errors carry
    /// their [`IoOperation`], the relevant file in `path`, and the save destination
    /// in `target_path`. If the blocking task cannot be joined, returns
    /// [`Error::SaveFileTaskJoin`]. If removing a temporary file also fails,
    /// returns [`Error::SaveFileCleanup`] with the target path, original error,
    /// temporary path and cleanup error. An I/O failure does not change the
    /// in-memory store.
    ///
    /// # Panics
    ///
    /// Panics if serialization succeeds and this future is polled without an active Tokio
    /// runtime when it starts the blocking file operation. A serialization error is
    /// returned before a runtime is needed.
    pub async fn save_file(&self, path: impl AsRef<Path> + Send + Sync) -> Result<()> {
        let path = path.as_ref();
        let serialized = self
            .save_str()
            .map_err(|error| error.with_save_target(path))?;
        write_atomic(path, serialized, write_atomic_blocking).await
    }

    /// Returns an owned snapshot of the raw TOML value at `section` and `key`.
    ///
    /// Both arguments name literal keys, not dotted paths. A missing section, a
    /// non-table section or a missing key returns `None`. Changing the returned
    /// value does not change the store; use [`set_value`](Self::set_value) to write it.
    pub fn get_value(&self, section: &str, key: &str) -> Option<Value> {
        section_table(&self.read(), section)
            .and_then(|table| table.get(key))
            .cloned()
    }

    /// Returns a cloned snapshot of the table stored at `section`, if it exists.
    ///
    /// Missing or non-table top-level entries return `None`. The snapshot includes
    /// nested tables and unknown keys; edits to it do not affect the store until
    /// passed to [`set_section`](Self::set_section).
    pub fn get_section(&self, section: &str) -> Option<Table> {
        section_table(&self.read(), section).cloned()
    }

    /// Returns a snapshot of `section`: empty when absent, an error when not a table.
    pub(crate) fn read_section(&self, section: &str) -> Result<Table> {
        match self.read().get(section) {
            Some(Value::Table(table)) => Ok(table.clone()),
            Some(other) => Err(section_mismatch(section, other)),
            None => Ok(Table::new()),
        }
    }

    /// Atomically replaces the complete table at `section`, creating it if absent.
    ///
    /// Unknown keys in the old section are removed. If the section holds a
    /// non-table value, returns a type mismatch and leaves the store unchanged.
    /// Other sections are not changed. `section` is a literal top-level key.
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] with an empty `table_path` and the section name
    /// as `key` when the entry is not a table.
    pub fn set_section(&self, section: &str, table: Table) -> Result<()> {
        let mut guard = self.write();
        match guard.entry(section.to_owned()) {
            toml::map::Entry::Occupied(mut entry) => match entry.get_mut() {
                Value::Table(inner) => {
                    *inner = table;
                    Ok(())
                }
                other => Err(section_mismatch(section, other)),
            },
            toml::map::Entry::Vacant(entry) => {
                entry.insert(Value::Table(table));
                Ok(())
            }
        }
    }

    /// Inserts `value` at `section.key`, creating the section if necessary.
    ///
    /// Both arguments name literal keys. An existing value at the key is replaced,
    /// regardless of its type; all other keys are left unchanged. Tables and arrays
    /// are accepted as raw values too.
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not
    /// a table, leaving the document unchanged.
    pub fn set_value(&self, section: &str, key: &str, value: Value) -> Result<()> {
        let mut guard = self.write();
        match guard.entry(section.to_owned()) {
            toml::map::Entry::Occupied(mut entry) => match entry.get_mut() {
                Value::Table(inner) => {
                    inner.insert(key.to_owned(), value);
                    Ok(())
                }
                other => Err(section_mismatch(section, other)),
            },
            toml::map::Entry::Vacant(entry) => {
                let mut table = Table::new();
                table.insert(key.to_owned(), value);
                entry.insert(Value::Table(table));
                Ok(())
            }
        }
    }

    /// Returns `true` when the store contains a table for `section`.
    ///
    /// An empty table counts as present. Missing entries and entries of any other
    /// TOML type return `false`.
    pub fn has_section(&self, section: &str) -> bool {
        matches!(self.read().get(section), Some(Value::Table(_)))
    }

    /// Creates `section` as an empty table if it is missing.
    ///
    /// An existing table is left unchanged, so the call is idempotent; unlike
    /// [`set_section`](Self::set_section), it never replaces a section. A non-table entry
    /// named `section` returns a type mismatch.
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] for a non-table entry without modifying it.
    pub fn ensure_section(&self, section: &str) -> Result<()> {
        let mut guard = self.write();
        match guard.entry(section.to_owned()) {
            toml::map::Entry::Occupied(entry) => match entry.get() {
                Value::Table(_) => Ok(()),
                other => Err(section_mismatch(section, other)),
            },
            toml::map::Entry::Vacant(entry) => {
                entry.insert(Value::Table(Table::new()));
                Ok(())
            }
        }
    }

    /// Removes `key` from `section`, ignoring missing keys or sections.
    ///
    /// Removing the last key leaves an empty section in place. To remove the
    /// top-level entry as well, use [`remove_section`](Self::remove_section).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if `section` exists but is not a table.
    pub fn remove_key(&self, section: &str, key: &str) -> Result<()> {
        match self.write().get_mut(section) {
            Some(Value::Table(inner)) => {
                inner.remove(key);
                Ok(())
            }
            Some(other) => Err(section_mismatch(section, other)),
            None => Ok(()),
        }
    }

    /// Removes the top-level entry `section` from the store, ignoring missing sections.
    ///
    /// Removes the whole entry, including nested tables, even if it is not a table.
    /// Unlike [`remove_key`](Self::remove_key), this operation has no error result.
    pub fn remove_section(&self, section: &str) {
        self.write().remove(section);
    }

    /// Lists all sections currently backed by a TOML table.
    ///
    /// Empty tables are included; top-level scalar and array entries are not.
    /// The returned names are owned snapshots, with no promised ordering.
    pub fn list_sections(&self) -> Vec<String> {
        self.read()
            .iter()
            .filter_map(|(name, value)| value.as_table().map(|_| name.clone()))
            .collect()
    }

    /// Lists the keys contained in `section`, or an empty vector when the section is absent.
    ///
    /// Lists immediate keys only, including those holding tables or arrays; it does
    /// not walk nested tables. The returned names have no promised ordering.
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if `section` exists but is not a table.
    pub fn list_keys(&self, section: &str) -> Result<Vec<String>> {
        match self.read().get(section) {
            Some(Value::Table(table)) => Ok(table.keys().cloned().collect()),
            Some(other) => Err(section_mismatch(section, other)),
            None => Ok(Vec::new()),
        }
    }

    /// Retrieves a string value stored at `section.key`.
    ///
    /// Returns an owned string. Numbers, booleans and datetimes are not coerced to strings.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for a missing section or key, or
    /// [`Error::TypeMismatch`] for a non-table section or a non-string value.
    pub fn get_string(&self, section: &str, key: &str) -> Result<String> {
        let value = self.fetch_value(section, key)?;
        value_conversion::string(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves an integer value stored at `section.key`.
    ///
    /// TOML integers are signed 64-bit values. Floats and numeric strings are not converted.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for a missing section or key, or
    /// [`Error::TypeMismatch`] for a non-table section or a non-integer value.
    pub fn get_integer(&self, section: &str, key: &str) -> Result<i64> {
        let value = self.fetch_value(section, key)?;
        value_conversion::integer(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a floating-point value stored at `section.key`.
    ///
    /// Accepts TOML floats and integers. Integer-to-`f64` conversion may lose precision
    /// for large integers; numeric strings are not parsed.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for a missing section or key, or
    /// [`Error::TypeMismatch`] for a non-table section or an incompatible value.
    pub fn get_float(&self, section: &str, key: &str) -> Result<f64> {
        let value = self.fetch_value(section, key)?;
        value_conversion::float(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a boolean value stored at `section.key`.
    ///
    /// Accepts only a TOML boolean, not integers or strings such as `"true"`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for a missing section or key, or
    /// [`Error::TypeMismatch`] for a non-table section or a non-boolean value.
    pub fn get_boolean(&self, section: &str, key: &str) -> Result<bool> {
        let value = self.fetch_value(section, key)?;
        value_conversion::boolean(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a [`Datetime`] value stored at `section.key`, parsing strings when necessary.
    ///
    /// Accepts TOML date, time and datetime values, or strings in a form understood
    /// by [`Datetime`]. It does not supply a timezone or normalize omitted components.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence, [`Error::TypeMismatch`] for a
    /// non-table section or an incompatible type, or [`Error::ValueParse`] for
    /// a string that cannot be parsed as a TOML datetime.
    pub fn get_datetime(&self, section: &str, key: &str) -> Result<Datetime> {
        let value = self.fetch_value(section, key)?;
        value_conversion::datetime(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a string array stored at `section.key`.
    ///
    /// Returns an owned vector; an empty TOML array produces an empty vector.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence or [`Error::TypeMismatch`] if the
    /// section is not a table or the value is not an array. A non-string element
    /// produces [`Error::ValueParse`] naming its zero-based index.
    pub fn get_string_vec(&self, section: &str, key: &str) -> Result<Vec<String>> {
        let value = self.fetch_value(section, key)?;
        value_conversion::string_vec(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves an integer array stored at `section.key`.
    ///
    /// Every element must be a TOML integer; floats and numeric strings are not converted.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence or [`Error::TypeMismatch`] for a
    /// non-table section or non-array value. An incompatible element produces
    /// [`Error::ValueParse`] naming its zero-based index.
    pub fn get_integer_vec(&self, section: &str, key: &str) -> Result<Vec<i64>> {
        let value = self.fetch_value(section, key)?;
        value_conversion::integer_vec(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a floating-point array stored at `section.key`.
    ///
    /// Accepts both float and integer elements, including mixed numeric arrays.
    /// Integer-to-`f64` conversion can lose precision.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence or [`Error::TypeMismatch`] for a
    /// non-table section or non-array value. An incompatible element produces
    /// [`Error::ValueParse`] naming its zero-based index.
    pub fn get_float_vec(&self, section: &str, key: &str) -> Result<Vec<f64>> {
        let value = self.fetch_value(section, key)?;
        value_conversion::float_vec(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a boolean array stored at `section.key`.
    ///
    /// Every element must be a TOML boolean; strings and numbers are not converted.
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence or [`Error::TypeMismatch`] for a
    /// non-table section or non-array value. An incompatible element produces
    /// [`Error::ValueParse`] naming its zero-based index.
    pub fn get_boolean_vec(&self, section: &str, key: &str) -> Result<Vec<bool>> {
        let value = self.fetch_value(section, key)?;
        value_conversion::boolean_vec(&crate::table_path::section_path(section), key, value)
    }

    /// Retrieves a [`Datetime`] array stored at `section.key`, parsing string values when necessary.
    ///
    /// Elements may be TOML datetime values or strings parsed as [`Datetime`].
    ///
    /// # Errors
    ///
    /// Returns [`Error::MissingKey`] for absence or [`Error::TypeMismatch`] for a
    /// non-table section or non-array value. An incompatible or invalid element
    /// produces [`Error::ValueParse`] naming its zero-based index.
    pub fn get_datetime_vec(&self, section: &str, key: &str) -> Result<Vec<Datetime>> {
        let value = self.fetch_value(section, key)?;
        value_conversion::datetime_vec(&crate::table_path::section_path(section), key, value)
    }

    /// Stores a string at `section.key`, creating the section if needed.
    ///
    /// Converts `value` with [`Into<String>`] before validating the section or acquiring
    /// the write lock. Replaces the key's previous value; other keys are unchanged.
    /// Section validation and errors are the same as [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged. The error has an empty `table_path` and uses
    /// `section` as its `key`.
    pub fn set_string(&self, section: &str, key: &str, value: impl Into<String>) -> Result<()> {
        self.set_value(section, key, Value::String(value.into()))
    }

    /// Stores an integer at `section.key`, creating the section if needed.
    ///
    /// Writes a TOML signed 64-bit integer. Replacement and section errors follow
    /// [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_integer(&self, section: &str, key: &str, value: i64) -> Result<()> {
        self.set_value(section, key, Value::Integer(value))
    }

    /// Stores a floating-point number at `section.key`, creating the section if needed.
    ///
    /// Writes a TOML float without integer coercion. Replacement and section errors
    /// follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_float(&self, section: &str, key: &str, value: f64) -> Result<()> {
        self.set_value(section, key, Value::Float(value))
    }

    /// Stores a boolean at `section.key`, creating the section if needed.
    ///
    /// Replaces the key's previous value. Section validation and errors follow
    /// [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_boolean(&self, section: &str, key: &str, value: bool) -> Result<()> {
        self.set_value(section, key, Value::Boolean(value))
    }

    /// Stores a TOML [`Datetime`] at `section.key`, creating the section if needed.
    ///
    /// Writes a datetime value, not a quoted string. Replacement and section errors
    /// follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_datetime(&self, section: &str, key: &str, value: Datetime) -> Result<()> {
        self.set_value(section, key, Value::Datetime(value))
    }

    /// Stores a string array at `section.key`, creating the section if needed.
    ///
    /// Accepts any iterator whose items convert into [`String`], including arrays of
    /// `&str`, vectors of `String`, and a borrowed `Vec<String>`. For a borrowed slice
    /// of `&str`, pass `values.iter().copied()`. A typed empty vector is stored as `[]`,
    /// not removed.
    ///
    /// The iterator is consumed and each item converted before validating the section
    /// or acquiring the write lock. Section validation and errors follow
    /// [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_string_vec(
        &self,
        section: &str,
        key: &str,
        values: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<()> {
        let array = values
            .into_iter()
            .map(|value| Value::String(value.into()))
            .collect();
        self.set_value(section, key, Value::Array(array))
    }

    /// Stores an integer array at `section.key`, creating the section if needed.
    ///
    /// Accepts any iterator of TOML signed 64-bit integers, including borrowed slices
    /// passed with `.iter().copied()`. An empty iterator is written as `[]`.
    /// Section validation and errors follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_integer_vec(
        &self,
        section: &str,
        key: &str,
        values: impl IntoIterator<Item = i64>,
    ) -> Result<()> {
        let array = values.into_iter().map(Value::Integer).collect();
        self.set_value(section, key, Value::Array(array))
    }

    /// Stores a floating-point array at `section.key`, creating the section if needed.
    ///
    /// Accepts any iterator of TOML floating-point numbers, including borrowed slices
    /// passed with `.iter().copied()`. An empty iterator is written as `[]`.
    /// Section validation and errors follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_float_vec(
        &self,
        section: &str,
        key: &str,
        values: impl IntoIterator<Item = f64>,
    ) -> Result<()> {
        let array = values.into_iter().map(Value::Float).collect();
        self.set_value(section, key, Value::Array(array))
    }

    /// Stores a boolean array at `section.key`, creating the section if needed.
    ///
    /// Accepts any iterator of booleans, including borrowed slices passed with
    /// `.iter().copied()`. An empty iterator is written as `[]`, not removed.
    /// Section validation and errors follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_boolean_vec(
        &self,
        section: &str,
        key: &str,
        values: impl IntoIterator<Item = bool>,
    ) -> Result<()> {
        let array = values.into_iter().map(Value::Boolean).collect();
        self.set_value(section, key, Value::Array(array))
    }

    /// Stores a [`Datetime`] array at `section.key`, creating the section if needed.
    ///
    /// Accepts any iterator of [`Datetime`] values, including borrowed slices passed
    /// with `.iter().copied()`. Writes datetime values, not quoted strings. An empty
    /// iterator is written as `[]`.
    /// Section validation and errors follow [`set_value`](Self::set_value).
    ///
    /// # Errors
    ///
    /// Returns [`Error::TypeMismatch`] if the top-level entry named `section` is not a
    /// table, leaving the document unchanged.
    pub fn set_datetime_vec(
        &self,
        section: &str,
        key: &str,
        values: impl IntoIterator<Item = Datetime>,
    ) -> Result<()> {
        let array = values.into_iter().map(Value::Datetime).collect();
        self.set_value(section, key, Value::Array(array))
    }

    /// Fetches the raw TOML [`Value`] stored at `section.key`, producing detailed errors.
    fn fetch_value(&self, section: &str, key: &str) -> Result<Value> {
        let path = crate::table_path::section_path(section);
        let guard = self.read();
        let section_value = guard
            .get(section)
            .ok_or_else(|| Error::missing_key_error(&path, key))?;
        let table = section_value
            .as_table()
            .ok_or_else(|| section_mismatch(section, section_value))?;
        table
            .get(key)
            .cloned()
            .ok_or_else(|| Error::missing_key_error(&path, key))
    }

    fn from_table(table: Table) -> Self {
        Self {
            table: Arc::new(RwLock::new(table)),
        }
    }

    /// Parses a TOML table from `source`, mapping parsing failures into [`Error`].
    fn parse_table(source: &str) -> Result<Table> {
        toml::from_str(source).map_err(Error::from)
    }

    // Every write is a single map operation or replaces a whole table, so the data behind a
    // poisoned lock is consistent and can be used as is.
    fn read(&self) -> RwLockReadGuard<'_, Table> {
        self.table.read().unwrap_or_else(PoisonError::into_inner)
    }

    fn write(&self) -> RwLockWriteGuard<'_, Table> {
        self.table.write().unwrap_or_else(PoisonError::into_inner)
    }
}

impl FromStr for ConfigStore {
    type Err = Error;

    /// Parses a new store with the same behavior and errors as [`ConfigStore::from_str`].
    fn from_str(source: &str) -> Result<Self> {
        ConfigStore::from_str(source)
    }
}

/// Retrieves the table stored within `root` at `section`, if it exists and is a table.
fn section_table<'a>(root: &'a Table, section: &str) -> Option<&'a Table> {
    root.get(section)?.as_table()
}

/// The error for a top-level entry `section` that is not a table.
fn section_mismatch(section: &str, found: &Value) -> Error {
    Error::type_mismatch_error("", section, "table", value_conversion::describe(found))
}

/// Reads the file at `path`, attaching the path to I/O errors.
async fn read_file(path: &Path) -> Result<String> {
    fs::read_to_string(path).await.map_err(|err| {
        Error::io_error_with_operation(Some(path.to_path_buf()), IoOperation::Read, err)
    })
}

/// Persists `contents` to `path` through a temporary sibling and a rename
/// (see [`ConfigStore::save_file`]).
///
/// Every call writes its own temporary sibling, so concurrent saves to the same path never
/// share a temporary file; on error cleanup is attempted. The blocking file
/// operations run on Tokio's blocking pool, like those of [`tokio::fs`].
async fn write_atomic(
    path: &Path,
    contents: String,
    run_save: impl FnOnce(&Path, &[u8]) -> Result<()> + Send + 'static,
) -> Result<()> {
    let target_path = path.to_path_buf();
    let task_path = target_path.clone();
    tokio::task::spawn_blocking(move || run_save(&task_path, contents.as_bytes()))
        .await
        .map_err(|source| Error::SaveFileTaskJoin {
            target_path,
            source,
        })?
}

fn write_atomic_blocking(path: &Path, contents: &[u8]) -> Result<()> {
    write_atomic_blocking_with(
        path,
        contents,
        std::fs::symlink_metadata(path),
        prepare_temporary,
        Write::write_all,
        |from, to| std::fs::rename(from, to),
    )
}

fn write_atomic_blocking_with(
    path: &Path,
    contents: &[u8],
    metadata: std::io::Result<std::fs::Metadata>,
    prepare: impl FnOnce(&std::fs::File, Option<&std::fs::Metadata>) -> std::io::Result<()>,
    write: impl FnOnce(&mut std::fs::File, &[u8]) -> std::io::Result<()>,
    mut rename: impl FnMut(&Path, &Path) -> std::io::Result<()>,
) -> Result<()> {
    let metadata =
        validate_target_metadata(path, metadata).map_err(|error| error.with_save_target(path))?;
    let (tmp_path, mut file) = create_temporary(metadata.as_ref(), || temporary_path(path))
        .map_err(|error| error.with_save_target(path))?;
    let result = (|| {
        prepare(&file, metadata.as_ref()).map_err(|err| {
            Error::io_error_with_operation(
                Some(tmp_path.clone()),
                IoOperation::PrepareTemporary,
                err,
            )
        })?;
        write(&mut file, contents).map_err(|err| {
            Error::io_error_with_operation(Some(tmp_path.clone()), IoOperation::WriteTemporary, err)
        })?;
        // Close the handle before the rename, which Windows requires.
        drop(file);
        rename(&tmp_path, path).map_err(|err| {
            Error::io_error_with_operation(
                Some(path.to_path_buf()),
                IoOperation::ReplaceTarget,
                err,
            )
        })
    })();
    match result {
        Ok(()) => Ok(()),
        Err(save_error) => Err(Error::with_save_file_cleanup(
            path.to_path_buf(),
            save_error.with_save_target(path),
            tmp_path.clone(),
            std::fs::remove_file(&tmp_path),
        )),
    }
}

fn validate_target_metadata(
    path: &Path,
    metadata: std::io::Result<std::fs::Metadata>,
) -> Result<Option<std::fs::Metadata>> {
    let metadata = match metadata {
        Ok(metadata) => metadata,
        Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
        Err(err) => {
            return Err(Error::io_error_with_operation(
                Some(path.to_path_buf()),
                IoOperation::InspectTarget,
                err,
            ));
        }
    };
    #[cfg(windows)]
    {
        use std::os::windows::fs::MetadataExt;
        validate_windows_attributes(path, metadata.file_attributes())?;
    }

    let file_type = metadata.file_type();
    let unsupported = if file_type.is_file() {
        None
    } else if file_type.is_symlink() {
        Some("symlink")
    } else if file_type.is_dir() {
        Some("directory")
    } else {
        #[cfg(unix)]
        {
            use std::os::unix::fs::FileTypeExt;
            Some(if file_type.is_socket() {
                "socket"
            } else if file_type.is_fifo() {
                "FIFO"
            } else if file_type.is_char_device() {
                "character device"
            } else if file_type.is_block_device() {
                "block device"
            } else {
                "non-regular file"
            })
        }
        #[cfg(not(unix))]
        {
            Some("non-regular file")
        }
    };
    if let Some(kind) = unsupported {
        return Err(unsupported_target(path, kind));
    }
    Ok(Some(metadata))
}

fn unsupported_target(path: &Path, kind: &str) -> Error {
    Error::io_error_with_operation(
        Some(path.to_path_buf()),
        IoOperation::InspectTarget,
        std::io::Error::new(
            ErrorKind::InvalidInput,
            format!("unsupported save target: {kind}"),
        ),
    )
}

#[cfg(windows)]
fn validate_windows_attributes(path: &Path, attributes: u32) -> Result<()> {
    const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x00000400;
    if attributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
        return Err(unsupported_target(path, "Windows reparse point"));
    }
    Ok(())
}

/// Creates a new temporary file, never opening an existing entry.
fn create_temporary(
    metadata: Option<&std::fs::Metadata>,
    mut next_path: impl FnMut() -> PathBuf,
) -> Result<(PathBuf, std::fs::File)> {
    const ATTEMPTS: usize = 16;
    let mut attempt = 1;
    loop {
        let tmp_path = next_path();
        let mut options = std::fs::OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        if let Some(metadata) = metadata {
            use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
            options.mode(metadata.permissions().mode() & 0o777);
        }
        #[cfg(not(unix))]
        let _ = metadata;
        let opened = options.open(&tmp_path);
        match opened {
            Ok(file) => return Ok((tmp_path, file)),
            // A stale file from an earlier process can hold the name; the next name differs.
            Err(err) if err.kind() == ErrorKind::AlreadyExists && attempt < ATTEMPTS => {
                attempt += 1;
            }
            Err(err) => {
                return Err(Error::io_error_with_operation(
                    Some(tmp_path),
                    IoOperation::CreateTemporary,
                    err,
                ));
            }
        }
    }
}

#[cfg(unix)]
fn prepare_temporary(
    file: &std::fs::File,
    metadata: Option<&std::fs::Metadata>,
) -> std::io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    if let Some(metadata) = metadata {
        file.set_permissions(std::fs::Permissions::from_mode(
            metadata.permissions().mode() & 0o777,
        ))?;
    }
    Ok(())
}

#[cfg(not(unix))]
fn prepare_temporary(
    _file: &std::fs::File,
    _metadata: Option<&std::fs::Metadata>,
) -> std::io::Result<()> {
    Ok(())
}

/// Computes a unique temporary sibling path independent of the target filename.
fn temporary_path(path: &Path) -> PathBuf {
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
    path.with_file_name(format!(
        ".conferencier.{}.{counter}.tmp",
        std::process::id()
    ))
}

#[cfg(test)]
mod save_tests;

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::error::Error as _;
    use std::future::Future;
    use std::panic::{AssertUnwindSafe, catch_unwind};
    use std::pin::Pin;
    use std::sync::mpsc::sync_channel;
    use std::task::{Context, Poll, Waker};
    use std::time::Duration;

    use super::*;
    use crate::error::{Error, Result};

    use tempfile::NamedTempFile;

    fn poll_without_runtime<F: Future>(future: Pin<&mut F>) -> Poll<F::Output> {
        let mut context = Context::from_waker(Waker::noop());
        future.poll(&mut context)
    }

    #[test]
    fn store_and_file_futures_are_send() {
        fn assert_store<T: Send + Sync + 'static>() {}
        fn assert_send<T: Send>(_: &T) {}

        assert_store::<ConfigStore>();
        let store = ConfigStore::new();
        // The futures are never polled, so no runtime or file is needed.
        assert_send(&ConfigStore::from_file("app.toml"));
        assert_send(&store.load_file("app.toml"));
        assert_send(&store.save_file("app.toml"));
    }

    struct StoreReadingString(ConfigStore);

    impl From<StoreReadingString> for String {
        fn from(value: StoreReadingString) -> Self {
            assert_eq!(value.0.get_integer("App", "answer").unwrap(), 42);
            String::from("converted")
        }
    }

    #[test]
    fn new_store_is_empty() {
        let store = ConfigStore::new();
        assert!(store.list_sections().is_empty());
    }

    #[test]
    fn clones_share_store_data() -> Result<()> {
        let store = ConfigStore::new();
        let clone = store.clone();

        clone.set_string("App", "name", String::from("shared"))?;
        assert_eq!(store.get_string("App", "name")?, "shared");

        clone.load_str("[App]\nname = \"reloaded\"")?;
        assert_eq!(store.get_string("App", "name")?, "reloaded");
        Ok(())
    }

    #[test]
    fn from_str_and_parse_agree() -> Result<()> {
        let source = "[App]\nport = 8080\n";
        let store = ConfigStore::from_str(source)?;
        let parsed: ConfigStore = source.parse()?;
        assert_eq!(store.save_str()?, parsed.save_str()?);

        let invalid = "[App\nport = 8080";
        let inherent = ConfigStore::from_str(invalid).unwrap_err();
        let parsed = invalid.parse::<ConfigStore>().unwrap_err();
        assert!(matches!(&inherent, Error::Parse { path: None, .. }));
        assert!(matches!(&parsed, Error::Parse { path: None, .. }));
        assert_eq!(inherent.to_string(), parsed.to_string());

        let error = store.load_str(invalid).unwrap_err();
        assert!(matches!(&error, Error::Parse { path: None, .. }));
        assert_eq!(error.to_string(), "failed to parse TOML");
        assert!(
            error
                .source()
                .unwrap()
                .downcast_ref::<toml::de::Error>()
                .is_some()
        );
        assert_eq!(store.get_integer("App", "port")?, 8080);
        Ok(())
    }

    #[tokio::test]
    async fn file_reads_report_missing_path() {
        let dir = tempfile::tempdir().expect("temp dir");
        let path = dir.path().join("missing.toml");
        let store = ConfigStore::from_str("[App]\nport = 8080").unwrap();
        for error in [
            ConfigStore::from_file(&path).await.unwrap_err(),
            store.load_file(&path).await.unwrap_err(),
        ] {
            assert!(matches!(
                &error,
                Error::Io {
                    path: Some(reported),
                    operation: Some(IoOperation::Read),
                    target_path: None,
                    ..
                } if reported == &path
            ));
            assert_eq!(error.to_string(), format!("failed to read file {path:?}"));
            let source = error
                .source()
                .unwrap()
                .downcast_ref::<std::io::Error>()
                .unwrap();
            assert_eq!(source.kind(), ErrorKind::NotFound);
        }
        assert_eq!(store.get_integer("App", "port").unwrap(), 8080);
    }

    #[tokio::test]
    async fn blocking_save_panic_preserves_join_error_and_target() {
        let path = Path::new("config.toml");
        let error = write_atomic(path, String::new(), |_, _| {
            panic!("injected blocking save panic")
        })
        .await
        .unwrap_err();
        assert_eq!(error.to_string(), format!("failed to save {path:?}"));

        match &error {
            Error::SaveFileTaskJoin {
                target_path,
                source,
                ..
            } => {
                assert_eq!(target_path, path);
                assert!(source.is_panic());
                let reported_source = error
                    .source()
                    .unwrap()
                    .downcast_ref::<tokio::task::JoinError>()
                    .unwrap();
                assert!(std::ptr::eq(source, reported_source));
            }
            other => panic!("unexpected error: {other}"),
        }
    }

    #[test]
    fn file_reads_panic_without_a_tokio_runtime() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[App]\nport = 80")?;

        let mut from_file = Box::pin(ConfigStore::from_file(&path));
        assert!(
            catch_unwind(AssertUnwindSafe(|| poll_without_runtime(
                from_file.as_mut()
            )))
            .is_err()
        );

        let store = ConfigStore::new();
        let mut load_file = Box::pin(store.load_file(&path));
        assert!(
            catch_unwind(AssertUnwindSafe(|| poll_without_runtime(
                load_file.as_mut()
            )))
            .is_err()
        );
        Ok(())
    }

    #[tokio::test]
    async fn file_parse_errors_keep_the_file_path_and_load_is_atomic() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("invalid.toml");
        std::fs::write(&path, "[broken")?;

        let error = ConfigStore::from_file(&path).await.unwrap_err();
        assert!(matches!(
            &error,
            Error::Parse {
                path: Some(error_path),
                ..
            } if error_path == &path
        ));
        assert_eq!(
            error.to_string(),
            format!("failed to parse TOML file {path:?}")
        );
        assert!(error.source().is_some());

        let store = ConfigStore::from_str("[App]\nport = 8080")?;
        let error = store.load_file(&path).await.unwrap_err();
        assert!(matches!(
            error,
            Error::Parse {
                path: Some(error_path),
                ..
            } if error_path == path
        ));
        assert_eq!(store.get_integer("App", "port")?, 8080);
        Ok(())
    }

    #[test]
    fn nesting_beyond_tomls_default_limit_returns_parse_errors() -> Result<()> {
        let depth = 128;
        let dotted_path = vec!["a"; depth].join(".");
        let inputs = [
            format!("value = {}0{}", "[".repeat(depth), "]".repeat(depth)),
            format!(
                "value = {}0{}",
                "{ value = ".repeat(depth),
                " }".repeat(depth)
            ),
            format!("{dotted_path}.value = 0"),
            format!("[{dotted_path}]\nvalue = 0"),
        ];
        let store = ConfigStore::from_str("[Existing]\nkeep = 1")?;
        let before = store.save_str()?;

        for source in inputs {
            let error = ConfigStore::from_str(&source).unwrap_err();
            assert!(matches!(error, Error::Parse { .. }));

            let error = store.load_str(&source).unwrap_err();
            assert!(matches!(error, Error::Parse { .. }));
            assert_eq!(store.save_str()?, before);
        }
        Ok(())
    }

    #[test]
    fn poisoned_lock_keeps_working() -> Result<()> {
        let store = ConfigStore::from_str("[App]\nport = 718293")?;
        let shared = store.clone();
        let _ = std::thread::spawn(move || {
            let _guard = shared.write();
            panic!("poison the store lock");
        })
        .join();
        assert!(store.table.is_poisoned());

        let debug = format!("{store:?}");
        assert!(debug.contains("App"));
        assert!(!debug.contains("718293"));
        store.set_integer("App", "port", 2)?;
        assert_eq!(store.get_integer("App", "port")?, 2);
        Ok(())
    }

    #[test]
    fn debug_lists_top_level_keys_without_values() -> Result<()> {
        let store = ConfigStore::from_str(
            "App = { password = \"sensitive_value_marker\" }\nMode = \"secret_mode_marker\"\nCounter = 92837465",
        )?;

        for debug in [format!("{store:?}"), format!("{store:#?}")] {
            assert!(debug.contains("App"));
            assert!(debug.contains("Mode"));
            assert!(debug.contains("Counter"));
            assert!(!debug.contains("sensitive_value_marker"));
            assert!(!debug.contains("secret_mode_marker"));
            assert!(!debug.contains("92837465"));
        }
        Ok(())
    }

    #[test]
    fn debug_does_not_wait_for_a_locked_store() -> Result<()> {
        let store = ConfigStore::from_str("[App]\nport = 80")?;
        let guard = store.write();
        let debug_store = store.clone();
        let (sender, receiver) = sync_channel(1);

        let debug_thread = std::thread::spawn(move || {
            let _ = sender.send(format!("{debug_store:?}"));
        });

        let result = receiver.recv_timeout(Duration::from_secs(5));
        drop(guard);
        let debug = result.expect("Debug should not wait for the store lock");
        debug_thread
            .join()
            .expect("formatting the store should not panic");
        assert!(debug.contains("locked"));
        Ok(())
    }

    #[test]
    fn section_tables_create_replace_and_snapshot() -> Result<()> {
        let store = ConfigStore::from_str("[Other]\nkeep = 1")?;
        assert!(store.read_section("App")?.is_empty());
        let first: Table = "old = 1\nnested = { stale = true }".parse().unwrap();
        store.set_section("App", first.clone())?;
        let mut snapshot = store.read_section("App")?;
        assert_eq!(snapshot, first);
        snapshot.clear();
        assert_eq!(store.get_section("App"), Some(first));

        let second: Table = "new = 2".parse().unwrap();
        store.set_section("App", second.clone())?;
        assert_eq!(store.get_section("App"), Some(second));
        assert_eq!(store.get_integer("Other", "keep")?, 1);
        store.set_section("App", Table::new())?;
        assert_eq!(store.get_section("App"), Some(Table::new()));
        store.set_section("App.server", Table::new())?;
        assert!(store.has_section("App.server"));
        assert!(store.get_value("App", "server").is_none());
        Ok(())
    }

    #[test]
    fn strict_section_operations_reject_non_tables_without_changes() -> Result<()> {
        let store = ConfigStore::from_str("App = 7\n[Other]\nkeep = true")?;
        let before = store.save_str()?;
        for error in [
            store.read_section("App").unwrap_err(),
            store.set_section("App", Table::new()).unwrap_err(),
        ] {
            assert!(matches!(
                &error,
                Error::TypeMismatch { table_path, key, expected: "table", found: "integer" }
                    if table_path.is_empty() && key == "App"
            ));
            assert_eq!(error.to_string(), "expected table at App but found integer");
        }
        assert_eq!(store.save_str()?, before);
        Ok(())
    }

    #[test]
    fn diagnostics_quote_literal_top_level_sections_and_keys() {
        let error = ConfigStore::new()
            .get_integer("App.server", "port")
            .unwrap_err();
        assert!(matches!(
            error,
            Error::MissingKey { ref table_path, ref key }
                if table_path == "\"App.server\"" && key == "port"
        ));
        assert_eq!(error.to_string(), "missing key \"App.server\".port");

        let error = Error::missing_key_error("App", "a.b");
        assert_eq!(error.to_string(), "missing key App.\"a.b\"");

        let error = ConfigStore::new().get_integer("", "key").unwrap_err();
        assert_eq!(error.to_string(), "missing key \"\".key");

        let error = Error::missing_key_error("App", "a\"b\n");
        assert_eq!(error.to_string(), "missing key App.\"a\\\"b\\n\"");
    }

    #[test]
    fn set_and_get_string_roundtrip() -> Result<()> {
        let store = ConfigStore::new();
        store.set_string("App", "name", "demo")?;
        assert_eq!(store.get_string("App", "name")?, "demo");
        assert_eq!(
            store.get_value("App", "name"),
            Some(Value::String("demo".into()))
        );
        Ok(())
    }

    #[test]
    fn typed_setters_accept_into_and_iterable_inputs() -> Result<()> {
        let store = ConfigStore::new();

        store.set_string("App", "borrowed", "text")?;
        store.set_string("App", "owned", String::from("text"))?;

        store.set_string_vec("App", "array", ["array", "values"])?;
        store.set_string_vec(
            "App",
            "owned_vec",
            vec![String::from("owned"), String::from("values")],
        )?;
        let borrowed_vec = vec![String::from("borrowed"), String::from("values")];
        store.set_string_vec("App", "borrowed_vec", &borrowed_vec)?;
        let borrowed_slice = ["slice", "values"];
        store.set_string_vec("App", "borrowed_slice", borrowed_slice.iter().copied())?;
        store.set_string_vec(
            "App",
            "mapped",
            (0..2).map(|index| format!("mapped-{index}")),
        )?;

        let integers = [1, 2];
        store.set_integer_vec("App", "integers", integers.iter().copied())?;
        let floats = [1.5, 2.5];
        store.set_float_vec("App", "floats", floats.iter().copied())?;
        let booleans = [true, false];
        store.set_boolean_vec("App", "booleans", booleans.iter().copied())?;
        let date: Datetime = "2024-01-01".parse().expect("valid TOML date");
        let dates = [date, date];
        store.set_datetime_vec("App", "dates", dates.iter().copied())?;

        store.set_integer_vec("App", "empty_integers", vec![])?;
        store.set_string_vec("App", "empty_strings", Vec::<String>::new())?;

        assert_eq!(store.get_string("App", "borrowed")?, "text");
        assert_eq!(store.get_string("App", "owned")?, "text");
        assert_eq!(
            store.get_string_vec("App", "array")?,
            vec![String::from("array"), String::from("values")]
        );
        assert_eq!(
            store.get_string_vec("App", "owned_vec")?,
            vec![String::from("owned"), String::from("values")]
        );
        assert_eq!(
            store.get_string_vec("App", "borrowed_vec")?,
            vec![String::from("borrowed"), String::from("values")]
        );
        assert_eq!(
            store.get_string_vec("App", "borrowed_slice")?,
            vec![String::from("slice"), String::from("values")]
        );
        assert_eq!(
            store.get_string_vec("App", "mapped")?,
            vec![String::from("mapped-0"), String::from("mapped-1")]
        );
        assert_eq!(store.get_integer_vec("App", "integers")?, integers);
        assert_eq!(store.get_float_vec("App", "floats")?, floats);
        assert_eq!(store.get_boolean_vec("App", "booleans")?, booleans);
        assert_eq!(store.get_datetime_vec("App", "dates")?, dates);
        assert!(store.get_integer_vec("App", "empty_integers")?.is_empty());
        assert!(store.get_string_vec("App", "empty_strings")?.is_empty());
        Ok(())
    }

    #[test]
    fn setter_inputs_can_read_the_same_store_without_deadlocking() -> Result<()> {
        let store = ConfigStore::from_str("[App]\nanswer = 42")?;
        let (sender, receiver) = sync_channel(1);
        let conversion_store = store.clone();
        std::thread::spawn(move || {
            let result = conversion_store.set_string(
                "App",
                "converted",
                StoreReadingString(conversion_store.clone()),
            );
            let _ = sender.send(result);
        });
        receiver
            .recv_timeout(Duration::from_secs(5))
            .expect("custom string conversion should finish outside the write lock")?;
        assert_eq!(store.get_string("App", "converted")?, "converted");

        let (sender, receiver) = sync_channel(1);
        let iterator_store = store.clone();
        std::thread::spawn(move || {
            let values = (0..2).map(|index| {
                assert_eq!(iterator_store.get_integer("App", "answer").unwrap(), 42);
                format!("item-{index}")
            });
            let result = iterator_store.set_string_vec("App", "items", values);
            let _ = sender.send(result);
        });
        receiver
            .recv_timeout(Duration::from_secs(5))
            .expect("iterator should finish outside the write lock")?;
        assert_eq!(
            store.get_string_vec("App", "items")?,
            vec![String::from("item-0"), String::from("item-1")]
        );
        Ok(())
    }

    #[test]
    fn typed_setters_consume_inputs_before_rejecting_invalid_sections() -> Result<()> {
        let store = ConfigStore::from_str("App = 7\n[Other]\nkeep = true")?;
        let before = store.save_str()?;
        let consumed = Cell::new(0);
        let values = (0_i64..3).inspect(|_| consumed.set(consumed.get() + 1));

        let error = store.set_integer_vec("App", "values", values).unwrap_err();
        assert!(matches!(
            error,
            Error::TypeMismatch {
                ref table_path,
                ref key,
                expected: "table",
                found: "integer",
                ..
            } if table_path.is_empty() && key == "App"
        ));
        assert_eq!(consumed.get(), 3);
        assert_eq!(store.save_str()?, before);
        Ok(())
    }

    #[test]
    fn datetime_fallback_from_string() -> Result<()> {
        let store = ConfigStore::new();
        store.set_string("Build", "time", "2024-01-01T00:00:00Z")?;
        let dt = store.get_datetime("Build", "time")?;
        assert_eq!(dt.to_string(), "2024-01-01T00:00:00Z");
        Ok(())
    }

    #[test]
    fn float_vec_accepts_integers() -> Result<()> {
        let store = ConfigStore::new();
        store.set_value(
            "App",
            "thresholds",
            Value::Array(vec![Value::Integer(1), Value::Float(2.5)]),
        )?;
        let values = store.get_float_vec("App", "thresholds")?;
        assert_eq!(values, vec![1.0, 2.5]);
        Ok(())
    }

    #[test]
    fn scalar_array_type_and_datetime_errors_include_the_index() -> Result<()> {
        let store = ConfigStore::from_str(
            "[App]\nports = [1, true]\ntimes = [\"2024-01-01\", \"not-a-date\"]",
        )?;

        let error = store.get_integer_vec("App", "ports").unwrap_err();
        assert!(matches!(
            error,
            Error::ValueParse { ref table_path, ref key, ref message }
                if table_path == "App"
                    && key == "ports"
                    && message == "expected array elements of type integer, found boolean (at index 1)"
        ));

        let error = store.get_datetime_vec("App", "times").unwrap_err();
        assert!(matches!(
            error,
            Error::ValueParse { ref table_path, ref key, ref message }
                if table_path == "App"
                    && key == "times"
                    && message.ends_with("(at index 1)")
        ));
        Ok(())
    }

    #[test]
    fn missing_key_yields_error() {
        let store = ConfigStore::new();
        let err = store.get_string("Missing", "key").unwrap_err();
        assert!(matches!(err, Error::MissingKey { .. }));
    }

    #[test]
    fn load_str_replaces_content() -> Result<()> {
        let store = ConfigStore::new();
        store.set_string("App", "name", "demo")?;
        store.load_str("[Srv]\nport = 8080\n")?;

        assert!(store.get_value("App", "name").is_none());
        assert_eq!(store.get_integer("Srv", "port")?, 8080);
        Ok(())
    }

    #[test]
    fn list_keys_missing_section_empty() -> Result<()> {
        let store = ConfigStore::new();
        assert!(store.list_keys("Unknown")?.is_empty());
        Ok(())
    }

    #[test]
    fn ensure_section_is_idempotent() -> Result<()> {
        let store = ConfigStore::new();
        store.ensure_section("Srv")?;
        store.set_integer("Srv", "port", 1)?;
        store.ensure_section("Srv")?;
        assert_eq!(store.get_integer("Srv", "port")?, 1);
        Ok(())
    }

    #[test]
    fn remove_key_missing_is_ok() -> Result<()> {
        let store = ConfigStore::new();
        store.remove_key("Srv", "unknown")?;
        store.ensure_section("Srv")?;
        store.remove_key("Srv", "missing")?;
        Ok(())
    }

    #[tokio::test]
    async fn save_and_load_file_roundtrip() -> Result<()> {
        let store = ConfigStore::new();
        store.set_string("App", "name", "demo")?;
        store.set_integer("App", "port", 3000)?;

        let temp = NamedTempFile::new().expect("temp file");
        let path_buf = temp.path().to_path_buf();
        store.save_file(&path_buf).await?;

        let restored = ConfigStore::from_file(&path_buf).await?;
        assert_eq!(restored.get_string("App", "name")?, "demo");
        assert_eq!(restored.get_integer("App", "port")?, 3000);
        Ok(())
    }

    #[tokio::test]
    async fn save_serialization_error_carries_the_target_path() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        let store = ConfigStore::new();
        store.set_datetime(
            "App",
            "invalid",
            Datetime {
                date: None,
                time: None,
                offset: None,
            },
        )?;

        let error = store.save_str().unwrap_err();
        assert!(matches!(
            &error,
            Error::Serialize {
                target_path: None,
                ..
            }
        ));
        assert_eq!(error.to_string(), "failed to serialize TOML");
        assert!(
            error
                .source()
                .unwrap()
                .downcast_ref::<toml::ser::Error>()
                .is_some()
        );

        let error = store.save_file(&path).await.unwrap_err();
        assert!(matches!(
            &error,
            Error::Serialize {
                target_path: Some(target_path),
                ..
            } if target_path == &path
        ));
        assert_eq!(
            error.to_string(),
            format!("failed to serialize TOML for {path:?}")
        );
        assert!(
            error
                .source()
                .unwrap()
                .downcast_ref::<toml::ser::Error>()
                .is_some()
        );
        assert!(!path.exists());
        Ok(())
    }

    #[test]
    fn save_file_requires_a_runtime_only_after_serialization() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "original")?;

        let invalid = ConfigStore::new();
        invalid.set_datetime(
            "App",
            "invalid",
            Datetime {
                date: None,
                time: None,
                offset: None,
            },
        )?;
        let mut serialization = Box::pin(invalid.save_file(&path));
        assert!(matches!(
            poll_without_runtime(serialization.as_mut()),
            Poll::Ready(Err(Error::Serialize {
                target_path: Some(target_path),
                ..
            })) if target_path == path
        ));
        assert_eq!(std::fs::read_to_string(&path)?, "original");

        let valid = ConfigStore::from_str("[App]\nport = 80")?;
        let mut file_operation = Box::pin(valid.save_file(&path));
        assert!(
            catch_unwind(AssertUnwindSafe(|| poll_without_runtime(
                file_operation.as_mut()
            )))
            .is_err()
        );
        assert_eq!(std::fs::read_to_string(&path)?, "original");
        Ok(())
    }

    #[tokio::test(flavor = "current_thread")]
    async fn file_methods_work_on_a_current_thread_runtime() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[App]\nport = 80")?;

        let store = ConfigStore::from_file(&path).await?;
        store.set_integer("App", "port", 8080)?;
        store.save_file(&path).await?;

        let reloaded = ConfigStore::new();
        reloaded.load_file(&path).await?;
        assert_eq!(reloaded.get_integer("App", "port")?, 8080);
        Ok(())
    }

    #[tokio::test]
    async fn save_file_overwrites_existing() -> Result<()> {
        let store = ConfigStore::new();
        store.set_boolean("Flags", "enabled", true)?;
        let temp = NamedTempFile::new().expect("temp file");
        let path_buf = temp.path().to_path_buf();
        store.save_file(&path_buf).await?;

        // Write again with different content to ensure overwrite
        store.set_boolean("Flags", "enabled", false)?;
        store.save_file(&path_buf).await?;

        let file_contents = tokio::fs::read_to_string(&path_buf).await?;
        assert!(file_contents.contains("enabled = false"));
        Ok(())
    }

    #[test]
    fn publication_error_does_not_remove_or_retry_target() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "original")?;
        let mut attempts = 0;
        let mut temporary_path = None;

        let error = write_atomic_blocking_with(
            &path,
            b"replacement",
            std::fs::symlink_metadata(&path),
            prepare_temporary,
            Write::write_all,
            |from, to| {
                attempts += 1;
                assert_eq!(to, path);
                assert_eq!(std::fs::read(from)?, b"replacement");
                temporary_path = Some(from.to_path_buf());
                Err(std::io::Error::new(
                    ErrorKind::AlreadyExists,
                    "rename failed",
                ))
            },
        )
        .unwrap_err();

        assert_eq!(attempts, 1);
        assert!(matches!(
            &error,
            Error::Io {
                path: Some(error_path),
                operation: Some(IoOperation::ReplaceTarget),
                target_path: Some(target_path),
                ..
            } if error_path == &path && target_path == &path
        ));
        match error {
            Error::Io {
                path: error_path,
                source,
                operation: Some(IoOperation::ReplaceTarget),
                target_path: Some(target_path),
                ..
            } => {
                assert_eq!(error_path.as_deref(), Some(path.as_path()));
                assert_eq!(target_path, path);
                assert_eq!(source.kind(), ErrorKind::AlreadyExists);
                assert_eq!(source.to_string(), "rename failed");
            }
            other => panic!("unexpected error: {other}"),
        }
        assert_eq!(std::fs::read(&path)?, b"original");
        assert!(!temporary_path.expect("rename was called").exists());
        Ok(())
    }

    #[cfg(windows)]
    #[tokio::test]
    async fn save_file_reports_windows_sharing_conflict() -> Result<()> {
        use std::os::windows::fs::OpenOptionsExt;

        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "original")?;
        let locked = std::fs::OpenOptions::new()
            .read(true)
            .share_mode(0x00000003) // Share reads and writes, but not deletion.
            .open(&path)?;
        let store = ConfigStore::new();
        store.set_integer("App", "value", 42)?;

        match store.save_file(&path).await.unwrap_err() {
            Error::Io {
                path: error_path,
                source,
                ..
            } => {
                assert_eq!(error_path.as_deref(), Some(path.as_path()));
                assert!(source.raw_os_error().is_some());
            }
            other => panic!("unexpected error: {other}"),
        }
        assert_eq!(std::fs::read(&path)?, b"original");
        drop(locked);
        let entries: Vec<_> = std::fs::read_dir(dir.path())?
            .map(|entry| entry.map(|entry| entry.file_name()))
            .collect::<std::io::Result<_>>()?;
        assert_eq!(entries, ["config.toml"]);
        Ok(())
    }

    #[tokio::test]
    async fn concurrent_reader_sees_complete_replacements() -> Result<()> {
        use std::io::Read;
        use std::sync::atomic::AtomicBool;
        use std::sync::mpsc::{channel, sync_channel};
        use std::time::Duration;

        let dir = tempfile::tempdir()?;
        let path = dir.path().join("config.toml");
        let mut stores = Vec::new();
        let mut snapshots = Vec::new();
        for value in ["a", "b"] {
            let store = ConfigStore::new();
            store.set_string("App", "payload", value.repeat(64 * 1024))?;
            snapshots.push(store.save_str()?.into_bytes());
            stores.push(store);
        }
        std::fs::write(&path, &snapshots[0])?;

        let stop = Arc::new(AtomicBool::new(false));
        let reader_stop = Arc::clone(&stop);
        let reader_path = path.clone();
        let (ready_tx, ready_rx) = sync_channel(0);
        let (resume_tx, resume_rx) = channel();
        let reader = std::thread::spawn(move || {
            let mut reads = 0;
            while !reader_stop.load(Ordering::Acquire) {
                let mut options = std::fs::OpenOptions::new();
                options.read(true);
                #[cfg(windows)]
                {
                    use std::os::windows::fs::OpenOptionsExt;
                    options.share_mode(0x00000007); // Allow replacement while reading.
                }
                let mut file = options.open(&reader_path)?;
                let mut contents = Vec::new();
                file.read_to_end(&mut contents)?;
                assert!(
                    snapshots.contains(&contents),
                    "reader saw a partial snapshot"
                );
                reads += 1;
                if reads == 2 {
                    assert_eq!(
                        contents, snapshots[1],
                        "reader missed the first replacement"
                    );
                }
                if reads <= 2 && ready_tx.send(()).is_err() {
                    break;
                }
                if reads == 1 && resume_rx.recv().is_err() {
                    break;
                }
            }
            Ok::<_, std::io::Error>(reads)
        });

        let result: Result<()> = async {
            ready_rx
                .recv_timeout(Duration::from_secs(10))
                .expect("reader did not start");
            for index in 0..100 {
                stores[(index + 1) % stores.len()].save_file(&path).await?;
                if index == 0 {
                    resume_tx.send(()).expect("reader stopped");
                    ready_rx
                        .recv_timeout(Duration::from_secs(10))
                        .expect("reader did not run during saves");
                }
            }
            Ok(())
        }
        .await;
        stop.store(true, Ordering::Release);
        drop(ready_rx);
        drop(resume_tx);
        let reads = reader.join().expect("reader failed")?;
        result?;
        assert!(reads >= 2);
        Ok(())
    }

    #[tokio::test]
    async fn save_file_supports_long_target_filenames() -> Result<()> {
        let dir = tempfile::tempdir().expect("temp dir");
        let filename = format!("{}.toml", "a".repeat(245));
        let path = dir.path().join(filename);
        std::fs::write(&path, "original = true")?;

        let store = ConfigStore::new();
        store.set_integer("App", "value", 42)?;
        store.save_file(&path).await?;

        let saved = std::fs::read_to_string(&path)?;
        assert!(saved.contains("value = 42"));
        let entries: Vec<_> = std::fs::read_dir(dir.path())?
            .map(|entry| entry.map(|entry| entry.file_name()))
            .collect::<std::io::Result<_>>()?;
        assert_eq!(entries, [path.file_name().unwrap()]);
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn concurrent_saves_install_complete_snapshots() -> Result<()> {
        let dir = tempfile::tempdir().expect("temp dir");
        let path = dir.path().join("config.toml");

        let mut snapshots = Vec::new();
        let mut tasks = Vec::new();
        for writer in 0..4_i64 {
            let store = ConfigStore::new();
            store.set_integer("App", "writer", writer)?;
            store.set_string("App", "payload", "x".repeat(64 * 1024))?;
            snapshots.push(store.save_str()?);

            let path = path.clone();
            tasks.push(tokio::spawn(async move {
                for _ in 0..50 {
                    store.save_file(&path).await?;
                }
                Ok::<(), Error>(())
            }));
        }
        for task in tasks {
            task.await.expect("save task panicked")?;
        }

        let saved = std::fs::read_to_string(&path)?;
        assert!(snapshots.contains(&saved), "the file mixes several saves");
        let entries: Vec<_> = std::fs::read_dir(dir.path())?
            .map(|entry| entry.map(|entry| entry.file_name()))
            .collect::<std::io::Result<_>>()?;
        assert_eq!(entries, ["config.toml"], "temporary files were left behind");
        Ok(())
    }
}