bilingua-model 0.10.0

Bilingua: the extracted-document model shared by every Bilingua file parser
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
//! Bilingua document model: what every file parser extracts and what it writes back.
//!
//! A parser (`Filter`) turns a source file into a [`Document`]: translatable [`Unit`]s (a paragraph, a cell, a
//! string) and a [`Skeleton`] that holds everything else byte for byte. Each unit holds one or more
//! [`Segment`]s (one before segmentation; sentences after `bilingua-seg` splits it). Formatting inside the text
//! is kept as inline [`Inline::Tag`] codes whose original markup lives in [`Unit::codes`], so a writer can put
//! it back exactly.
//!
//! JSON shape: `Inline` and `TagKind` serialise exactly like `catcore::Inline` and `catcore::TagKind`
//! (`{"text": ".."}` / `{"tag": {"id", "kind": "open|close|standalone", "display"}}`), so one editor UI shows
//! bilingual files (catcore) and source files (Bilingua) the same way. Every field added since is optional and
//! left out of the JSON when empty: a tag's `free` flag, a segment's `origin`, a unit's `notes` and `lock_reason`,
//! a skeleton's `parser`.
//!
//! Bilingual files (XLIFF and the like) use the same model: a segment may carry a target, a state and an origin
//! (with a match percentage), and a unit may carry notes and a lock reason. Monolingual formats leave them empty.
//!
//! [`profile`] holds what one consumer needs and no other (the Localization OS profile, and
//! [`profile::tokens`], which converts inline codes to and from LocOS's text tokens, one token per tag).

#![warn(missing_docs)]
#![deny(unsafe_code)]

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

pub mod b64;
/// CLDR plural categories per language, shared by the format crates (not part of the semver API).
#[doc(hidden)]
pub mod cldr;
pub mod codepage;
pub mod enc;
pub mod garble;
pub mod guard;
/// The gettext plural rules of Localization OS, shared by the mo and po crates (not part of the semver API).
#[doc(hidden)]
pub mod plural_table;
pub mod profile;
mod pytables;
pub mod pytext;
pub mod smallmap;
/// A scanning-step counter for tests that show a parser is linear without timing it (not part of the semver API).
#[doc(hidden)]
pub mod work;

// Unit model step 6 (BILI-119a): plural groups and the shared ICU parser.
pub mod icu;
mod plural;
pub use plural::{PluralKind, PluralSlot};

pub use encoding_rs;
pub use guard::{LimitError, LimitKind, Limits};
pub use smallmap::{SmallMap, TextMap};
pub use pytext::{py_is_word_char, py_is_word_start_char, py_isdecimal_char, py_isalnum_char, py_isalpha_char, py_isspace_char, py_has_alnum, py_has_alpha, py_isalnum, py_isalpha, py_isspace, py_lstrip, py_rstrip, py_strip};

/// What a builder takes for an optional text field: `"de-DE"`, a `String`, or an `Option` of either (`None`
/// clears the field). `impl Into<Option<String>>` would refuse a plain `&str`, so the builders take this instead.
///
/// The trait is sealed: Bilingua implements it for `&str`, `String`, `&String` and their `Option`s, and may add
/// more types in a 1.x release; other crates cannot implement it.
pub trait IntoOptionString: sealed::Sealed {
    /// The value as an optional `String`.
    fn into_option_string(self) -> Option<String>;
}

mod sealed {
    pub trait Sealed {}
    impl Sealed for &str {}
    impl Sealed for String {}
    impl Sealed for &String {}
    impl Sealed for Option<String> {}
    impl Sealed for Option<&str> {}
    impl Sealed for Option<&String> {}
}

impl IntoOptionString for &str {
    fn into_option_string(self) -> Option<String> {
        Some(self.to_string())
    }
}
impl IntoOptionString for String {
    fn into_option_string(self) -> Option<String> {
        Some(self)
    }
}
impl IntoOptionString for &String {
    fn into_option_string(self) -> Option<String> {
        Some(self.clone())
    }
}
impl IntoOptionString for Option<String> {
    fn into_option_string(self) -> Option<String> {
        self
    }
}
impl IntoOptionString for Option<&str> {
    fn into_option_string(self) -> Option<String> {
        self.map(str::to_string)
    }
}
impl IntoOptionString for Option<&String> {
    fn into_option_string(self) -> Option<String> {
        self.cloned()
    }
}

/// One piece of segment content, in logical order.
///
/// The enum is closed (text or a code), matching catcore's JSON. The `Tag` variant is `#[non_exhaustive]`: build it
/// with [`Inline::tag`] or [`Inline::free_tag`] (and [`Inline::with_free`]), and match it with `..`, so a code can
/// carry more in a 1.x release.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Inline {
    /// Plain text.
    Text(String),
    /// An inline code. `free` is true for a standalone code that names its argument (`%1$s`, `{name}`, `{0}`, an
    /// XLIFF `<x id>` or `<ph id>`): a translation may move it, because the writer finds it again by `id`. It is
    /// false for markup and for unnumbered placeholders (`%s`), whose order is the argument order. Only a
    /// standalone code can be free.
    #[non_exhaustive]
    Tag {
        /// The code's id: the key of its original markup in [`Unit::codes`]. An open and its close share it.
        id: String,
        /// Open, close or standalone.
        kind: TagKind,
        /// What an editor shows for the code (`<b>`, `{name}`).
        display: String,
        /// The code may move freely in the target (see above).
        #[serde(default, skip_serializing_if = "is_false")]
        free: bool,
    },
}

fn is_false(b: &bool) -> bool {
    !*b
}

impl Inline {
    /// A text piece.
    pub fn text(s: impl Into<String>) -> Inline {
        Inline::Text(s.into())
    }
    /// A code that is not free (markup, or an unnumbered placeholder).
    pub fn tag(id: impl Into<String>, kind: TagKind, display: impl Into<String>) -> Inline {
        Inline::Tag { id: id.into(), kind, display: display.into(), free: false }
    }
    /// A standalone code that may move freely in the target (see [`Inline::Tag`]).
    pub fn free_tag(id: impl Into<String>, display: impl Into<String>) -> Inline {
        Inline::Tag { id: id.into(), kind: TagKind::Standalone, display: display.into(), free: true }
    }
    /// True for a free-order standalone code.
    pub fn is_free(&self) -> bool {
        matches!(self, Inline::Tag { free: true, .. })
    }
    /// Sets the free flag of a standalone code (`Inline::tag(id, kind, display).with_free(f)`). Only a standalone
    /// code can be free: on an open or close code, and on text, the flag stays false.
    pub fn with_free(mut self, value: bool) -> Inline {
        if let Inline::Tag { kind, free, .. } = &mut self {
            *free = value && *kind == TagKind::Standalone;
        }
        self
    }
}

/// The kind of an inline code. Closed: a code opens, closes or stands alone.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TagKind {
    /// Opens a pair (`<b>`).
    Open,
    /// Closes a pair (`</b>`).
    Close,
    /// Stands alone (`<br/>`, `{name}`, `%s`).
    Standalone,
}

/// The original markup of one inline code, kept so the writer can reproduce it.
/// A paired code fills `open` and `close`; a standalone code fills `standalone`. Build it with
/// [`CodeData::paired`], [`CodeData::standalone`] or [`CodeData::new`] and the `with_*` methods.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CodeData {
    /// The opening markup of a paired code.
    pub open: Option<String>,
    /// The closing markup of a paired code.
    pub close: Option<String>,
    /// The markup of a standalone code.
    pub standalone: Option<String>,
    /// Human label for the tag chip, e.g. "bold", "link".
    pub label: Option<String>,
    /// What the code is (bold, link, field...), when the format knows (BILI-118). JSON key `type`, left out when
    /// unset. Extractors fill it; writers never read it. Unset under the LocOS profile.
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub ty: Option<CodeType>,
    /// Facts about the code, keyed per type (see [`CodeType::attr_keys`]): `href` and `title` for a link, ...
    /// Left out of the JSON when empty. Unset under the LocOS profile.
    #[serde(default, skip_serializing_if = "TextMap::is_empty")]
    pub attrs: TextMap,
}

impl CodeData {
    /// No markup and no label.
    pub fn new() -> CodeData {
        CodeData::default()
    }
    /// A paired code's markup.
    pub fn paired(open: impl Into<String>, close: impl Into<String>) -> CodeData {
        let (open, close): (String, String) = (open.into(), close.into());
        CodeData::new().with_open(open).with_close(close)
    }
    /// A standalone code's markup.
    pub fn standalone(markup: impl Into<String>) -> CodeData {
        let markup: String = markup.into();
        CodeData::new().with_standalone(markup)
    }
    /// Sets (or with `None` clears) the opening markup.
    pub fn with_open(mut self, open: impl IntoOptionString) -> CodeData {
        self.open = open.into_option_string();
        self
    }
    /// Sets (or clears) the closing markup.
    pub fn with_close(mut self, close: impl IntoOptionString) -> CodeData {
        self.close = close.into_option_string();
        self
    }
    /// Sets (or clears) the standalone markup.
    pub fn with_standalone(mut self, standalone: impl IntoOptionString) -> CodeData {
        self.standalone = standalone.into_option_string();
        self
    }
    /// Sets (or clears) the chip label.
    pub fn with_label(mut self, label: impl IntoOptionString) -> CodeData {
        self.label = label.into_option_string();
        self
    }
    /// The opening markup of a paired code. (`open_markup`, not `open`, to match [`CodeData::standalone_markup`],
    /// whose plain name is taken by the [`CodeData::standalone`] constructor.)
    pub fn open_markup(&self) -> Option<&str> {
        self.open.as_deref()
    }
    /// The closing markup of a paired code.
    pub fn close_markup(&self) -> Option<&str> {
        self.close.as_deref()
    }
    /// The markup of a standalone code.
    pub fn standalone_markup(&self) -> Option<&str> {
        self.standalone.as_deref()
    }
    /// The chip label.
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }
    /// Sets (or with `None` clears) the opening markup in place.
    pub fn set_open(&mut self, open: impl IntoOptionString) {
        self.open = open.into_option_string();
    }
    /// Sets (or clears) the closing markup in place.
    pub fn set_close(&mut self, close: impl IntoOptionString) {
        self.close = close.into_option_string();
    }
    /// Sets (or clears) the standalone markup in place.
    pub fn set_standalone(&mut self, standalone: impl IntoOptionString) {
        self.standalone = standalone.into_option_string();
    }
    /// Sets (or clears) the chip label in place.
    pub fn set_label(&mut self, label: impl IntoOptionString) {
        self.label = label.into_option_string();
    }
}

// --- BILI-118: typed inline codes (unit model step 5) ---------------------------------------------------

impl CodeData {
    /// Sets (or clears) the code type.
    pub fn with_code_type(mut self, ty: Option<CodeType>) -> CodeData {
        self.ty = ty;
        self
    }
    /// Sets one attribute (`href`, `field`, ...); an empty value is not stored.
    pub fn with_attr(mut self, key: &str, value: impl Into<Box<str>>) -> CodeData {
        self.set_attr(key, value);
        self
    }
    /// What the code is, when the format knows.
    pub fn code_type(&self) -> Option<CodeType> {
        self.ty
    }
    /// Sets (or with `None` clears) the code type in place.
    pub fn set_code_type(&mut self, ty: Option<CodeType>) {
        self.ty = ty;
    }
    /// The code's attributes (keys per [`CodeType::attr_keys`]).
    pub fn attrs(&self) -> &TextMap {
        &self.attrs
    }
    /// The code's attributes, for changing.
    pub fn attrs_mut(&mut self) -> &mut TextMap {
        &mut self.attrs
    }
    /// One attribute's value.
    pub fn attr(&self, key: &str) -> Option<&str> {
        self.attrs.get(key)
    }
    /// Sets one attribute in place; an empty value removes it instead.
    pub fn set_attr(&mut self, key: &str, value: impl Into<Box<str>>) {
        let value: Box<str> = value.into();
        if value.is_empty() {
            self.attrs.remove(key);
        } else {
            self.attrs.insert(key, value);
        }
    }
    /// Clears the type and the attributes (what the LocOS profile does to every code).
    pub fn clear_typing(&mut self) {
        self.ty = None;
        self.attrs.clear();
    }
}

/// What an inline code is, for chips and QA ("bold", "link to x.com"). Closed list with a catch-all: match with a
/// wildcard arm; a JSON value this release does not know reads as `Other`. Serialised lowercase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum CodeType {
    /// Bold or strong.
    Bold,
    /// Italic or emphasis.
    Italic,
    /// Underline.
    Underline,
    /// Strike-through.
    Strike,
    /// Superscript.
    Superscript,
    /// Subscript.
    Subscript,
    /// Inline code or a monospace span.
    Code,
    /// A named character style (attr `style`).
    Style,
    /// A hyperlink (attrs `href`, `title`).
    Link,
    /// An inline image (attrs `src`, `alt`).
    Image,
    /// A line break.
    Linebreak,
    /// A tab.
    Tab,
    /// A field (attr `field`: PAGE, DATE, TOC...).
    Field,
    /// A footnote or endnote reference.
    Footnoteref,
    /// A placeholder (attr `name`).
    Placeholder,
    /// Anything else. An unknown type in a JSON file also reads as `Other`.
    #[serde(other)]
    Other,
}

impl CodeType {
    /// The JSON name (`"bold"`, `"link"`, ...).
    pub fn as_str(self) -> &'static str {
        match self {
            CodeType::Bold => "bold",
            CodeType::Italic => "italic",
            CodeType::Underline => "underline",
            CodeType::Strike => "strike",
            CodeType::Superscript => "superscript",
            CodeType::Subscript => "subscript",
            CodeType::Code => "code",
            CodeType::Style => "style",
            CodeType::Link => "link",
            CodeType::Image => "image",
            CodeType::Linebreak => "linebreak",
            CodeType::Tab => "tab",
            CodeType::Field => "field",
            CodeType::Footnoteref => "footnoteref",
            CodeType::Placeholder => "placeholder",
            CodeType::Other => "other",
        }
    }
    /// The type for a JSON name; an unknown name is `Other`.
    pub fn from_name(name: &str) -> CodeType {
        CodeType::ALL.iter().copied().find(|t| t.as_str() == name).unwrap_or(CodeType::Other)
    }
    /// Every type, in declaration order.
    pub const ALL: [CodeType; 16] = [
        CodeType::Bold,
        CodeType::Italic,
        CodeType::Underline,
        CodeType::Strike,
        CodeType::Superscript,
        CodeType::Subscript,
        CodeType::Code,
        CodeType::Style,
        CodeType::Link,
        CodeType::Image,
        CodeType::Linebreak,
        CodeType::Tab,
        CodeType::Field,
        CodeType::Footnoteref,
        CodeType::Placeholder,
        CodeType::Other,
    ];
    /// The `attrs` keys this type may carry: `href`, `title` (link); `field` (field); `src`, `alt` (image);
    /// `style` (style); `name` (placeholder); none for the others.
    pub fn attr_keys(self) -> &'static [&'static str] {
        match self {
            CodeType::Link => &["href", "title"],
            CodeType::Field => &["field"],
            CodeType::Image => &["src", "alt"],
            CodeType::Style => &["style"],
            CodeType::Placeholder => &["name"],
            _ => &[],
        }
    }
}

impl std::fmt::Display for CodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// The original markup of a unit's inline codes, by code id: a compact sorted map (see [`SmallMap`]).
/// The type of [`Unit::codes`]; what [`Unit::set_codes`] and [`Unit::with_codes`] take.
pub type Codes = SmallMap<CodeData>;

/// Where a segment is in its life, from untouched to signed off. Monolingual files only use `New`, `Draft`,
/// `Translated` and `Confirmed`; bilingual files and review workflows use the rest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SegState {
    /// No target yet.
    #[default]
    #[serde(rename = "new")]
    New,
    /// A target exists but nobody accepted it (machine output, TM match, work in progress).
    #[serde(rename = "draft")]
    Draft,
    /// The translator accepted the target.
    #[serde(rename = "translated")]
    Translated,
    /// Confirmed or approved by the translator (XLIFF 2 `final`, Trados `ApprovedTranslation`).
    #[serde(rename = "confirmed")]
    Confirmed,
    /// A reviewer checked it.
    #[serde(rename = "reviewed")]
    Reviewed,
    /// Final approval (XLIFF 1.2 `signed-off`).
    #[serde(rename = "signed-off")]
    SignedOff,
    /// A reviewer sent it back; it needs work again.
    #[serde(rename = "rejected")]
    Rejected,
    /// Frozen: nobody edits it (context match, project lock).
    #[serde(rename = "locked")]
    Locked,
}

impl SegState {
    /// Position in the life cycle, used when segments are joined (the joined segment is as far along as its
    /// least advanced part). `Rejected` ranks just above `New`: it has a target but needs work.
    pub fn rank(self) -> u8 {
        match self {
            SegState::New => 0,
            SegState::Rejected => 1,
            SegState::Draft => 2,
            SegState::Translated => 3,
            SegState::Confirmed => 4,
            SegState::Reviewed => 5,
            SegState::SignedOff => 6,
            SegState::Locked => 7,
        }
    }

    /// Inverse of [`SegState::rank`]; anything above the top rank is `Locked`.
    pub fn from_rank(r: u8) -> SegState {
        match r {
            0 => SegState::New,
            1 => SegState::Rejected,
            2 => SegState::Draft,
            3 => SegState::Translated,
            4 => SegState::Confirmed,
            5 => SegState::Reviewed,
            6 => SegState::SignedOff,
            _ => SegState::Locked,
        }
    }

    /// True when a person accepted the target (translated or further along).
    pub fn is_accepted(self) -> bool {
        self.rank() >= SegState::Translated.rank()
    }
}

/// Where a segment's target came from. Match with a wildcard arm: a 1.x release may add kinds (a JSON file with
/// a kind this release does not know reads as `Other`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum OriginKind {
    /// Translation memory.
    Tm,
    /// Machine translation engine.
    Mt,
    /// Language model translation or post-edit.
    Ai,
    /// Typed by a person.
    Human,
    /// Anything else; `Origin::name` says more. An unknown kind in a JSON file also reads as `Other`.
    #[default]
    #[serde(other)]
    Other,
}

/// How a target came to be, with the match percentage when it came from memory. Build it with [`Origin::new`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Origin {
    /// Memory, machine, AI, person or other.
    pub kind: OriginKind,
    /// Engine, memory or person: "Acme TM", "engine-2", "ana". Free text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Match percentage as the file states it (0 to 100; some tools write 101 for a context match).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub match_pct: Option<u8>,
}

impl Origin {
    /// An origin of this kind, with no name and no match percentage.
    pub fn new(kind: OriginKind) -> Origin {
        Origin { kind, name: None, match_pct: None }
    }
    /// Sets the match percentage.
    pub fn with_match(mut self, pct: u8) -> Origin {
        self.match_pct = Some(pct);
        self
    }
    /// Sets (or with `None` clears) the match percentage.
    pub fn with_match_pct(mut self, pct: Option<u8>) -> Origin {
        self.match_pct = pct;
        self
    }
    /// Sets (or with `None` clears) the engine, memory or person name.
    pub fn with_name(mut self, name: impl IntoOptionString) -> Origin {
        self.name = name.into_option_string();
        self
    }
}

/// One translatable piece of a unit: its source, and its target once there is one. Build it with
/// [`Segment::new`] and the `with_*` methods.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Segment {
    /// Unique within the document: `<unit id>` when the unit has one segment, else `<unit id>#<n>` (1-based).
    pub id: String,
    /// The source text with its inline codes.
    pub source: Vec<Inline>,
    /// Empty = untranslated, unless `empty_target` says the translation is empty on purpose (see below).
    #[serde(default)]
    pub target: Vec<Inline>,
    /// Where the segment is in its life (new, draft, translated, ...).
    #[serde(default)]
    pub state: SegState,
    /// Where the target came from. `None` for monolingual files and for targets nobody labelled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<Origin>,
    /// Translated to nothing on purpose. The empty-translation rule: a segment with an empty `target` is
    /// untranslated and the writer keeps its source text, unless `empty_target` is true; then the segment counts as
    /// translated ([`Segment::has_target`]) and the writer writes nothing for it. [`Document::set_target_empty`]
    /// sets it; [`Document::set_target`] with any target clears it. Left out of the JSON when false. (Localization
    /// OS writes an empty target as an empty value or paragraph: its adapter sets this flag for an empty LocOS
    /// target.)
    #[serde(default, skip_serializing_if = "is_false")]
    pub empty_target: bool,
}

impl Segment {
    /// An untranslated segment (no target, state `New`, no origin).
    pub fn new(id: impl Into<String>, source: Vec<Inline>) -> Segment {
        Segment { id: id.into(), source, target: vec![], state: SegState::New, origin: None, empty_target: false }
    }
    /// True when the segment is translated: it has a target, or it was translated to nothing on purpose.
    pub fn has_target(&self) -> bool {
        !self.target.is_empty() || self.empty_target
    }
    /// The text to write: the target, nothing for an empty translation, or the source when untranslated.
    pub fn output(&self) -> &[Inline] {
        if self.has_target() {
            &self.target
        } else {
            &self.source
        }
    }
    /// Marks (or unmarks) the segment as translated to nothing on purpose; marking clears the target.
    pub fn with_empty_target(mut self, empty_target: bool) -> Segment {
        self.empty_target = empty_target;
        if empty_target {
            self.target.clear();
        }
        self
    }
    /// Sets the target as it is (no tag check: use [`Document::set_target`] for edits).
    pub fn with_target(mut self, target: Vec<Inline>) -> Segment {
        self.target = target;
        self
    }
    /// Sets the state.
    pub fn with_state(mut self, state: SegState) -> Segment {
        self.state = state;
        self
    }
    /// Sets (or with `None` clears) where the target came from.
    pub fn with_origin(mut self, origin: impl Into<Option<Origin>>) -> Segment {
        self.origin = origin.into();
        self
    }
}

/// A comment on a unit: a translator query, a developer instruction, a reviewer remark. Build it with
/// [`Note::new`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Note {
    /// Who wrote it, when the file says.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub author: Option<String>,
    /// The comment.
    pub text: String,
    /// Kind as the file has it: "comment", "instruction", "developer", "reviewer", ... Free text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
}

impl Note {
    /// A note with this text, no author and no kind.
    pub fn new(text: impl Into<String>) -> Note {
        Note { author: None, text: text.into(), kind: None }
    }
    /// Sets (or with `None` clears) the author.
    pub fn with_author(mut self, author: impl IntoOptionString) -> Note {
        self.author = author.into_option_string();
        self
    }
    /// Sets (or clears) the kind ("comment", "developer", ...).
    pub fn with_kind(mut self, kind: impl IntoOptionString) -> Note {
        self.kind = kind.into_option_string();
        self
    }
}

/// What helps a translator, an AI lane or a preview understand a unit. Build it with [`UnitContext::new`] and the
/// `with_*` methods; `extra` takes anything without a field of its own.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UnitContext {
    /// What the unit is: "heading", "paragraph", "list-item", "cell", "button", "title", "alt", "key",
    /// "subtitle", ... Free text; parsers use the most specific word they know.
    pub kind: Option<String>,
    /// Style or element name as the file has it, e.g. "Heading 2", "h2", "td".
    pub style: Option<String>,
    /// Resource key or path for key/value formats (JSON pointer, PO msgctxt + msgid, subtitle number).
    pub key: Option<String>,
    /// Nearest heading text above the unit, when the format has headings.
    pub heading: Option<String>,
    /// Location in the original, readable: `word/document.xml paragraph 12`, `/html/body/main/p[3]`.
    pub location: Option<String>,
    /// Maximum length in characters, when the file states one.
    pub max_len: Option<usize>,
    /// Comments or notes attached in the file (translator comments, PO comments, DOCX comments).
    #[serde(default)]
    pub notes: Vec<String>,
    /// Subtitle timing and similar extras the editor may show.
    #[serde(default)]
    pub extra: TextMap,
    /// The unit's structural role in the document (a heading, a header, a speaker note, ...), when the format
    /// knows it. Left out of JSON when unset. Not filled under the `locos` profile.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<Role>,
    /// The outline level for [`Role::Heading`] (1 for a top-level heading). Left out of JSON when unset.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub level: Option<u8>,
}

/// A unit's structural role ([`UnitContext::role`]). JSON: the lowercase name. A value this release does not know
/// (written by a newer one) reads as [`Role::Other`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Role {
    /// A heading; [`UnitContext::level`] holds its level.
    Heading,
    /// The document title.
    Title,
    /// The document subtitle.
    Subtitle,
    /// A slide's title.
    SlideTitle,
    /// Page header text.
    Header,
    /// Page footer text.
    Footer,
    /// Footnote text.
    Footnote,
    /// Endnote text.
    Endnote,
    /// Speaker notes.
    Notes,
    /// Comment (annotation) text.
    Comment,
    /// A table or figure caption.
    Caption,
    /// A sheet's name.
    SheetName,
    /// A role from a newer release, or one without a name here.
    #[serde(other)]
    Other,
}

impl Role {
    /// The JSON name (`"heading"`, `"slidetitle"`, ...).
    pub fn as_str(self) -> &'static str {
        match self {
            Role::Heading => "heading",
            Role::Title => "title",
            Role::Subtitle => "subtitle",
            Role::SlideTitle => "slidetitle",
            Role::Header => "header",
            Role::Footer => "footer",
            Role::Footnote => "footnote",
            Role::Endnote => "endnote",
            Role::Notes => "notes",
            Role::Comment => "comment",
            Role::Caption => "caption",
            Role::SheetName => "sheetname",
            Role::Other => "other",
        }
    }
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl UnitContext {
    /// Sets (or with `None` clears) the structural role.
    pub fn with_role(mut self, role: impl Into<Option<Role>>) -> UnitContext {
        self.role = role.into();
        self
    }
    /// Sets (or clears) the heading level.
    pub fn with_level(mut self, level: impl Into<Option<u8>>) -> UnitContext {
        self.level = level.into();
        self
    }
    /// The structural role, when known.
    pub fn role(&self) -> Option<Role> {
        self.role
    }
    /// The heading level, when known.
    pub fn level(&self) -> Option<u8> {
        self.level
    }
    /// Sets (or with `None` clears) the structural role, in place.
    pub fn set_role(&mut self, role: impl Into<Option<Role>>) {
        self.role = role.into();
    }
    /// Sets (or clears) the heading level, in place.
    pub fn set_level(&mut self, level: impl Into<Option<u8>>) {
        self.level = level.into();
    }
    /// Sets the role [`Role::Heading`] and its level in one call (a level of 0 is stored as unset).
    pub fn set_heading_role(&mut self, level: u8) {
        self.role = Some(Role::Heading);
        self.level = (level > 0).then_some(level);
    }
}

impl UnitContext {
    /// An empty context.
    pub fn new() -> UnitContext {
        UnitContext::default()
    }
    /// Sets (or with `None` clears) what the unit is ("heading", "cell", ...).
    pub fn with_kind(mut self, kind: impl IntoOptionString) -> UnitContext {
        self.kind = kind.into_option_string();
        self
    }
    /// Sets (or clears) the style or element name.
    pub fn with_style(mut self, style: impl IntoOptionString) -> UnitContext {
        self.style = style.into_option_string();
        self
    }
    /// Sets (or clears) the resource key or path.
    pub fn with_key(mut self, key: impl IntoOptionString) -> UnitContext {
        self.key = key.into_option_string();
        self
    }
    /// Sets (or clears) the nearest heading.
    pub fn with_heading(mut self, heading: impl IntoOptionString) -> UnitContext {
        self.heading = heading.into_option_string();
        self
    }
    /// Sets (or clears) the readable location.
    pub fn with_location(mut self, location: impl IntoOptionString) -> UnitContext {
        self.location = location.into_option_string();
        self
    }
    /// Sets (or clears) the maximum length in characters.
    pub fn with_max_len(mut self, max_len: Option<usize>) -> UnitContext {
        self.max_len = max_len;
        self
    }
    /// Replaces the notes.
    pub fn with_notes(mut self, notes: Vec<String>) -> UnitContext {
        self.notes = notes;
        self
    }
    /// Replaces the extras (a [`TextMap`], or a `BTreeMap<String, String>`).
    pub fn with_extra(mut self, extra: impl Into<TextMap>) -> UnitContext {
        self.set_extra(extra);
        self
    }

    /// What the unit is ("heading", "cell", ...).
    pub fn kind(&self) -> Option<&str> {
        self.kind.as_deref()
    }
    /// The style or element name.
    pub fn style(&self) -> Option<&str> {
        self.style.as_deref()
    }
    /// The resource key or path.
    pub fn key(&self) -> Option<&str> {
        self.key.as_deref()
    }
    /// The nearest heading.
    pub fn heading(&self) -> Option<&str> {
        self.heading.as_deref()
    }
    /// The readable location.
    pub fn location(&self) -> Option<&str> {
        self.location.as_deref()
    }
    /// Sets (or with `None` clears) what the unit is, in place.
    pub fn set_kind(&mut self, kind: impl IntoOptionString) {
        self.kind = kind.into_option_string();
    }
    /// Sets (or clears) the style or element name, in place.
    pub fn set_style(&mut self, style: impl IntoOptionString) {
        self.style = style.into_option_string();
    }
    /// Sets (or clears) the resource key or path, in place.
    pub fn set_key(&mut self, key: impl IntoOptionString) {
        self.key = key.into_option_string();
    }
    /// Sets (or clears) the nearest heading, in place.
    pub fn set_heading(&mut self, heading: impl IntoOptionString) {
        self.heading = heading.into_option_string();
    }
    /// Sets (or clears) the readable location, in place.
    pub fn set_location(&mut self, location: impl IntoOptionString) {
        self.location = location.into_option_string();
    }

    /// The extras, read-only.
    pub fn extra(&self) -> &TextMap {
        &self.extra
    }
    /// The extras, to change in place.
    pub fn extra_mut(&mut self) -> &mut TextMap {
        &mut self.extra
    }
    /// Replaces the extras (a [`TextMap`], or a `BTreeMap<String, String>`).
    pub fn set_extra(&mut self, extra: impl Into<TextMap>) {
        self.extra = extra.into();
    }
}

/// One translatable unit of the file (a paragraph, a cell, a string): its segments, the original markup of its
/// inline codes and its context. Build it with [`Unit::new`] or [`Unit::from_segments`] and the `with_*` methods.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "UnitRepr")]
#[non_exhaustive]
pub struct Unit {
    /// Unique within the document, stable for the same input file.
    pub id: String,
    /// One segment before sentence splitting; one per sentence after.
    pub segments: Vec<Segment>,
    /// Text between segments as it appeared in the source (spaces after a full stop). Always
    /// `segments.len() - 1` entries; the writer joins segment targets with these.
    #[serde(default)]
    pub separators: Vec<String>,
    /// Original markup per inline code id.
    #[serde(default)]
    pub codes: Codes,
    /// What helps a translator understand the unit.
    #[serde(default)]
    pub context: UnitContext,
    /// False for content shown for context only (locked).
    #[serde(default = "yes")]
    pub translate: bool,
    /// Comments attached to the unit in a bilingual file. (Source files keep theirs in `context.notes`.)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<Note>,
    /// Why the unit is locked ("context match", "locked by the project"). Free text, usually with
    /// `translate == false`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lock_reason: Option<String>,
    /// What the parser needs to write the unit back and nobody else sees: the value as written in the file, its
    /// white space, a layout flag. Keyed by a short name the parser picks ("raw", "lead"). It is not an inline code,
    /// so editors, the LocOS token map and tag checks never see it. Left out of the JSON when empty. (Parsers used
    /// to keep this in `codes` under `~` names; such codes in an old JSON document are moved here when it is read.)
    #[serde(default, skip_serializing_if = "TextMap::is_empty")]
    pub hidden: TextMap,
    /// The plural group this unit is one form of (see [`PluralSlot`]); `None` for most units and under the LocOS
    /// profile. Left out of the JSON when unset. No writer reads it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub plural: Option<Box<PluralSlot>>,
}

fn yes() -> bool {
    true
}

/// The JSON form of a [`Unit`] as read: the same fields, with old `~` codes moved into `hidden`.
#[derive(Deserialize)]
struct UnitRepr {
    id: String,
    segments: Vec<Segment>,
    #[serde(default)]
    separators: Vec<String>,
    #[serde(default)]
    codes: Codes,
    #[serde(default)]
    context: UnitContext,
    #[serde(default = "yes")]
    translate: bool,
    #[serde(default)]
    notes: Vec<Note>,
    #[serde(default)]
    lock_reason: Option<String>,
    #[serde(default)]
    hidden: TextMap,
    #[serde(default)]
    plural: Option<Box<PluralSlot>>,
}

impl From<UnitRepr> for Unit {
    fn from(r: UnitRepr) -> Unit {
        let mut hidden = r.hidden;
        let mut codes = r.codes;
        // an old parser's hidden value: a `~` code with no open or close markup, whose `standalone` (or, for a
        // flag, `label`) held it. Most documents have none, so the map is only rebuilt when one is there.
        let old = |k: &str, c: &CodeData| k.starts_with('~') && c.open.is_none() && c.close.is_none();
        if codes.iter().any(|(k, c)| old(k, c)) {
            let mut kept = Vec::with_capacity(codes.len());
            for (k, c) in codes {
                if old(&k, &c) {
                    let name = &k[1..];
                    if !hidden.contains_key(name) {
                        hidden.insert(name, c.standalone.or(c.label).unwrap_or_default());
                    }
                } else {
                    kept.push((k, c));
                }
            }
            codes = kept.into_iter().collect();
        }
        Unit {
            id: r.id,
            segments: r.segments,
            separators: r.separators,
            codes,
            context: r.context,
            translate: r.translate,
            notes: r.notes,
            lock_reason: r.lock_reason,
            hidden,
            plural: r.plural,
        }
    }
}

impl Unit {
    /// A unit with one segment holding `source`.
    pub fn new(id: impl Into<String>, source: Vec<Inline>) -> Unit {
        let id = id.into();
        let seg = Segment::new(id.clone(), source);
        Unit::from_segments(id, vec![seg])
    }

    /// A unit made of the given segments (their ids are kept as they are). Separators start empty, one per gap.
    pub fn from_segments(id: impl Into<String>, segments: Vec<Segment>) -> Unit {
        Unit {
            separators: vec![String::new(); segments.len().saturating_sub(1)],
            segments,
            id: id.into(),
            codes: Codes::new(),
            context: UnitContext::default(),
            translate: true,
            notes: vec![],
            lock_reason: None,
            hidden: TextMap::new(),
            plural: None,
        }
    }
    /// A hidden value (see [`Unit::hidden`]).
    pub fn hidden(&self, key: &str) -> Option<&str> {
        self.hidden.get(key)
    }
    /// Sets a hidden value.
    pub fn set_hidden(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.hidden.insert(key.into(), value.into());
    }
    /// True when the hidden value is set (a flag).
    pub fn has_hidden(&self, key: &str) -> bool {
        self.hidden.contains_key(key)
    }
    /// Sets a hidden value (builder form of [`Unit::set_hidden`]).
    pub fn with_hidden(mut self, key: impl Into<String>, value: impl Into<String>) -> Unit {
        self.set_hidden(key, value);
        self
    }
    /// Every hidden value, read-only.
    pub fn hidden_map(&self) -> &TextMap {
        &self.hidden
    }
    /// Every hidden value, to change in place.
    pub fn hidden_map_mut(&mut self) -> &mut TextMap {
        &mut self.hidden
    }
    /// Replaces the hidden values (a [`TextMap`], or a `BTreeMap<String, String>`).
    pub fn set_hidden_map(&mut self, hidden: impl Into<TextMap>) {
        self.hidden = hidden.into();
    }

    /// The original markup of the inline codes, read-only. Prefer it to the field.
    pub fn codes(&self) -> &Codes {
        &self.codes
    }
    /// The original markup of the inline codes, to change in place (see [`Unit::codes`]).
    pub fn codes_mut(&mut self) -> &mut Codes {
        &mut self.codes
    }
    /// Replaces the original markup of the inline codes (a [`Codes`], or a `BTreeMap<String, CodeData>`).
    pub fn set_codes(&mut self, codes: impl Into<Codes>) {
        self.codes = codes.into();
    }
    /// Replaces the separators (one per gap between segments).
    pub fn with_separators(mut self, separators: Vec<String>) -> Unit {
        self.separators = separators;
        self
    }
    /// Replaces the original markup of the inline codes (a [`Codes`], or a `BTreeMap<String, CodeData>`).
    pub fn with_codes(mut self, codes: impl Into<Codes>) -> Unit {
        self.set_codes(codes);
        self
    }
    /// Adds (or replaces) the original markup of one inline code.
    pub fn with_code(mut self, id: impl Into<String>, code: CodeData) -> Unit {
        self.codes.insert(id.into(), code);
        self
    }
    /// Sets the context.
    pub fn with_context(mut self, context: UnitContext) -> Unit {
        self.context = context;
        self
    }
    /// Sets whether the unit is translatable (`false`: shown for context only).
    pub fn with_translate(mut self, translate: bool) -> Unit {
        self.translate = translate;
        self
    }
    /// Replaces the bilingual-file notes.
    pub fn with_notes(mut self, notes: Vec<Note>) -> Unit {
        self.notes = notes;
        self
    }
    /// Sets (or with `None` clears) why the unit is locked.
    pub fn with_lock_reason(mut self, reason: impl IntoOptionString) -> Unit {
        self.lock_reason = reason.into_option_string();
        self
    }

    /// The whole unit's source, segments joined with their separators.
    pub fn source(&self) -> Vec<Inline> {
        self.join(|s| &s.source)
    }

    /// The text to write for this unit: each segment's target, or its source when the segment is untranslated
    /// (as every CAT tool does), or nothing for a segment translated to empty ([`Segment::empty_target`]).
    pub fn output(&self) -> Vec<Inline> {
        self.join(|s| if s.has_target() { &s.target } else { &s.source })
    }

    /// True when some segment of a translatable unit is translated (has a target or an empty translation).
    pub fn is_translated(&self) -> bool {
        self.translate && self.segments.iter().any(Segment::has_target)
    }

    fn join<'a>(&'a self, pick: impl Fn(&'a Segment) -> &'a Vec<Inline>) -> Vec<Inline> {
        let mut out = Vec::new();
        for (i, seg) in self.segments.iter().enumerate() {
            if i > 0 {
                if let Some(sep) = self.separators.get(i - 1) {
                    if !sep.is_empty() {
                        out.push(Inline::Text(sep.clone()));
                    }
                }
            }
            out.extend(pick(seg).iter().cloned());
        }
        merge_text(out)
    }
}

// Unit model step 6: the plural slot.
impl Unit {
    /// The plural group this unit is one form of, if any.
    pub fn plural(&self) -> Option<&PluralSlot> {
        self.plural.as_deref()
    }
    /// The plural slot, to change in place.
    pub fn plural_mut(&mut self) -> Option<&mut PluralSlot> {
        self.plural.as_deref_mut()
    }
    /// Sets (or with `None` clears) the plural slot.
    pub fn set_plural(&mut self, plural: impl Into<Option<PluralSlot>>) {
        self.plural = plural.into().map(Box::new);
    }
    /// Sets the plural slot (builder form of [`Unit::set_plural`]).
    pub fn with_plural(mut self, plural: impl Into<Option<PluralSlot>>) -> Unit {
        self.set_plural(plural);
        self
    }
}

/// Joins neighbouring `Text` pieces.
pub fn merge_text(v: Vec<Inline>) -> Vec<Inline> {
    let mut out: Vec<Inline> = Vec::with_capacity(v.len());
    for i in v {
        match (out.last_mut(), i) {
            (Some(Inline::Text(a)), Inline::Text(b)) => a.push_str(&b),
            (_, Inline::Text(t)) if t.is_empty() => {}
            (_, x) => out.push(x),
        }
    }
    out
}

/// Plain text with the tags removed.
pub fn plain_text(v: &[Inline]) -> String {
    v.iter()
        .filter_map(|i| match i {
            Inline::Text(t) => Some(t.as_str()),
            _ => None,
        })
        .collect()
}

/// One piece of a text skeleton: literal output, or the place where a unit's text goes. Closed: anything else a
/// format needs lives in its own parts' text or in [`Unit::hidden`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SkelPart {
    /// Literal output.
    Raw(String),
    /// Index into `Document::units`. The parser decides how the unit's inlines are rendered (escaping, codes).
    Unit(usize),
}

/// The content of one zip entry in a skeleton. Closed: copied bytes or rebuilt text.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ZipData {
    /// Copied unchanged. In JSON this is a base64 string; the old array-of-integers form still reads.
    Raw(#[serde(with = "b64")] Vec<u8>),
    /// A text entry rebuilt from parts.
    Parts(Vec<SkelPart>),
}

/// One entry of a zip skeleton, in the archive's order. Build it with [`ZipEntry::new`] or [`ZipEntry::raw`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ZipEntry {
    /// The member name in the archive.
    pub name: String,
    /// Its content.
    pub data: ZipData,
}

impl ZipEntry {
    /// An entry with this name and content.
    pub fn new(name: impl Into<String>, data: ZipData) -> ZipEntry {
        ZipEntry { name: name.into(), data }
    }
    /// An entry copied unchanged.
    pub fn raw(name: impl Into<String>, bytes: Vec<u8>) -> ZipEntry {
        ZipEntry::new(name, ZipData::Raw(bytes))
    }
}

/// Which parser made a skeleton: the crate name, its version (for information) and the skeleton schema number.
/// The schema is an integer each format crate bumps only when its skeleton layout changes, so a new crate release
/// that did not touch the layout still merges old skeletons (see [`check_parser`]).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ParserInfo {
    /// The producing crate, e.g. "bilingua-docx".
    pub name: String,
    /// Its semver version, e.g. "0.1.0". Informational only; never compared.
    pub version: String,
    /// The skeleton schema number. Absent in stamps written before schemas existed, which read as schema 1.
    #[serde(default = "legacy_schema")]
    pub schema: u32,
}

fn legacy_schema() -> u32 {
    1
}

impl ParserInfo {
    /// A stamp for this crate name, version and skeleton schema (usually written `parser_info!()`).
    pub fn new(name: impl Into<String>, version: impl Into<String>, schema: u32) -> ParserInfo {
        ParserInfo { name: name.into(), version: version.into(), schema }
    }

    /// True when a skeleton made by `self` can be read by `current`: same crate and a schema no newer than the
    /// current one.
    pub fn compatible_with(&self, current: &ParserInfo) -> bool {
        self.name == current.name && self.schema <= current.schema
    }
}

/// The producing parser of the running crate: `parser_info!()` expands to a [`ParserInfo`] with the calling
/// crate's name, version and `crate::SKELETON_SCHEMA`. Every format crate defines that constant
/// (`pub const SKELETON_SCHEMA: u32 = 1;`) and bumps it only when its skeleton changes.
/// `parser_info!(n)` stamps an explicit schema instead.
// `crate::SKELETON_SCHEMA` must resolve in the calling crate: that is the point.
#[allow(clippy::crate_in_macro_def)]
#[macro_export]
macro_rules! parser_info {
    () => {
        $crate::parser_info!(crate::SKELETON_SCHEMA)
    };
    ($schema:expr) => {
        $crate::ParserInfo::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), $schema)
    };
}

/// Refuses a document whose skeleton came from another crate or from a newer schema than `current`. Older
/// schemas and skeletons with no record (written before stamps existed) are accepted; a crate that stops
/// reading some old schema uses [`check_parser_min`].
pub fn check_parser(doc: &Document, current: &ParserInfo) -> Result<(), Error> {
    check_parser_min(doc, current, 1)
}

/// Like [`check_parser`], but also refuses schemas older than `oldest`, the oldest one the crate still reads.
pub fn check_parser_min(doc: &Document, current: &ParserInfo, oldest: u32) -> Result<(), Error> {
    match doc.skeleton.parser() {
        Some(found) if !found.compatible_with(current) || found.schema < oldest => Err(Error::ParserVersion {
            found: format!("{} {} (skeleton schema {})", found.name, found.version, found.schema),
            expected: if found.name != current.name {
                current.name.clone()
            } else if found.schema > current.schema {
                format!("{} {} (reads skeleton schema {} and older)", current.name, current.version, current.schema)
            } else {
                format!("{} {} (reads skeleton schema {} to {})", current.name, current.version, oldest, current.schema)
            },
        }),
        _ => Ok(()),
    }
}

/// Everything in the file that is not translatable text. New kinds of skeleton come as new variants (match with a
/// wildcard arm); the fields of each variant are fixed for 1.x (a format records more in its parts, in the
/// `encoding` tag's `;name=value` items, or in [`Unit::hidden`]).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Skeleton {
    /// A text file: parts in order. `encoding` is what the writer must produce ("utf-8", "utf-8-bom", ...).
    Text {
        /// The parts in order.
        parts: Vec<SkelPart>,
        /// The encoding tag (the envelope of [`enc::decode_text`]: `"utf-8"`, `"windows-1252;nl=crlf"`, ...).
        encoding: String,
        /// The parser that made it.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        parser: Option<ParserInfo>,
    },
    /// A zip container (DOCX, XLSX, PPTX, ODF, IDML): entries in their original order.
    Zip {
        /// The entries in archive order.
        entries: Vec<ZipEntry>,
        /// The parser that made it.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        parser: Option<ParserInfo>,
    },
    /// A binary file kept whole (a compiled MO catalog): the writer rebuilds the file from these bytes and the
    /// units. In JSON the bytes are a base64 string.
    Binary {
        /// The original file.
        #[serde(with = "b64")]
        data: Vec<u8>,
        /// The parser that made it.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        parser: Option<ParserInfo>,
    },
}

impl Default for Skeleton {
    /// An empty UTF-8 text skeleton.
    fn default() -> Self {
        Skeleton::Text { parts: Vec::new(), encoding: "utf-8".into(), parser: None }
    }
}

impl Skeleton {
    /// A text skeleton stamped with the producing parser.
    pub fn text(parts: Vec<SkelPart>, encoding: impl Into<String>, parser: ParserInfo) -> Skeleton {
        Skeleton::Text { parts, encoding: encoding.into(), parser: Some(parser) }
    }
    /// A zip skeleton stamped with the producing parser.
    pub fn zip(entries: Vec<ZipEntry>, parser: ParserInfo) -> Skeleton {
        Skeleton::Zip { entries, parser: Some(parser) }
    }
    /// A binary skeleton stamped with the producing parser.
    pub fn binary(data: Vec<u8>, parser: ParserInfo) -> Skeleton {
        Skeleton::Binary { data, parser: Some(parser) }
    }
    /// The parser that made this skeleton, if recorded.
    pub fn parser(&self) -> Option<&ParserInfo> {
        match self {
            Skeleton::Text { parser, .. } | Skeleton::Zip { parser, .. } | Skeleton::Binary { parser, .. } => parser.as_ref(),
        }
    }
}

/// How finely units are split into segments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Granularity {
    /// SRX sentence segmentation (the classic CAT default).
    #[default]
    Sentence,
    /// One segment per unit (paragraph, cell, string).
    Paragraph,
    /// One segment per unit, flagged so AI lanes translate the whole document in one pass. The editor still
    /// edits per unit.
    Document,
}

/// An extracted file: its units and its skeleton. Build it with [`Document::new`] and the `with_*` methods; edit
/// targets with [`Document::set_target`] and friends, which check the tags. `Default` is an empty document of no
/// format (no writer accepts it).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Document {
    /// Parser id, e.g. "html", "docx", "json".
    pub format: String,
    /// Original file name, when known.
    pub name: Option<String>,
    /// The source language (a BCP 47 tag, `en`, `de-DE`), when known.
    pub source_lang: Option<String>,
    /// The target language, when known.
    pub target_lang: Option<String>,
    /// How finely the units are segmented.
    pub granularity: Granularity,
    /// The translatable units, in file order.
    pub units: Vec<Unit>,
    /// Everything else in the file.
    pub skeleton: Skeleton,
    /// Things the reader noticed but did not refuse the file for (text that looks garbled in the code page it was
    /// read with, ...). Left out of the JSON when empty.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<Warning>,
    /// The encoding a text file was decoded from when it was not UTF-8 (`utf-16le`, `windows-1252`, `shift_jis`,
    /// ...); `None` for UTF-8 (with or without BOM) and for zip and binary files. The `bilingua` facade fills it
    /// from the skeleton; left out of the JSON when unset.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_encoding: Option<String>,
    /// Segment id to position, built on the first [`Document::set_target`] and checked on every use.
    #[serde(skip)]
    seg_index: SegIndex,
}

/// The lookup behind [`Document::set_target`]. It is a cache: it never takes part in equality, serialisation or
/// the document's identity, and a stale entry is detected and rebuilt, never trusted.
#[derive(Default)]
struct SegIndex(Option<std::collections::HashMap<String, (usize, usize)>>);

/// A clone starts without the cache (it is rebuilt on its first use), so cloning a document costs no more than
/// before the index existed.
impl Clone for SegIndex {
    fn clone(&self) -> SegIndex {
        SegIndex(None)
    }
}

/// The cache never shows in a document's `Debug` output beyond its name, so the output does not change with it.
impl std::fmt::Debug for SegIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("SegIndex")
    }
}

impl PartialEq for SegIndex {
    fn eq(&self, _: &SegIndex) -> bool {
        true
    }
}

impl Eq for SegIndex {}

// Unit model step 6: plural categories of the target.
impl Document {
    /// The categories the target language uses for `kind`, in CLDR order and always with `other` (`one`, `few`,
    /// `many`, `other` for Polish cardinals), read from `target_lang` through the shared CLDR table (the Babel data
    /// Localization OS uses; aliases such as `deu` and `iw` resolve). `None` for a select, without a target
    /// language, or for one the table does not know. Units hold no copy of them.
    pub fn plural_categories(&self, kind: PluralKind) -> Option<Vec<&'static str>> {
        plural::categories(self.target_lang.as_deref(), kind)
    }
}

/// A notice about a file that was read anyway: it never stops a merge. `kind` is a stable word for programs
/// ("text_encoding"); `message` is plain text safe to show the person who uploaded the file.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Warning {
    /// A stable word for programs (`text_encoding`).
    pub kind: String,
    /// Which part of the file the notice is about ("text encoding", an entry name, ...).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub part: String,
    /// Plain text for the person who uploaded the file.
    pub message: String,
}

impl Warning {
    /// A warning of this kind about this part.
    pub fn new(kind: impl Into<String>, part: impl Into<String>, message: impl Into<String>) -> Warning {
        Warning { kind: kind.into(), part: part.into(), message: message.into() }
    }
}

impl Document {
    /// A document of `format` (the filter id) with its units and skeleton; no name, no languages, sentence
    /// granularity, no warnings.
    pub fn new(format: impl Into<String>, units: Vec<Unit>, skeleton: Skeleton) -> Document {
        Document { format: format.into(), name: None, source_lang: None, target_lang: None, granularity: Granularity::default(), units, skeleton, warnings: vec![], source_encoding: None, seg_index: SegIndex::default() }
    }
    /// Takes the languages and the granularity from the options the file was opened with (what every parser does).
    pub fn with_options(mut self, opts: &ExtractOptions) -> Document {
        self.source_lang = opts.source_lang.clone();
        self.target_lang = opts.target_lang.clone();
        self.granularity = opts.granularity;
        self
    }
    /// Sets (or with `None` clears) the file name.
    pub fn with_name(mut self, name: impl IntoOptionString) -> Document {
        self.name = name.into_option_string();
        self
    }
    /// Sets (or clears) the source language.
    pub fn with_source_lang(mut self, lang: impl IntoOptionString) -> Document {
        self.source_lang = lang.into_option_string();
        self
    }
    /// Sets (or clears) the target language.
    pub fn with_target_lang(mut self, lang: impl IntoOptionString) -> Document {
        self.target_lang = lang.into_option_string();
        self
    }
    /// Sets the granularity.
    pub fn with_granularity(mut self, granularity: Granularity) -> Document {
        self.granularity = granularity;
        self
    }
    /// The encoding of a text skeleton (or of a LocOS-profile markdown, po or subtitle skeleton), when it is not
    /// UTF-8: the base of its tag (`utf-16le-bom` and `utf-16le`
    /// both give `utf-16le`). `None` for UTF-8, binary plists, zip and binary skeletons.
    pub fn detected_source_encoding(&self) -> Option<String> {
        let encoding = match &self.skeleton {
            Skeleton::Text { encoding, .. } => encoding.as_str(),
            // the LocOS-profile text formats keep the decoded file under a first line with the skeleton tag
            Skeleton::Binary { data, .. } if matches!(self.format.as_str(), "markdown" | "po" | "srt" | "vtt") => enc::locos_skeleton_parts(data)?.0,
            _ => return None,
        };
        let base = encoding.split(';').next().unwrap_or("").to_ascii_lowercase();
        let base = base.strip_suffix("-bom").or_else(|| base.strip_suffix("-nobom")).unwrap_or(&base).to_string();
        (!matches!(base.as_str(), "" | "utf-8" | "utf8" | "bplist")).then_some(base)
    }
    /// Replaces the warnings.
    pub fn with_warnings(mut self, warnings: Vec<Warning>) -> Document {
        self.warnings = warnings;
        self
    }

    /// Every segment with its unit, in order.
    pub fn segments(&self) -> impl Iterator<Item = (&Unit, &Segment)> {
        self.units.iter().flat_map(|u| u.segments.iter().map(move |s| (u, s)))
    }

    /// The number of segments.
    pub fn segment_count(&self) -> usize {
        self.units.iter().map(|u| u.segments.len()).sum()
    }

    /// The segment with this id and its unit.
    pub fn find(&self, seg_id: &str) -> Option<(&Unit, &Segment)> {
        self.segments().find(|(_, s)| s.id == seg_id)
    }

    /// Sets a segment's target. Rejects unknown segments, untranslatable units, segments in state
    /// [`SegState::Locked`], and tags the source lacks or repeats. A missing tag is allowed (the writer leaves it out), as in catcore.
    ///
    /// The first call builds an index of segment ids (one pass over the document); later calls find the segment
    /// through it, so setting every segment of a 12 000 unit file is linear, not quadratic. The index is checked
    /// on each use and rebuilt when the units were edited since (a segment added, removed or renamed through the
    /// public fields); for many segments at once [`Document::set_targets`] is still the lighter call. When two
    /// segments share an id, the first one in the document wins as long as the units are not edited between
    /// calls.
    pub fn set_target(&mut self, seg_id: &str, target: Vec<Inline>) -> Result<(), Error> {
        let at = self.locate(seg_id).ok_or_else(|| Error::UnknownSegment(seg_id.to_string()))?;
        self.apply_target(at, seg_id, target, false)
    }

    /// [`Document::set_target`] that lets a translation repeat a standalone placeholder tag (the `#` of an ICU
    /// plural, Chrome `$x$`, go `{{..}}`), as Localization OS does; paired tags still keep the exactly-once rule.
    pub fn set_target_repeating(&mut self, seg_id: &str, target: Vec<Inline>) -> Result<(), Error> {
        let at = self.locate(seg_id).ok_or_else(|| Error::UnknownSegment(seg_id.to_string()))?;
        self.apply_target(at, seg_id, target, true)
    }

    /// Position (unit, segment) of the segment with this id, through the cached index.
    pub(crate) fn locate(&mut self, seg_id: &str) -> Option<(usize, usize)> {
        let valid = |units: &[Unit], (ui, si): (usize, usize)| units.get(ui).and_then(|u| u.segments.get(si)).is_some_and(|s| s.id == seg_id);
        if let Some(at) = self.seg_index.0.as_ref().and_then(|m| m.get(seg_id)).copied() {
            if valid(&self.units, at) {
                return Some(at);
            }
        }
        // A miss or a stale hit: the index may be out of date, so rebuild it once and look again.
        let fresh = self.segment_index();
        let at = fresh.get(seg_id).copied();
        self.seg_index = SegIndex(Some(fresh));
        at
    }

    /// Sets many targets in one pass: one id index is built for the whole call, so 100 000 segments take
    /// milliseconds where 100 000 [`Document::set_target`] calls scan the document each time. Items are applied in
    /// order with the same rules as `set_target`; the first error stops the call and is returned, items before it
    /// stay applied. When an id appears more than once, the segment is set by each item in turn (the last wins).
    pub fn set_targets<I, S>(&mut self, items: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = (S, Vec<Inline>)>,
        S: AsRef<str>,
    {
        let index = self.segment_index();
        for (id, target) in items {
            let id = id.as_ref();
            let at = *index.get(id).ok_or_else(|| Error::UnknownSegment(id.to_string()))?;
            self.apply_target(at, id, target, false)?;
        }
        Ok(())
    }

    /// Sets many states in one pass (see [`Document::set_targets`]).
    pub fn set_states<I, S>(&mut self, items: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = (S, SegState)>,
        S: AsRef<str>,
    {
        let index = self.segment_index();
        for (id, state) in items {
            let id = id.as_ref();
            let (ui, si) = *index.get(id).ok_or_else(|| Error::UnknownSegment(id.to_string()))?;
            self.units[ui].segments[si].state = state;
        }
        Ok(())
    }

    /// Segment id to (unit index, segment index); the first segment with an id wins, as in `set_target`.
    fn segment_index(&self) -> std::collections::HashMap<String, (usize, usize)> {
        let mut m = std::collections::HashMap::with_capacity(self.segment_count());
        for (ui, u) in self.units.iter().enumerate() {
            for (si, s) in u.segments.iter().enumerate() {
                m.entry(s.id.clone()).or_insert((ui, si));
            }
        }
        m
    }

    fn apply_target(&mut self, (ui, si): (usize, usize), seg_id: &str, target: Vec<Inline>, repeat_standalone: bool) -> Result<(), Error> {
        let unit = &mut self.units[ui];
        if !unit.translate {
            return Err(Error::Locked(seg_id.to_string()));
        }
        let seg = &mut unit.segments[si];
        if seg.state == SegState::Locked {
            return Err(Error::Locked(seg_id.to_string()));
        }
        check_tags(&seg.source, &target, repeat_standalone)?;
        // The free flag belongs to the source code: a target tag takes it from the source tag with its id, so a
        // caller (an editor, a LocOS adapter) that does not know the flag cannot drop or invent it.
        let target = merge_text(
            target
                .into_iter()
                .map(|i| match i {
                    Inline::Tag { id, kind, display, .. } => {
                        let free = kind == TagKind::Standalone
                            && seg.source.iter().any(|s| matches!(s, Inline::Tag { id: sid, kind: TagKind::Standalone, free: true, .. } if *sid == id));
                        Inline::Tag { id, kind, display, free }
                    }
                    t => t,
                })
                .collect(),
        );
        let empty = plain_text(&target).trim().is_empty() && !target.iter().any(|i| matches!(i, Inline::Tag { .. }));
        seg.target = if empty { vec![] } else { target };
        seg.empty_target = false;
        seg.state = if seg.target.is_empty() { SegState::New } else { SegState::Draft };
        Ok(())
    }

    /// Marks a segment as translated to nothing on purpose (see [`Segment::empty_target`]): the writer writes an
    /// empty value for it. State `Draft`, as for any new target. Rejects unknown segments, untranslatable units
    /// and locked segments.
    pub fn set_target_empty(&mut self, seg_id: &str) -> Result<(), Error> {
        let (ui, si) = self.locate(seg_id).ok_or_else(|| Error::UnknownSegment(seg_id.to_string()))?;
        let unit = &mut self.units[ui];
        if !unit.translate {
            return Err(Error::Locked(seg_id.to_string()));
        }
        let seg = &mut unit.segments[si];
        if seg.state == SegState::Locked {
            return Err(Error::Locked(seg_id.to_string()));
        }
        seg.target.clear();
        seg.empty_target = true;
        seg.state = SegState::Draft;
        Ok(())
    }

    /// Sets a segment's state (for many, [`Document::set_states`]).
    ///
    /// Like `set_target`, the first segment with the id wins as long as the units are not edited between calls.
    pub fn set_state(&mut self, seg_id: &str, state: SegState) -> Result<(), Error> {
        // through the cached index, as set_target: a scan per call made a whole-document pass quadratic (BILI-112)
        let (ui, si) = self.locate(seg_id).ok_or_else(|| Error::UnknownSegment(seg_id.to_string()))?;
        self.units[ui].segments[si].state = state;
        Ok(())
    }
}

/// The merge-time tag check ([`guard::check_targets`]). Each target tag must be a tag of the segment's source (same
/// id and kind) or have its markup in the unit's `codes` (a reader that maps a stored translation's placeholders to
/// the unit's codes, as PO and MO do for a plural form, gives such tags). With `counts`, a paired tag of the
/// source may appear no more often than there.
pub(crate) fn check_target_tags(source: &[Inline], target: &[Inline], codes: &Codes, counts: bool) -> Result<(), Error> {
    let mut allowed: BTreeMap<(&str, TagKind), usize> = BTreeMap::new();
    for i in source {
        if let Inline::Tag { id, kind, .. } = i {
            *allowed.entry((id.as_str(), *kind)).or_default() += 1;
        }
    }
    let mut used: BTreeMap<(&str, TagKind), usize> = BTreeMap::new();
    for i in target {
        if let Inline::Tag { id, kind, .. } = i {
            match allowed.get(&(id.as_str(), *kind)) {
                Some(max) => {
                    let n = used.entry((id.as_str(), *kind)).or_default();
                    *n += 1;
                    if counts && *n > *max && *kind != TagKind::Standalone {
                        return Err(Error::DuplicatedTag(id.clone()));
                    }
                }
                None if codes.contains_key(id) => {}
                None => return Err(Error::InventedTag(id.clone())),
            }
        }
    }
    Ok(())
}

fn check_tags(source: &[Inline], target: &[Inline], repeat_standalone: bool) -> Result<(), Error> {
    let mut allowed: BTreeMap<(&str, TagKind), usize> = BTreeMap::new();
    for i in source {
        if let Inline::Tag { id, kind, .. } = i {
            *allowed.entry((id.as_str(), *kind)).or_default() += 1;
        }
    }
    let mut used: BTreeMap<(&str, TagKind), usize> = BTreeMap::new();
    for i in target {
        if let Inline::Tag { id, kind, .. } = i {
            let n = used.entry((id.as_str(), *kind)).or_default();
            *n += 1;
            match allowed.get(&(id.as_str(), *kind)) {
                None => return Err(Error::InventedTag(id.clone())),
                Some(max) if *n > *max && !(repeat_standalone && *kind == TagKind::Standalone) => return Err(Error::DuplicatedTag(id.clone())),
                _ => {}
            }
        }
    }
    Ok(())
}

/// Options for [`Filter::extract`]. Build them with [`ExtractOptions::new`] and the `with_*` methods
/// (`ExtractOptions::new().with_target_lang("de-DE")`); new options are added without breaking callers.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ExtractOptions {
    /// The source language (a BCP 47 tag, `en`, `de-DE`), when known.
    pub source_lang: Option<String>,
    /// The target language, when known.
    pub target_lang: Option<String>,
    /// Parsers always extract one segment per unit; `bilingua-seg` applies the granularity afterwards.
    pub granularity: Granularity,
    /// Extract text the format marks as not translatable as locked units (for context), instead of skipping it.
    pub include_locked: bool,
    /// Safety limits for this call; `None` uses [`Limits::default`] (Localization OS's numbers).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limits: Option<Limits>,
    /// Options only one format knows, as `"<format>.<name>"` to a text value (`"ftl.translate_terms"` to
    /// `"true"`). Each filter declares its keys and their kind ([`Filter::format_option_keys`]); the `bilingua`
    /// facade refuses a key no filter declares, or a value of the wrong kind, with [`Error::FormatOption`].
    /// Booleans are `true`/`false`/`1`/`0`/`yes`/`no`/`on`/`off`; integers are decimal.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub format_options: BTreeMap<String, String>,
    /// A behaviour profile ([`profile`]): `Some("locos")` for the Localization OS adapter. `None`: Bilingua's own
    /// behaviour. The `bilingua` facade refuses a name it does not know.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,
}

impl ExtractOptions {
    /// Switches a profile on (`"locos"`), or off with `None`.
    pub fn with_profile(mut self, profile: impl IntoOptionString) -> ExtractOptions {
        self.profile = profile.into_option_string();
        self
    }
    /// True when this profile is on.
    pub fn is_profile(&self, name: &str) -> bool {
        self.profile.as_deref() == Some(name)
    }
    /// The defaults: no languages, sentence granularity, locked text skipped, Localization OS's limits.
    pub fn new() -> ExtractOptions {
        ExtractOptions::default()
    }
    /// Sets (or with `None` clears) the source language; it picks the sentence rules.
    pub fn with_source_lang(mut self, lang: impl IntoOptionString) -> ExtractOptions {
        self.source_lang = lang.into_option_string();
        self
    }
    /// Sets (or clears) the target language.
    pub fn with_target_lang(mut self, lang: impl IntoOptionString) -> ExtractOptions {
        self.target_lang = lang.into_option_string();
        self
    }
    /// Sets the granularity.
    pub fn with_granularity(mut self, granularity: Granularity) -> ExtractOptions {
        self.granularity = granularity;
        self
    }
    /// Extracts locked text as locked units, for context.
    pub fn with_include_locked(mut self, include_locked: bool) -> ExtractOptions {
        self.include_locked = include_locked;
        self
    }
    /// Limits for this call (`Limits` or `Option<Limits>`; `None` is the defaults).
    pub fn with_limits(mut self, limits: impl Into<Option<Limits>>) -> ExtractOptions {
        self.limits = limits.into();
        self
    }
    /// Sets one format option (`"<format>.<name>"`); the value is text (`true`, `42`, `name=Name`).
    pub fn with_format_option(mut self, key: impl Into<String>, value: impl ToString) -> ExtractOptions {
        self.format_options.insert(key.into(), value.to_string());
        self
    }
    /// Replaces all format options.
    pub fn with_format_options(mut self, options: BTreeMap<String, String>) -> ExtractOptions {
        self.format_options = options;
        self
    }

    /// A format option as text, if set.
    pub fn format_option(&self, key: &str) -> Option<&str> {
        self.format_options.get(key).map(String::as_str)
    }

    /// A format option as a boolean (`true`, `1`, `yes`, `on` are true; `false`, `0`, `no`, `off` are false; any
    /// other text or no entry gives `None`).
    pub fn format_flag(&self, key: &str) -> Option<bool> {
        match self.format_option(key)?.trim().to_ascii_lowercase().as_str() {
            "true" | "1" | "yes" | "on" => Some(true),
            "false" | "0" | "no" | "off" => Some(false),
            _ => None,
        }
    }

    /// A format option as an integer (`None` when unset or not a decimal integer).
    pub fn format_int(&self, key: &str) -> Option<i64> {
        self.format_option(key)?.trim().parse().ok()
    }

    /// The limits in force: the caller's, or the defaults.
    pub fn limits(&self) -> Limits {
        // clamped, so a caller's huge number (2^62 units from a CLI flag or a Python dict) cannot overflow the
        // budgets formats derive from a limit (`40 * max_units`)
        self.limits.unwrap_or_default().clamped()
    }
}

/// The kind of value a format option takes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OptionKind {
    /// `true`/`false` (also `1`/`0`, `yes`/`no`, `on`/`off`).
    Bool,
    /// A decimal integer.
    Int,
    /// Any text.
    Text,
}

/// One option a format understands in `ExtractOptions::format_options`: its full key (`"csv.columns"`), the kind
/// of value, and a line of help for tools that list the options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct FormatOptionKey {
    /// The full key, `<family>.<name>`.
    pub key: &'static str,
    /// The kind of value.
    pub kind: OptionKind,
    /// One line of help.
    pub help: &'static str,
}

impl FormatOptionKey {
    /// A key of this kind.
    pub const fn new(key: &'static str, kind: OptionKind, help: &'static str) -> FormatOptionKey {
        FormatOptionKey { key, kind, help }
    }
    /// A boolean key.
    pub const fn bool(key: &'static str, help: &'static str) -> FormatOptionKey {
        FormatOptionKey::new(key, OptionKind::Bool, help)
    }
    /// An integer key.
    pub const fn int(key: &'static str, help: &'static str) -> FormatOptionKey {
        FormatOptionKey::new(key, OptionKind::Int, help)
    }
    /// A text key.
    pub const fn text(key: &'static str, help: &'static str) -> FormatOptionKey {
        FormatOptionKey::new(key, OptionKind::Text, help)
    }

    /// Refuses a value that is not of this key's kind.
    pub fn check(&self, value: &str) -> Result<(), Error> {
        let v = value.trim();
        let ok = match self.kind {
            OptionKind::Bool => matches!(v.to_ascii_lowercase().as_str(), "true" | "1" | "yes" | "on" | "false" | "0" | "no" | "off"),
            OptionKind::Int => v.parse::<i64>().is_ok(),
            OptionKind::Text => true,
        };
        if ok {
            Ok(())
        } else {
            let want = match self.kind {
                OptionKind::Bool => "true or false",
                OptionKind::Int => "an integer",
                OptionKind::Text => "text",
            };
            Err(Error::FormatOption { key: self.key.to_string(), msg: format!("the value {value:?} is not {want}") })
        }
    }
}

/// Checks every `format_options` entry against the keys the filters declare: an unknown key or a value of the
/// wrong kind is an [`Error::FormatOption`]. The `bilingua` facade calls this with every filter's keys, so an
/// option meant for another format (the adapter sends `docx.translate_comments` with every file) is accepted.
pub fn check_format_options(opts: &ExtractOptions, known: &[FormatOptionKey]) -> Result<(), Error> {
    check_option_map(&opts.format_options, known)
}

/// [`check_format_options`] for any map of options (`MergeOptions::format_options` too).
pub fn check_option_map(options: &BTreeMap<String, String>, known: &[FormatOptionKey]) -> Result<(), Error> {
    for (k, v) in options {
        match known.iter().find(|f| f.key == k) {
            Some(f) => f.check(v)?,
            None => {
                let family = k.split('.').next().unwrap_or("");
                let same: Vec<&str> = known.iter().filter(|f| f.key.split('.').next() == Some(family)).map(|f| f.key).collect();
                let msg = if same.is_empty() { "no format has this option".to_string() } else { format!("unknown option; {family} knows {}", same.join(", ")) };
                return Err(Error::FormatOption { key: k.clone(), msg });
            }
        }
    }
    Ok(())
}

/// The check every filter makes when it is called directly (not through the `bilingua` facade), first thing in
/// `extract` (with `&opts.format_options`) and in `merge_with` (with its `MergeOptions::format_options`): an option
/// in one of the filter's own families (its id, and the `<family>.` prefix of every key it declares) that it does
/// not declare is refused (a misspelt `csv.colums` would otherwise be ignored), and so is a value of the wrong kind
/// for a key it declares. Keys of other formats are left alone: one option map may be meant for many formats, and
/// only the facade, which knows every filter's keys, can tell a good foreign key from a misspelt one.
pub fn check_own_options<F: Filter + ?Sized>(filter: &F, options: &BTreeMap<String, String>) -> Result<(), Error> {
    if options.is_empty() {
        return Ok(());
    }
    let keys = filter.format_option_keys();
    let id = filter.id();
    let family = |k: &str| k.split('.').next().unwrap_or("").to_string();
    for (k, v) in options {
        let fam = family(k);
        if fam != id && !keys.iter().any(|f| family(f.key) == fam) {
            continue;
        }
        match keys.iter().find(|f| f.key == k) {
            Some(f) => f.check(v)?,
            None => {
                let mine: Vec<&str> = keys.iter().map(|f| f.key).collect();
                let msg = if mine.is_empty() { format!("{id} has no options") } else { format!("unknown option; {id} knows {}", mine.join(", ")) };
                return Err(Error::FormatOption { key: k.clone(), msg });
            }
        }
    }
    Ok(())
}

/// How a merge writes text files.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum OutputEncoding {
    /// The file's own encoding, BOM and line endings (the default; an untouched merge is byte-identical).
    #[default]
    #[serde(rename = "keep")]
    Keep,
    /// Always UTF-8 (Localization OS behaviour). An XML, HTML or PO charset declaration is rewritten to say UTF-8,
    /// so the file stays truthful; a UTF-8 BOM the file had is kept, a UTF-16 BOM is dropped.
    #[serde(rename = "utf-8", alias = "utf8")]
    Utf8,
}

/// Options for [`Filter::merge_with`]. The default is what [`Filter::merge`] does. They live here, once, and
/// every format that stamps a language, fills plurals or writes text honours them:
///
/// - `output_encoding`: [`OutputEncoding::Keep`] or [`OutputEncoding::Utf8`] (through `enc::encode_text_with`, so
///   every text format that keeps its encoding in the skeleton gets it).
/// - `stamp_target_always`: by default a merge in which nothing is translated returns the original bytes (no target
///   language stamp, no plural fill, no format conversion). With `true` the target language is stamped and
///   missing plural forms are filled even then, as Localization OS does. It only has an effect when
///   `Document::target_lang` is set.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct MergeOptions {
    /// Keep the file's encoding, or write UTF-8.
    pub output_encoding: OutputEncoding,
    /// Stamp the target language and fill plurals even when nothing is translated.
    pub stamp_target_always: bool,
    /// The profile the document is written for (see [`ExtractOptions::profile`]); format crates may switch on it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,
    /// Merge-time format options, as for [`ExtractOptions::format_options`] (checked against the same keys).
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub format_options: BTreeMap<String, String>,
    /// Safety limits for a merge that reads a stored package again (EPUB, MadCap Flare); `None` uses
    /// [`Limits::default`]. They are the caller's policy: a merge never takes limits from the document, so a file
    /// extracted under raised limits is merged under the same ones only when the caller passes them here again.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limits: Option<Limits>,
}

impl MergeOptions {
    /// The defaults: keep the file's encoding, no stamp when nothing is translated (what [`Filter::merge`] does).
    pub fn new() -> MergeOptions {
        MergeOptions::default()
    }
    /// Sets the output encoding.
    pub fn with_output_encoding(mut self, output_encoding: OutputEncoding) -> MergeOptions {
        self.output_encoding = output_encoding;
        self
    }
    /// Sets the stamp rule.
    pub fn with_stamp_target_always(mut self, stamp_target_always: bool) -> MergeOptions {
        self.stamp_target_always = stamp_target_always;
        self
    }
    /// Switches a profile on (`"locos"`), or off with `None`.
    pub fn with_profile(mut self, profile: impl IntoOptionString) -> MergeOptions {
        self.profile = profile.into_option_string();
        self
    }
    /// True when this profile is on.
    pub fn is_profile(&self, name: &str) -> bool {
        self.profile.as_deref() == Some(name)
    }
    /// Sets the safety limits for this merge, or `None` for [`Limits::default`].
    pub fn with_limits(mut self, limits: impl Into<Option<Limits>>) -> MergeOptions {
        self.limits = limits.into();
        self
    }
    /// The limits in force: the caller's, or the defaults.
    pub fn limits(&self) -> Limits {
        // clamped, so a caller's huge number (2^62 units from a CLI flag or a Python dict) cannot overflow the
        // budgets formats derive from a limit (`40 * max_units`)
        self.limits.unwrap_or_default().clamped()
    }
    /// Sets one format option (`"<format>.<name>"`).
    pub fn with_format_option(mut self, key: impl Into<String>, value: impl ToString) -> MergeOptions {
        self.format_options.insert(key.into(), value.to_string());
        self
    }
    /// Replaces all format options.
    pub fn with_format_options(mut self, options: BTreeMap<String, String>) -> MergeOptions {
        self.format_options = options;
        self
    }

    /// True when the document has at least one segment with a target.
    pub fn any_translated(doc: &Document) -> bool {
        doc.segments().any(|(_, s)| s.has_target())
    }

    /// Should a format stamp the target language and fill plurals for this document? `target_lang` must be set (and
    /// not blank) and something translated, or `stamp_target_always`. "Translated" is [`MergeOptions::any_translated`];
    /// a format with its own notion of a change uses [`MergeOptions::stamp_lang`].
    pub fn should_stamp(&self, doc: &Document) -> bool {
        self.stamp_lang(doc, Self::any_translated(doc)).is_some()
    }

    /// Can this text be a language code in any spelling a format writes (`de-DE`, `zh_Hant`, `sr@latin`,
    /// `de_DE.UTF-8`)? ASCII letters, digits, `-`, `_`, `@` and `.`, at most 64 bytes. The target language is free
    /// text (a host setting, or the `trgLang` of a returned XLIFF), and several formats write it into a header
    /// line or a key; anything else, a line break above all, is never stamped.
    pub fn plausible_lang(code: &str) -> bool {
        !code.is_empty() && code.len() <= 64 && code.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'@' | b'.'))
    }

    /// The one stamp rule every format uses: the document's `target_lang` (trimmed, and only when it can be a
    /// language code, see [`MergeOptions::plausible_lang`]) when `translated` (the format's own "something was
    /// written" test) or `stamp_target_always`, else `None`.
    pub fn stamp_lang<'a>(&self, doc: &'a Document, translated: bool) -> Option<&'a str> {
        doc.target_lang.as_deref().map(str::trim).filter(|t| Self::plausible_lang(t)).filter(|_| translated || self.stamp_target_always)
    }
}

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
/// Every Bilingua failure. Match with a wildcard arm: a 1.x release may add variants.
pub enum Error {
    #[error("not a {format} file: {msg}")]
    /// The bytes are not a file of this format.
    Parse {
        /// The format id.
        format: String,
        /// What is wrong.
        msg: String,
    },
    #[error("unsupported: {0}")]
    /// No parser for the file, or a feature the format crate does not have.
    Unsupported(String),
    #[error("unknown segment {0}")]
    /// No segment has this id.
    UnknownSegment(String),
    #[error("segment {0} is locked")]
    /// The segment is locked, or belongs to a locked unit.
    Locked(String),
    #[error("tag {0} is not in the source")]
    /// The target uses a tag the source does not have.
    InventedTag(String),
    #[error("tag {0} appears more often than in the source")]
    /// The target uses a tag more often than the source.
    DuplicatedTag(String),
    #[error("this skeleton was made by {found}, but this is {expected}; open the file again with the current parser")]
    /// The skeleton comes from another parser or a newer skeleton schema.
    ParserVersion {
        /// The parser that made the skeleton.
        found: String,
        /// The parser that tried to read it.
        expected: String,
    },
    #[error("cannot write: {0}")]
    /// The skeleton is broken or does not belong to this document.
    Write(String),
    /// The translation cannot be written in this format: it does not fit (a caption too long), a character has no
    /// code in the file's code table, or the text would read back as structure (every letter swallowed by a
    /// placeholder). The document itself is fine; the translator has to change the text. `unit` names the unit when
    /// one is to blame; `reason` is plain text (LocOS's message where LocOS has one), safe to show the translator, and
    /// is also the whole `Display` text.
    /// `Write` stays for a broken or foreign skeleton.
    #[error("{reason}")]
    Unwritable {
        /// The unit to blame, when there is one.
        unit: Option<String>,
        /// Why, safe to show the translator.
        reason: String,
    },
    /// The target language changed after extract: `unit` was added at extract for a plural category of another
    /// target language (Polish `many` in a document now merged for Slovenian), so writing it would put one
    /// language's form under another's category. Extract the file again for this target (0.10).
    #[error("unit {unit} is a plural form added for another target language; extract the file again for this target")]
    TargetChanged {
        /// The first such unit.
        unit: String,
    },
    /// The file is over a safety limit or holds something unsafe (a zip bomb, an XML entity bomb, too many
    /// units). The message is short, names the limit and is safe to show the person who uploaded the file.
    #[error("{0}")]
    Limit(LimitError),
    /// The bytes cannot be read as text in any encoding Bilingua knows.
    #[error("{0}")]
    Encoding(String),
    /// A `format_options` key no format declares, or a value of the wrong kind.
    #[error("format option {key}: {msg}")]
    FormatOption {
        /// The key.
        key: String,
        /// What is wrong with it.
        msg: String,
    },
    /// A profile name Bilingua does not know (see [`profile::profiles`]).
    #[error("unknown profile {0:?}")]
    UnknownProfile(String),
    #[error(transparent)]
    /// An I/O error from a reader (Bilingua itself reads no files).
    Io(#[from] std::io::Error),
}

/// A file parser. Implementations live in one crate per format (`bilingua-html`, `bilingua-docx`, ...).
///
/// The trait is open: another crate may implement it for a format of its own. It implements `id`, `extensions`,
/// `sniff`, `extract` and `merge_with`; the rest have defaults, and a 1.x release adds methods only with defaults.
/// Such a filter is called directly (and segmented with `bilingua_seg::segment`): the `bilingua` facade's
/// `filters()` list is Bilingua's own formats. What a writer needs is public: [`Document::new`], [`Unit::new`],
/// [`Skeleton::text`] and friends, [`parser_info!`] with a `SKELETON_SCHEMA` constant, [`check_parser`], the
/// [`enc`] codecs and the [`guard`] limits. Start `extract` and `merge_with` with [`check_own_options`].
pub trait Filter: Send + Sync {
    /// Short id, e.g. "html".
    fn id(&self) -> &'static str;
    /// Lowercase extensions without the dot, e.g. `["html", "htm"]`.
    fn extensions(&self) -> &'static [&'static str];
    /// True when the bytes look like this format (used when the extension is missing or ambiguous).
    fn sniff(&self, bytes: &[u8]) -> bool;
    /// True when the format carries source and target (PO, XLIFF and the like): its extract may fill segment
    /// targets and states. False for source files.
    fn bilingual(&self) -> bool {
        false
    }
    /// The `ExtractOptions::format_options` keys this filter reads (`"csv.columns"`, ...), each with the
    /// `<family>.` prefix of its format. The facade refuses keys no filter declares; the filter itself refuses
    /// undeclared keys of its own families ([`check_own_options`]). Default: none.
    fn format_option_keys(&self) -> &'static [FormatOptionKey] {
        &[]
    }
    /// Source file to document. One segment per unit; no sentence splitting here.
    fn extract(&self, bytes: &[u8], opts: &ExtractOptions) -> Result<Document, Error>;
    /// Document back to a file in the original format, targets in place of sources, with [`MergeOptions`]. Every
    /// format honours both options: `output_encoding` (keep the file's encoding, or write UTF-8) and
    /// `stamp_target_always` (stamp the target language and fill plurals even when nothing is translated; the
    /// rule is [`MergeOptions::should_stamp`]).
    fn merge_with(&self, doc: &Document, opts: &MergeOptions) -> Result<Vec<u8>, Error>;
    /// [`Filter::merge_with`] with the default options: the file's own encoding, and no stamp when nothing is
    /// translated (an untouched merge gives the original bytes).
    fn merge(&self, doc: &Document) -> Result<Vec<u8>, Error> {
        self.merge_with(doc, &MergeOptions::default())
    }
    /// For formats a browser can show: the translated document as HTML in which every segment's text sits in
    /// `<span data-bl-seg="SEGMENT_ID">..</span>`, so a live preview can patch one segment without reloading.
    /// Scripts must be stripped. `None` when the format has no preview.
    fn preview(&self, _doc: &Document) -> Option<String> {
        None
    }
    /// The inner HTML of one segment's span in `preview()` (its current target, or source when empty), so a
    /// live preview can patch that span in place. `None` when the format has no preview or the id is unknown.
    fn render_segment(&self, _doc: &Document, _seg_id: &str) -> Option<String> {
        None
    }
}

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

    #[test]
    fn json_shape_matches_catcore() {
        let v = vec![Inline::text("Hi "), Inline::tag("1", TagKind::Open, "<b>")];
        let j = serde_json::to_string(&v).unwrap();
        assert_eq!(j, r#"[{"text":"Hi "},{"tag":{"id":"1","kind":"open","display":"<b>"}}]"#);
    }

    #[test]
    fn output_joins_segments_and_falls_back_to_source() {
        let mut u = Unit::new("p1", vec![]);
        u.segments = vec![
            Segment { id: "p1#1".into(), source: vec![Inline::text("One.")], target: vec![Inline::text("Uno.")], state: SegState::Draft, origin: None, empty_target: false },
            Segment { id: "p1#2".into(), source: vec![Inline::text("Two.")], target: vec![], state: SegState::New, origin: None, empty_target: false },
        ];
        u.separators = vec![" ".into()];
        assert_eq!(u.output(), vec![Inline::text("Uno. Two.")]);
        assert_eq!(plain_text(&u.source()), "One. Two.");
    }

    #[test]
    fn set_target_takes_the_free_flag_from_the_source() {
        let mut d = Document {
            format: "test".into(),
            name: None,
            source_lang: None,
            target_lang: None,
            granularity: Granularity::Paragraph,
            units: vec![Unit::new("u1", vec![Inline::text("Hi "), Inline::free_tag("1", "{name}"), Inline::tag("2", TagKind::Standalone, "%s")])],
            skeleton: Skeleton::Text { parts: vec![SkelPart::Unit(0)], encoding: "utf-8".into(), parser: None },
            warnings: vec![],
            source_encoding: None,
            seg_index: SegIndex::default(),
        };
        // An editor that does not know the flag sends both codes as not free, and claims the %s is free.
        let t = vec![
            Inline::Tag { id: "2".into(), kind: TagKind::Standalone, display: "%s".into(), free: true },
            Inline::text(" hola "),
            Inline::tag("1", TagKind::Standalone, "{name}"),
        ];
        d.set_target("u1", t).unwrap();
        let got: Vec<bool> = d.units[0].segments[0].target.iter().filter(|i| matches!(i, Inline::Tag { .. })).map(Inline::is_free).collect();
        assert_eq!(got, vec![false, true]);
    }

    #[test]
    fn set_target_checks_tags() {
        let mut d = Document {
            format: "test".into(),
            name: None,
            source_lang: None,
            target_lang: None,
            granularity: Granularity::Paragraph,
            units: vec![Unit::new("u1", vec![Inline::tag("1", TagKind::Open, "<b>"), Inline::text("x"), Inline::tag("1", TagKind::Close, "</b>")])],
            skeleton: Skeleton::Text { parts: vec![SkelPart::Unit(0)], encoding: "utf-8".into(), parser: None },
            warnings: vec![],
            source_encoding: None,
            seg_index: SegIndex::default(),
        };
        assert!(d.set_target("u1", vec![Inline::text("y")]).is_ok());
        assert!(matches!(d.set_target("u1", vec![Inline::tag("2", TagKind::Open, "<i>")]), Err(Error::InventedTag(_))));
        assert!(matches!(
            d.set_target("u1", vec![Inline::tag("1", TagKind::Open, ""), Inline::tag("1", TagKind::Open, "")]),
            Err(Error::DuplicatedTag(_))
        ));
        assert_eq!(d.find("u1").unwrap().1.state, SegState::Draft);
    }

    fn big_doc(n: usize) -> Document {
        Document {
            format: "test".into(),
            units: (0..n).map(|i| Unit::new(format!("u{i}"), vec![Inline::text(format!("Source {i}"))])).collect(),
            skeleton: Skeleton::Text { parts: vec![], encoding: "utf-8".into(), parser: None },
            ..Default::default()
        }
    }

    #[test]
    fn set_targets_sets_many_and_matches_set_target() {
        let mut a = big_doc(50);
        let mut b = big_doc(50);
        a.units[7].translate = false;
        b.units[7].translate = false;
        for i in (0..50).filter(|i| *i != 7) {
            a.set_target(&format!("u{i}"), vec![Inline::text(format!("T{i}"))]).unwrap();
        }
        b.set_targets((0..50).filter(|i| *i != 7).map(|i| (format!("u{i}"), vec![Inline::text(format!("T{i}"))]))).unwrap();
        assert_eq!(a, b);
        assert_eq!(b.find("u49").unwrap().1.state, SegState::Draft);
    }

    #[test]
    fn set_target_index_follows_edits_and_stays_out_of_equality() {
        let mut d = big_doc(20);
        d.set_target("u5", vec![Inline::text("a")]).unwrap();
        // the cached index does not make two equal documents unequal, and does not reach the JSON
        let e: Document = serde_json::from_str(&serde_json::to_string(&d).unwrap()).unwrap();
        assert_eq!(d, e);
        assert!(!serde_json::to_string(&d).unwrap().contains("seg_index"));
        // units removed, inserted and renamed through the public fields since the index was built
        let moved = d.units.remove(2);
        d.set_target("u6", vec![Inline::text("b")]).unwrap();
        assert_eq!(d.find("u6").unwrap().1.target, vec![Inline::text("b")]);
        assert!(matches!(d.set_target("u2", vec![]), Err(Error::UnknownSegment(_))));
        d.units.insert(0, moved);
        d.set_target("u2", vec![Inline::text("c")]).unwrap();
        assert_eq!(d.units[0].segments[0].target, vec![Inline::text("c")]);
        d.units[3].segments[0].id = "renamed".into();
        d.set_target("renamed", vec![Inline::text("d")]).unwrap();
        assert!(d.find("renamed").unwrap().1.has_target());
        // a clone and the JSON round trip set targets the same way
        let mut c: Document = serde_json::from_str(&serde_json::to_string(&d).unwrap()).unwrap();
        c.set_target("u9", vec![Inline::text("x")]).unwrap();
        assert_eq!(c.find("u9").unwrap().1.target, vec![Inline::text("x")]);
    }

    #[test]
    fn set_target_index_keeps_first_duplicate_send_sync_and_debug() {
        fn send_sync<T: Send + Sync>() {}
        send_sync::<Document>();
        let mut d = big_doc(6);
        d.units[4].segments[0].id = "u1".into();
        let before = format!("{d:?}");
        d.set_target("u1", vec![Inline::text("a")]).unwrap();
        d.set_target("u1", vec![Inline::text("b")]).unwrap();
        // the first segment with the id wins, as the old scan did; the later twin is untouched
        assert_eq!(d.units[1].segments[0].target, vec![Inline::text("b")]);
        assert!(!d.units[4].segments[0].has_target());
        // the cache does not show in Debug, and a clone (which starts without it) sets targets the same way
        let mut fresh = big_doc(6);
        fresh.units[4].segments[0].id = "u1".into();
        assert_eq!(before, format!("{fresh:?}"));
        let mut c = d.clone();
        c.set_target("u3", vec![Inline::text("z")]).unwrap();
        assert_eq!(c.find("u3").unwrap().1.target, vec![Inline::text("z")]);
        assert_eq!(format!("{:?}", d.clone()), format!("{d:?}"));
    }

    #[test]
    fn set_targets_errors_like_set_target_and_keeps_earlier_items() {
        let mut d = big_doc(3);
        d.units[1].translate = false;
        let r = d.set_targets(vec![("u0", vec![Inline::text("a")]), ("u1", vec![Inline::text("b")]), ("u2", vec![Inline::text("c")])]);
        assert!(matches!(r, Err(Error::Locked(_))));
        assert_eq!(d.units[0].segments[0].target, vec![Inline::text("a")]);
        assert!(d.units[2].segments[0].target.is_empty());
        assert!(matches!(d.set_targets(vec![("nope", vec![])]), Err(Error::UnknownSegment(_))));
        let r = d.set_targets(vec![("u0", vec![Inline::tag("9", TagKind::Open, "<i>")])]);
        assert!(matches!(r, Err(Error::InventedTag(_))));
        // an empty target clears the segment, as set_target does
        d.set_targets(vec![("u0", vec![Inline::text("  ")])]).unwrap();
        assert_eq!(d.units[0].segments[0].state, SegState::New);
    }

    #[test]
    fn set_states_batch() {
        let mut d = big_doc(4);
        d.set_states((0..4).map(|i| (format!("u{i}"), SegState::Confirmed))).unwrap();
        assert!(d.segments().all(|(_, s)| s.state == SegState::Confirmed));
        assert!(matches!(d.set_states(vec![("zzz", SegState::New)]), Err(Error::UnknownSegment(_))));
    }

    #[test]
    fn set_targets_100k_is_fast() {
        let n = 100_000;
        let mut d = big_doc(n);
        let t = std::time::Instant::now();
        d.set_targets((0..n).map(|i| (format!("u{i}"), vec![Inline::text("x")]))).unwrap();
        d.set_states((0..n).map(|i| (format!("u{i}"), SegState::Translated))).unwrap();
        let took = t.elapsed();
        // well under a second in release; a debug build gets a loose bound that still rules out a quadratic scan
        let limit = if cfg!(debug_assertions) { 10_000 } else { 1_000 };
        assert!(took.as_millis() < limit, "100k targets took {took:?}");
        assert_eq!(d.units[n - 1].segments[0].state, SegState::Translated);
    }
}