quarb-open 0.34.1

One open for every target the Quarb tools speak: files, readings, archives, databases, and the adapter-scheme fleet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
//! `quarb-open` — one `open` for every target the Quarb tools
//! speak: filesystem paths, documents by extension or content,
//! the reading prefixes (`text:`, `corpus:`, `lit:`, `koine:`,
//! `code:`, `web:`, `lines:`, `json2:`), archives, databases and
//! the adapter-scheme fleet (`gcl:`, `kafka:`, `neo4j://`, …).
//! The CLI, the session (quai, the Python package) and every
//! other front-end mount through [`open_target`], so a target
//! opens the same way everywhere and a format is dispatched in
//! one place.
//!
//! The source boundary lives here too: `?encoding=` comes off a
//! target before it is opened, `?delimiter=` / `?decimal=` for
//! delimited files, `?references=STANDARD` for a text reading,
//! and the record of the decoding a read made goes on the
//! mount's root as `::::encoding` / `::::replaced`.

use anyhow::Context;
use quarb::{AstAdapter, NodeId};
use quarb_age::AgeAdapter;
use quarb_arangodb::ArangoAdapter;
use quarb_archive::ArchiveAdapter;
#[cfg(feature = "heavy")]
use quarb_athena::AthenaAdapter;
use quarb_atrep::AtrepAdapter;
use quarb_azlogs::AzlAdapter;
#[cfg(feature = "heavy")]
use quarb_bigquery::BigqueryAdapter;
use quarb_cflogs::CflAdapter;
use quarb_compose::{ComposeAdapter, DocumentGraft, SourceGraft};
use quarb_cosmos::CosmosAdapter;
use quarb_csv::CsvAdapter;
use quarb_cwlogs::CwlAdapter;
use quarb_datastore::DatastoreAdapter;
use quarb_ddlogs::DdlAdapter;
#[cfg(feature = "heavy")]
use quarb_duckdb::DuckdbAdapter;
use quarb_dynamodb::DynamodbAdapter;
use quarb_falkordb::FalkorAdapter;
use quarb_firebase::FirebaseAdapter;
use quarb_firestore::FirestoreAdapter;
use quarb_fs::{FsAdapter, FsOptions};
use quarb_gcplogs::GclAdapter;
use quarb_git::GitAdapter;
use quarb_github::GithubAdapter;
use quarb_gitlab::GitlabAdapter;
use quarb_gsheet::GsheetAdapter;
use quarb_html::HtmlAdapter;
use quarb_imap::ImapAdapter;
use quarb_json::JsonAdapter;
use quarb_kafka::KafkaAdapter;
use quarb_kubernetes::KubernetesAdapter;
#[cfg(feature = "kuzu")]
use quarb_kuzu::KuzuAdapter;
use quarb_ldap::LdapAdapter;
use quarb_maildir::MaildirAdapter;
use quarb_memgraph::MemgraphAdapter;
#[cfg(unix)]
use quarb_metatheca::MetathecaAdapter;
#[cfg(feature = "heavy")]
use quarb_mongodb::MongodbAdapter;
use quarb_mount::Shared;
#[cfg(feature = "heavy")]
use quarb_mssql::MssqlAdapter;
#[cfg(feature = "heavy")]
use quarb_mysql::MysqlAdapter;
use quarb_neo4j::Neo4jAdapter;
use quarb_neptune::NeptuneAdapter;
use quarb_objstore::ObjstoreAdapter;
#[cfg(feature = "heavy")]
use quarb_oracle::OracleAdapter;
use quarb_postgres::PostgresAdapter;
use quarb_redis::RedisAdapter;
use quarb_serve::ServeAdapter;
use quarb_sparql::SparqlAdapter;
use quarb_sqlite::SqliteAdapter;
use quarb_tree_sitter::TreeSitterAdapter;
use quarb_xlsx::XlsxAdapter;
use quarb_xml::XmlAdapter;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::rc::Rc;

/// The source boundary of one open: the options the target carried
/// — `?encoding=` (the explicit label), `?delimiter=` / `?decimal=`
/// for a delimited file, `?references=STANDARD` for a text reading —
/// and the record of the decoding a read made, which goes on the
/// mount's root as `::::encoding` / `::::replaced`. One per target
/// opened, passed to every door and reader; nothing about a source
/// lives in a thread-local.
#[derive(Clone, Default)]
pub struct SourceCtx {
    encoding: std::cell::RefCell<Option<String>>,
    csv: std::cell::Cell<quarb_csv::CsvOptions>,
    references: std::cell::RefCell<Option<String>>,
    record: std::cell::RefCell<Option<(String, usize)>>,
}

impl SourceCtx {
    /// The `?encoding=` label in force, if one was given.
    pub fn encoding(&self) -> Option<String> {
        self.encoding.borrow().clone()
    }

    fn set_encoding(&self, label: &str) {
        *self.encoding.borrow_mut() = Some(label.to_string());
    }

    fn clear_encoding(&self) {
        *self.encoding.borrow_mut() = None;
    }

    /// The delimited-file options in force.
    pub fn csv(&self) -> quarb_csv::CsvOptions {
        self.csv.get()
    }

    fn set_csv(&self, next: quarb_csv::CsvOptions) {
        self.csv.set(next);
    }

    fn references(&self) -> Option<String> {
        self.references.borrow().clone()
    }

    fn set_references(&self, standard: Option<&str>) {
        *self.references.borrow_mut() = standard.map(str::to_string);
    }

    fn set_record(&self, encoding: String, replaced: usize) {
        *self.record.borrow_mut() = Some((encoding, replaced));
    }

    /// The decoding record of the read this context saw, taken.
    pub fn take_record(&self) -> Option<(String, usize)> {
        self.record.borrow_mut().take()
    }
}

/// Read and parse the `--refs` document into the relational form,
/// `(field, container)` pairs. (The Firebase adapter parses the
/// same file into its own path-shaped form.) Empty when no --refs.
pub fn relational_refs(refs: &Option<PathBuf>) -> anyhow::Result<Vec<(String, String)>> {
    match refs {
        Some(f) => {
            let text = std::fs::read_to_string(f)
                .with_context(|| format!("reading refs file {}", f.display()))?;
            quarb_relational::parse_refs(&text).map_err(|e| anyhow::anyhow!("parsing refs: {e}"))
        }
        None => Ok(Vec::new()),
    }
}

/// Whether the input is a Quarb query file (`.quarb`), to be
/// reflected as a query arbor.
pub fn is_quarb(path: Option<&Path>) -> bool {
    path.and_then(Path::extension)
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("quarb"))
}

/// Whether the input names a PostgreSQL connection rather than a
/// file: a `postgres://` / `postgresql://` URL, or the keyword
/// form (`host=... dbname=...`).
pub fn is_pg_config(s: &str) -> bool {
    s.starts_with("postgres://") || s.starts_with("postgresql://") || s.starts_with("host=")
}

/// Whether the extension belongs to a format the text dispatch
/// owns: the archive/SQLite magic sniffs must not pre-empt these —
/// a CSV whose first cell starts with "PK" is still a CSV.
pub fn known_text_ext(path: &Path) -> bool {
    path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
        matches!(
            e.to_ascii_lowercase().as_str(),
            "quarb"
                | "csv"
                | "tsv"
                | "json"
                | "jsonl"
                | "ndjson"
                | "yaml"
                | "yml"
                | "toml"
                | "md"
                | "markdown"
                | "kaiv"
                | "daiv"
                | "raiv"
                | "atd"
                | "atk"
                | "xml"
                | "fb2"
                | "svg"
                | "xhtml"
                | "html"
                | "htm"
                | "txt"
                | "conllu"
                | "conllup"
        )
    })
}

/// Zip-family or tar archives, by extension or magic bytes. The
/// magic sniff skips extensions the text dispatch owns.
pub fn is_archive(path: &Path) -> bool {
    if let Some(e) = path.extension().and_then(|e| e.to_str())
        && (quarb_archive::archive_extension(e) || quarb_archive::container_extension(e))
    {
        return true;
    }
    if known_text_ext(path) {
        return false;
    }
    let mut buf = [0u8; 2];
    std::fs::File::open(path)
        .and_then(|mut f| std::io::Read::read(&mut f, &mut buf))
        .map(|n| n == 2 && (buf == *b"PK" || buf == [0x1f, 0x8b]))
        .unwrap_or(false)
}

/// Whether the input is a SQLite database: by extension
/// (`.db` / `.sqlite` / `.sqlite3`), or by the 16-byte magic (again
/// skipped for extensions the text dispatch owns).
pub fn is_sqlite(path: &Path) -> bool {
    if path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
        e.eq_ignore_ascii_case("db")
            || e.eq_ignore_ascii_case("sqlite")
            || e.eq_ignore_ascii_case("sqlite3")
    }) {
        return true;
    }
    if known_text_ext(path) {
        return false;
    }
    let mut buf = [0u8; 16];
    std::fs::File::open(path)
        .and_then(|mut f| std::io::Read::read_exact(&mut f, &mut buf))
        .is_ok()
        && &buf == b"SQLite format 3\0"
}

/// The CSV field delimiter implied by the file extension: `.csv`
/// (comma) or `.tsv` (tab), else not a CSV file.
pub fn csv_delimiter(path: Option<&Path>) -> Option<u8> {
    let ext = path?.extension()?.to_str()?;
    if ext.eq_ignore_ascii_case("csv") {
        Some(b',')
    } else if ext.eq_ignore_ascii_case("tsv") {
        Some(b'\t')
    } else {
        None
    }
}

/// Whether the input is an atrep document: the first content line
/// (after an optional shebang) is a dialektos declaration in either
/// sigil — `@@@!<id>` or `\\\!<id>`. Extension dispatch handles
/// `.atd`/`.atk`; this sniff catches stdin and unsuffixed files,
/// and cannot collide with the `<`-leading XML/HTML sniffs.
pub fn is_atrep(text: &str) -> bool {
    let mut lines = text.lines();
    let mut first = lines.next().unwrap_or("");
    if first.starts_with("#!") {
        first = lines.next().unwrap_or("");
    }
    let decl = first.trim_start();
    decl.starts_with("@@@!") || decl.starts_with("\\\\\\!")
}

/// Whether the input should be parsed as XML: an `.xml`/`.svg`/
/// `.xhtml` extension, or content that begins with the `<?xml`
/// prologue. Checked before HTML, whose generic `<` sniff would
/// otherwise swallow XML.
pub fn is_xml(path: Option<&Path>, text: &str) -> bool {
    let by_ext = path
        .and_then(Path::extension)
        .and_then(|e| e.to_str())
        .is_some_and(|e| {
            ["xml", "svg", "xhtml", "fb2"]
                .iter()
                .any(|x| e.eq_ignore_ascii_case(x))
        });
    by_ext || text.trim_start().starts_with("<?xml")
}

/// Whether the input should be parsed as HTML: an `.html`/`.htm`
/// extension, or content that begins with `<`.
pub fn is_html(path: Option<&Path>, text: &str) -> bool {
    let by_ext = path
        .and_then(Path::extension)
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("html") || e.eq_ignore_ascii_case("htm"));
    by_ext || text.trim_start().starts_with('<')
}

/// The text-level reading of a document (`text:` targets and
/// `.txt` files): the producer is chosen by extension, `<` sniffs
/// markup for the rest, plain paragraphs are the fallback.
/// The binary text-level producers, by extension: formats whose
/// bytes are a container, not UTF-8. Plain (unprefixed) paths
/// keep their archive reading — `text:` opts into the reader's
/// model.
/// A DjVu by its extension (`.djvu`, `.djv`): the text layer's
/// reading, at `text:`.
pub fn is_djvu(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("djvu") || e.eq_ignore_ascii_case("djv"))
}

pub fn binary_text_kind(path: &Path) -> Option<&'static str> {
    // A Lingvo DSL dictionary (`.dsl`, `.dsl.dz`): the koine route's
    // import, under the dictionary vocabulary.
    if quarb_text_koine::is_dsl_path(path) {
        return Some("dsl");
    }
    let ext = path.extension()?.to_str()?.to_ascii_lowercase();
    match ext.as_str() {
        "docx" => Some("docx"),
        "epub" => Some("epub"),
        // OpenDocument text: the package, the flat XML, and
        // OpenOffice.org 1.0's; `.ods` is a spreadsheet (xlsx's
        // reading), `.odp` a deck, neither a text.
        "odt" | "fodt" | "sxw" => Some("odt"),
        // Word 97–2003 by its OLE2 magic; a plain-text file named
        // `.doc` (the kernel's m68k notes) keeps the plain reading.
        "doc" => std::fs::read(path)
            .ok()
            .filter(|b| quarb_text_doc::is_ole2(b))
            .map(|_| "doc"),
        // A zip under another name whose `mimetype` member
        // declares an OpenDocument text.
        "zip" => std::fs::read(path)
            .ok()
            .filter(|b| quarb_text_odt::declares_text(b))
            .map(|_| "odt"),
        _ => None,
    }
}

pub fn binary_text_level(
    kind: &str,
    bytes: &[u8],
    path: &Path,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    match kind {
        "dsl" => quarb_text_koine::parse_dsl_file(path)
            .map_err(|e| anyhow::anyhow!("as a Lingvo DSL dictionary: {e}")),
        "docx" => quarb_text_docx::parse_with_decoding(bytes, &current_decoding(ctx)?)
            .map(|mut m| {
                // Word's sources list lowers under the Latin
                // campi; the census lets ::author answer.
                quarb_text_koine::attach_bib_aliases(&mut m);
                m
            })
            .map_err(|e| anyhow::anyhow!("as Word: {e}")),
        "odt" => quarb_text_odt::parse_with_decoding(bytes, &current_decoding(ctx)?)
            .map_err(|e| anyhow::anyhow!("as OpenDocument: {e}")),
        "doc" => quarb_text_doc::parse(bytes).map_err(|e| anyhow::anyhow!("as Word 97: {e}")),
        _ => quarb_text_epub::parse_with_decoding(bytes, &current_decoding(ctx)?)
            .map_err(|e| anyhow::anyhow!("as EPUB: {e}")),
    }
}

pub enum WebSite {
    Memory(quarb_web::WebAdapter<quarb_web::MemoryStore>),
    /// The store itself: the caller plans before wrapping it.
    Sqlite(quarb_web::db::sqlite::SqliteStore),
    Postgres(quarb_web::db::postgres::PostgresStore),
}

/// `encoding=LABEL` among a reader's own options: validated and
/// set for the reads that follow. Every reader prefix takes it.
pub fn set_encoding_option(label: &str, ctx: &SourceCtx) -> anyhow::Result<()> {
    quarb::source::encoding_for_label(label)
        .ok_or_else(|| anyhow::anyhow!("unknown encoding label {label:?}"))?;
    ctx.set_encoding(label);
    Ok(())
}

/// The source-boundary rule for the mount being opened (its
/// `?encoding=` and the host's declaration reporter), for the
/// layers that read bytes themselves: a folder's grafts, an
/// archive's members, a mailbox, a site's pages.
pub fn current_decoding(ctx: &SourceCtx) -> anyhow::Result<quarb::source::SourceDecoding> {
    let explicit = ctx.encoding();
    quarb_compose::source_decoding(explicit.as_deref()).map_err(|e| anyhow::anyhow!("{e}"))
}

/// The mailbox rule: the mount's option, then the charset a
/// message's own headers declare.
pub fn mail_decoding(ctx: &SourceCtx) -> anyhow::Result<quarb::source::SourceDecoding> {
    let explicit = ctx.encoding();
    quarb::source::SourceDecoding::default()
        .with_declared(|_, b| quarb_maildir::declared_encoding(b))
        .with_option(explicit.as_deref())
        .map_err(|e| anyhow::anyhow!("{e}"))
}

/// A reader prefix with no options of its own (`text:`, `lines:`,
/// `json2:`, `code:`) still takes `?encoding=LABEL`; a path as
/// written is taken as is.
pub fn take_reader_encoding(rest: &str, ctx: &SourceCtx) -> anyhow::Result<String> {
    // Each mount states its own options: a `?references=` from an
    // earlier mount does not carry over.
    ctx.set_references(None);
    if Path::new(rest).exists() || !rest.contains('?') {
        return Ok(rest.to_string());
    }
    let (path, query) = rest.rsplit_once('?').unwrap();
    for pair in query.split('&').filter(|p| !p.is_empty()) {
        match pair.split_once('=') {
            Some(("encoding", label)) => set_encoding_option(label, ctx)?,
            Some(("references", standard)) => set_references_option(standard, ctx)?,
            _ => anyhow::bail!(
                "unknown option {pair:?} on {path} — this reader takes encoding=, references="
            ),
        }
    }
    Ok(path.to_string())
}

pub fn set_references_option(standard: &str, ctx: &SourceCtx) -> anyhow::Result<()> {
    anyhow::ensure!(
        quarb::bibref::STANDARDS.contains(&standard),
        "references={standard} — references= takes {}",
        quarb::bibref::STANDARDS.join(", ")
    );
    ctx.set_references(Some(standard));
    Ok(())
}

/// Apply the mount's `?references=` to a model just built (every
/// document of a folder mount alike).
pub fn apply_references(
    mut model: quarb_text::TextModel,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    if let Some(standard) = ctx.references() {
        model
            .lower_references(&standard)
            .map_err(|e| anyhow::anyhow!("{e}"))?;
    }
    Ok(model)
}

pub fn web_level(spec: &str, ctx: &SourceCtx) -> anyhow::Result<WebSite> {
    let (path_part, base) = match spec.split_once('?') {
        Some((p, q)) => {
            let mut base = String::new();
            for pair in q.split('&') {
                match pair.split_once('=') {
                    Some(("base", v)) => base = v.to_string(),
                    Some(("encoding", v)) => set_encoding_option(v, ctx)?,
                    _ => anyhow::bail!("unknown web option {pair:?} — supported: base=, encoding="),
                }
            }
            (p, base)
        }
        None => (spec, String::new()),
    };
    if path_part.starts_with("postgres://") || path_part.starts_with("postgresql://") {
        return quarb_web::db::postgres::PostgresStore::open(path_part)
            .map(WebSite::Postgres)
            .map_err(|e| anyhow::anyhow!("opening the site store: {e}"));
    }
    let target = Path::new(path_part);
    if target.is_dir() {
        return quarb_web::fs::open_dir_with_decoding(target, &base, current_decoding(ctx)?)
            .map(WebSite::Memory)
            .with_context(|| format!("reading {} as a site", target.display()));
    }
    if is_archive(target) {
        return quarb_web::archive::open_path_with_decoding(target, &base, current_decoding(ctx)?)
            .map(WebSite::Memory)
            .with_context(|| format!("reading {} as a site", target.display()));
    }
    // An index-backed store: a site.db built by quarb-web-ingest.
    if target
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| matches!(e, "db" | "sqlite" | "sqlite3"))
    {
        return quarb_web::db::sqlite::SqliteStore::open(target)
            .map(WebSite::Sqlite)
            .map_err(|e| anyhow::anyhow!("opening {} as a site store: {e}", target.display()));
    }
    anyhow::bail!(
        "web: takes a directory of pages, an archive of them, or a site.db store, not {}",
        target.display()
    )
}

/// The koine reading (`koine:` — and `text:` on native atrep
/// files): native atrep documents lower directly; Markdown, HTML,
/// reStructuredText, Org, and djot arrive through atrep's
/// endomorphosis importers into their mirror dialects. Anything
/// else refuses by design — no atrep import exists for it yet,
/// and a PDF never will (the print reading is `text:`).
pub fn koine_level(spec: &str, ctx: &SourceCtx) -> anyhow::Result<quarb_text::TextModel> {
    apply_references(koine_level_inner(spec, ctx)?, ctx)
}

pub fn koine_level_inner(spec: &str, ctx: &SourceCtx) -> anyhow::Result<quarb_text::TextModel> {
    // The house ?param syntax: `koine:records.xml?format=jats`
    // forces a format — the third dispatch tier, for stdin-ish,
    // extensionless, or undeclared inputs.
    ctx.set_references(None);
    let (path_part, format) = match spec.split_once('?') {
        Some((p, q)) => {
            let mut fmt = None;
            for pair in q.split('&') {
                match pair.split_once('=') {
                    Some(("format", v)) => fmt = Some(v.to_string()),
                    Some(("encoding", v)) => set_encoding_option(v, ctx)?,
                    Some(("references", v)) => set_references_option(v, ctx)?,
                    _ => anyhow::bail!(
                        "unknown koine option {pair:?} — supported: format=, encoding=, references="
                    ),
                }
            }
            (p, fmt)
        }
        None => (spec, None),
    };
    let target = Path::new(path_part);
    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    let read = || -> anyhow::Result<String> {
        let text = read_prose(target, ctx)?;
        Ok(match text.strip_prefix('\u{feff}') {
            Some(rest) => rest.to_owned(),
            None => text,
        })
    };
    if let Some(fmt) = format {
        let imported = match fmt.as_str() {
            "atd" | "atk" => {
                return quarb_text_koine::parse_file(target)
                    .with_context(|| format!("reading {} as an atrep document", target.display()));
            }
            "md" | "markdown" => quarb_text_koine::parse_markdown(&read()?),
            "html" => quarb_text_koine::parse_html(&read()?),
            "rst" => quarb_text_koine::parse_rst(&read()?),
            "org" => quarb_text_koine::parse_org(&read()?),
            "dj" | "djot" => quarb_text_koine::parse_djot(&read()?),
            "tei" | "docbook" | "jats" | "usx" | "osis" | "fb2" | "rnc" | "opencorpora"
            | "proiel" => quarb_text_koine::parse_xml_as(&read()?, &fmt),
            "usfm" | "sfm" => quarb_text_koine::parse_xml_as(&read()?, "usfm"),
            // Word through atrep's importer (bytes in): the second
            // route beside the native text:x.docx.
            "docx" => quarb_text_koine::parse_docx(&std::fs::read(target)?),
            // A Lingvo DSL dictionary into lexigramma, lowered under
            // the dictionary vocabulary.
            "dsl" => quarb_text_koine::parse_dsl_file(target),
            other => anyhow::bail!(
                "unknown koine format {other:?} — known: md, html, rst, org, djot, tei, docbook, jats, usx, osis, fb2, usfm, docx, dsl, atd"
            ),
        };
        return imported
            .with_context(|| format!("importing {} through atrep as {fmt}", target.display()));
    }
    if quarb_text_koine::is_dsl_path(target) {
        return quarb_text_koine::parse_dsl_file(target)
            .with_context(|| format!("importing {} as a Lingvo DSL dictionary", target.display()));
    }
    let imported = match ext.as_deref() {
        Some("atd" | "atk") => {
            return quarb_text_koine::parse_file(target)
                .with_context(|| format!("reading {} as an atrep document", target.display()));
        }
        Some("docx") => quarb_text_koine::parse_docx(&std::fs::read(target)?),
        Some("usfm" | "sfm") => quarb_text_koine::parse_xml_as(&read()?, "usfm"),
        Some("fb2") => quarb_text_koine::parse_xml_as(&read()?, "fb2"),
        Some("md" | "markdown") => quarb_text_koine::parse_markdown(&read()?),
        Some("html" | "htm") => quarb_text_koine::parse_html(&read()?),
        Some("rst") => quarb_text_koine::parse_rst(&read()?),
        Some("org") => quarb_text_koine::parse_org(&read()?),
        Some("dj" | "djot") => quarb_text_koine::parse_djot(&read()?),
        Some("xml") => {
            // The XML vocabularies dispatch by DECLARED identity:
            // root namespace, DOCTYPE public id, or unambiguous
            // root element — never a statistical guess.
            let text = read()?;
            match quarb_text_koine::detect_xml_kind(&text) {
                Some(kind) => quarb_text_koine::parse_xml_as(&text, kind),
                None => anyhow::bail!(
                    "{} declares no XML identity this route knows (no namespace, DOCTYPE, or unambiguous root) — force one with koine:{}?format=tei|docbook|jats|usx|osis|fb2|rnc|opencorpora|proiel",
                    target.display(),
                    target.display()
                ),
            }
        }
        Some("pdf") => anyhow::bail!(
            "atrep cannot import a PDF — the print reading is text:{}",
            target.display()
        ),
        _ => anyhow::bail!(
            "no atrep import for this format yet — the native reading is text:{}",
            target.display()
        ),
    };
    imported.with_context(|| format!("importing {} through atrep", target.display()))
}

/// The corpus reading (`corpus:`): the text-level
/// reading of a document with every prose block's tokens as its
/// children — the tokenizer decided once, at mount. Every source
/// `text:` reads as a prose model qualifies: plain text, HTML,
/// Markdown, LaTeX, atrep, TEI and the other koine dialects,
/// .docx and .epub. An archive keeps to `text:` for now, and a
/// PDF's reading is line geometry, never prose.
/// A document under the property reading (`json2:`).
pub enum PropertyDoc {
    Json(JsonAdapter),
    Cbor(quarb_cbor::CborAdapter),
}

/// Read `target` under the property reading: the format by
/// extension (JSON by default; JSONL, YAML, TOML, CBOR by theirs),
/// every one of them the JSON data model read the `json2` way —
/// containers as nodes, scalars as properties.
pub fn property_reading(target: &Path, ctx: &SourceCtx) -> anyhow::Result<PropertyDoc> {
    use quarb_json::Reading;
    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase())
        .unwrap_or_default();
    if ext == "cbor" {
        let bytes =
            std::fs::read(target).with_context(|| format!("reading {}", target.display()))?;
        let a =
            quarb_cbor::CborAdapter::parse_as(&bytes, Reading::Property).context("parsing CBOR")?;
        return Ok(PropertyDoc::Cbor(a));
    }
    let text = read_prose(target, ctx)?;
    let a = match ext.as_str() {
        "yaml" | "yml" => quarb_yaml::parse_as(&text, Reading::Property).context("parsing YAML")?,
        "toml" => quarb_toml::parse_as(&text, Reading::Property).context("parsing TOML")?,
        "jsonl" | "ndjson" => {
            JsonAdapter::parse_lines_as(&text, Reading::Property).context("parsing JSONL")?
        }
        _ => match JsonAdapter::parse_as(&text, Reading::Property) {
            Ok(a) => a,
            Err(e) => match JsonAdapter::parse_lines_as(&text, Reading::Property) {
                Ok(a) => a,
                Err(_) => return Err(e).context("parsing JSON"),
            },
        },
    };
    Ok(PropertyDoc::Json(a))
}

pub fn corpus_level(
    rest: &str,
    allow_shell: bool,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    // The house ?param syntax: `?conllu=FILE` takes the tokens,
    // sentences and annotation from a CoNLL-U sidecar; `?annotate=CMD`
    // runs a command over the document's prose (stdin) and reads
    // CoNLL-U from its stdout — under the shell gate, like sh().
    // `?format=conllu` names the reading when no extension can
    // (a pipe, an extensionless file); the sniff covers the rest.
    let (rest, conllu, annotate, desm, format, cast, places, modernize, notes) = match rest
        .split_once('?')
    {
        Some((p, q)) => {
            let (
                mut conllu,
                mut annotate,
                mut desm,
                mut format,
                mut cast,
                mut places,
                mut modernize,
                mut notes,
            ) = (None, None, Vec::new(), None, None, None, None, None);
            for pair in q.split('&') {
                match pair.split_once('=') {
                    Some(("conllu", v)) => conllu = Some(v.to_string()),
                    Some(("annotate", v)) => annotate = Some(v.to_string()),
                    Some(("desm", v)) => desm.push(v.to_string()),
                    Some(("cast", v)) => cast = Some(v.to_string()),
                    Some(("places", v)) => places = Some(v.to_string()),
                    Some(("modernize", v)) => modernize = Some(v.to_string()),
                    Some(("notes", v)) => notes = Some(v.to_string()),
                    Some(("encoding", v)) => set_encoding_option(v, ctx)?,
                    Some(("format", "conllu")) => format = Some("conllu"),
                    Some(("format", v)) => anyhow::bail!(
                        "corpus: format={v} is not a treebank format — format= takes conllu"
                    ),
                    _ => anyhow::bail!(
                        "unknown corpus option {pair:?} — supported: conllu=, annotate=, desm=, cast=, places=, modernize=, notes=, format=, encoding="
                    ),
                }
            }
            (
                p, conllu, annotate, desm, format, cast, places, modernize, notes,
            )
        }
        None => (rest, None, None, Vec::new(), None, None, None, None, None),
    };
    let target = Path::new(rest);
    if is_archive(target) && binary_text_kind(target).is_none() {
        anyhow::bail!(
            "corpus: over an archive is not supported yet — mount it as text:{rest}, or corpus: one member"
        );
    }
    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    if ext.as_deref() == Some("pdf") {
        anyhow::bail!(
            "corpus: needs a prose reading, and a PDF's is line geometry — text:{rest} reads its lines"
        );
    }
    // A treebank is its own annotation: the file's
    // tokens under its sentences; `conllu=` / `annotate=` do not
    // apply.
    let own_annotation = || {
        anyhow::ensure!(
            conllu.is_none() && annotate.is_none(),
            "corpus: {rest} is its own annotation — conllu= and annotate= apply to prose"
        );
        Ok(())
    };
    // A treebank is its own tokens: the cast table and the
    // spelling table still apply.
    let treebank_options = |mut m: quarb_text::TextModel| -> anyhow::Result<quarb_text::TextModel> {
        if let Some(t) = &modernize {
            m.set_orthography(t)
                .map_err(|e| anyhow::anyhow!("corpus: {e}"))?;
        }
        if let Some(f) = &cast {
            let rows = quarb_text::CastRow::read_csv(f)
                .map_err(|e| anyhow::anyhow!("corpus: reading the cast table {f}: {e}"))?;
            m.apply_cast(&rows);
        }
        if let Some(f) = &places {
            let rows = quarb_text::CastRow::read_csv(f)
                .map_err(|e| anyhow::anyhow!("corpus: reading the places table {f}: {e}"))?;
            m.apply_places(&rows);
        }
        Ok(m)
    };
    if matches!(ext.as_deref(), Some("conllu" | "conllup")) || format == Some("conllu") {
        own_annotation()?;
        let text = read_prose(target, ctx)?;
        let mut m = quarb_text::TextModel::parse_conllu_corpus(&text)
            .map_err(|e| anyhow::anyhow!("reading {rest} as CoNLL-U: {e}"))?;
        m.set_document_path(rest);
        return treebank_options(m);
    }
    // A directory of treebank files — a UD treebank's train / dev /
    // test, a release — reads as one corpus, a section per file.
    if target.is_dir() {
        own_annotation()?;
        let files = quarb_text::read_conllu_dir_with(target, &current_decoding(ctx)?)
            .map_err(|e| anyhow::anyhow!("corpus: over a directory reads its treebank — {e}"))?;
        let refs: Vec<(&str, &str)> = files
            .iter()
            .map(|(n, t)| (n.as_str(), t.as_str()))
            .collect();
        let mut m = quarb_text::TextModel::parse_conllu_corpus_set(&refs)
            .map_err(|e| anyhow::anyhow!("reading {rest} as CoNLL-U: {e}"))?;
        m.set_document_path(rest);
        return treebank_options(m);
    }
    let model = if matches!(ext.as_deref(), Some("atd" | "atk" | "usfm" | "sfm")) {
        koine_level(rest, ctx)?
    } else if let Some(kind) = binary_text_kind(target) {
        let bytes =
            std::fs::read(target).with_context(|| format!("reading {}", target.display()))?;
        binary_text_level(kind, &bytes, target, ctx)
            .with_context(|| format!("reading {}", target.display()))?
    } else {
        let text = read_prose(target, ctx)?;
        if is_xml(Some(target), &text) {
            koine_level(rest, ctx)?
        } else if ext.is_none() && quarb_text::looks_like_conllu(&text) {
            // A pipe or an extensionless file carrying CoNLL-U:
            // the treebank reading, as `?format=conllu` would say.
            own_annotation()?;
            let mut m = quarb_text::TextModel::parse_conllu_corpus(&text)
                .map_err(|e| anyhow::anyhow!("reading {rest} as CoNLL-U: {e}"))?;
            m.set_document_path(rest);
            return treebank_options(m);
        } else {
            let mut m = text_level(&text, Some(target), ctx)?;
            m.set_document_path(&target.display().to_string());
            m
        }
    };
    finish_corpus(
        model,
        conllu,
        annotate,
        &desm,
        cast,
        places,
        modernize,
        notes,
        allow_shell,
        "corpus",
        ctx,
    )
}

/// The corpus reading's second half, shared with `lit:`: the
/// sidecar or command annotation, else the tokenizer with the
/// session's and the mount's sentence bonds.
pub fn finish_corpus(
    mut model: quarb_text::TextModel,
    conllu: Option<String>,
    annotate: Option<String>,
    desm: &[String],
    cast: Option<String>,
    places: Option<String>,
    modernize: Option<String>,
    notes: Option<String>,
    allow_shell: bool,
    level: &str,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    match notes.as_deref() {
        None => {}
        Some("omit") => model.omit_notes(true),
        Some(v) => anyhow::bail!(
            "{level}: notes={v} — notes= takes omit (a note is part of the text by default)"
        ),
    }
    if let Some(table) = &modernize {
        model
            .set_orthography(table)
            .map_err(|e| anyhow::anyhow!("{level}: {e}"))?;
    }
    let conllu_text = match (conllu, annotate) {
        (Some(_), Some(_)) => anyhow::bail!("{level}: takes conllu= or annotate=, not both"),
        (Some(file), None) => Some(
            // A sidecar is a source too; its record stays its own.
            quarb_compose::read_source(Path::new(&file), ctx.encoding().as_deref())
                .with_context(|| format!("reading {file} as CoNLL-U"))?
                .map_err(|e| anyhow::anyhow!("reading {file} as CoNLL-U: {e}"))?
                .text,
        ),
        (None, Some(cmd)) => {
            anyhow::ensure!(
                allow_shell,
                "{level}: annotate= runs a command; pass --allow-shell to permit it"
            );
            Some(annotate_with(&cmd, &model.corpus_text())?)
        }
        (None, None) => None,
    };
    match conllu_text {
        Some(text) => model
            .annotate_conllu(&text)
            .map_err(|e| anyhow::anyhow!("{level}: {e}"))?,
        None => {
            // The session's bonds, plus this mount's own `?desm=` files.
            let mut bonds = quarb::sentence_bonds()
                .and_then(|b| b.as_any().downcast_ref::<syndesmos::Syndesmos>().cloned());
            for f in desm {
                let more = syndesmos::Syndesmos::load(f)
                    .map_err(|e| anyhow::anyhow!("reading {f}: {e}"))?;
                bonds
                    .get_or_insert_with(syndesmos::Syndesmos::empty)
                    .extend(more);
            }
            model.tokenize_with(bonds.as_ref());
        }
    }
    if let Some(file) = cast {
        let rows = quarb_text::CastRow::read_csv(&file)
            .map_err(|e| anyhow::anyhow!("{level}: reading the cast table {file}: {e}"))?;
        model.apply_cast(&rows);
    }
    if let Some(file) = places {
        let rows = quarb_text::CastRow::read_csv(&file)
            .map_err(|e| anyhow::anyhow!("{level}: reading the places table {file}: {e}"))?;
        model.apply_places(&rows);
    }
    Ok(model)
}

/// The literary reading, `lit:` — litogramma, TEI, USX and OSIS
/// through atrep's importers with every inline simmere kept as a
/// node named by its sim, its genoses as traits and its aphanes
/// monosims as properties (`//quotation<said>[::prosopon =
/// "tom"]`), then the corpus reading's tokens beneath, each token
/// answering the spans that cover it. The same `?conllu=`,
/// `?annotate=`, `?desm=` options as `corpus:`; `?format=` names
/// the vocabulary (`tei`, `usx`, `osis`, `atd`) when the file
/// does not.
pub fn lit_level(
    rest: &str,
    allow_shell: bool,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    let (rest, conllu, annotate, desm, format, cast, places, scheme, modernize, notes) = match rest
        .split_once('?')
    {
        Some((p, q)) => {
            let (
                mut conllu,
                mut annotate,
                mut desm,
                mut format,
                mut cast,
                mut places,
                mut scheme,
                mut modernize,
                mut notes,
            ) = (None, None, Vec::new(), None, None, None, None, None, None);
            for pair in q.split('&') {
                match pair.split_once('=') {
                    Some(("conllu", v)) => conllu = Some(v.to_string()),
                    Some(("annotate", v)) => annotate = Some(v.to_string()),
                    Some(("desm", v)) => desm.push(v.to_string()),
                    Some(("cast", v)) => cast = Some(v.to_string()),
                    Some(("places", v)) => places = Some(v.to_string()),
                    Some(("format", v)) => format = Some(v.to_string()),
                    Some(("scheme", v)) => scheme = Some(v.to_string()),
                    Some(("modernize", v)) => modernize = Some(v.to_string()),
                    Some(("notes", v)) => notes = Some(v.to_string()),
                    Some(("encoding", v)) => set_encoding_option(v, ctx)?,
                    _ => anyhow::bail!(
                        "unknown lit option {pair:?} — supported: conllu=, annotate=, desm=, cast=, places=, format=, scheme=, modernize=, notes=, encoding="
                    ),
                }
            }
            (
                p, conllu, annotate, desm, format, cast, places, scheme, modernize, notes,
            )
        }
        None => (
            rest,
            None,
            None,
            Vec::new(),
            None,
            None,
            None,
            None,
            None,
            None,
        ),
    };
    let target = Path::new(rest);
    if is_archive(target) && binary_text_kind(target).is_none() {
        anyhow::bail!(
            "lit: over an archive is not supported yet — mount it as text:{rest}, or lit: one member"
        );
    }
    // A word-processor file declares no literary vocabulary: its
    // prose reads under corpus:, its structure under text:.
    if let Some(kind) = binary_text_kind(target) {
        if kind == "dsl" {
            anyhow::bail!(
                "lit: reads TEI, USX, OSIS, USFM, FictionBook, RNC, OpenCorpora, PROIEL and litogramma — {rest} is a dictionary, no literary edition; koine:{rest} reads it (text:{rest} and corpus:{rest} alike)"
            );
        }
        let what = match kind {
            "docx" => "a Word document",
            "doc" => "a Word 97 document",
            "odt" => "an OpenDocument text",
            _ => "an EPUB",
        };
        anyhow::bail!(
            "lit: reads TEI, USX, OSIS, USFM, FictionBook, RNC, OpenCorpora, PROIEL and litogramma — {rest} is {what}, whose prose corpus:{rest} reads (text:{rest} its structure)"
        );
    }
    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    let kind = match format.as_deref() {
        Some(k) => k.to_string(),
        None => match ext.as_deref() {
            Some("atd" | "atk") => "atd".to_string(),
            Some("usx") => "usx".to_string(),
            Some("usfm" | "sfm") => "usfm".to_string(),
            Some("fb2") => "fb2".to_string(),
            _ => {
                let text = read_prose(target, ctx)?;
                match quarb_text_koine::detect_xml_kind(&text) {
                    Some(k) => k.to_string(),
                    None if is_xml(Some(target), &text) => anyhow::bail!(
                        "lit: reads TEI, USX, OSIS, FictionBook, RNC, OpenCorpora, PROIEL and litogramma — {rest} declares none of them (koine:{rest} reads the other XML vocabularies)"
                    ),
                    None => anyhow::bail!(
                        "lit: reads TEI, USX, OSIS, FictionBook, RNC, OpenCorpora, PROIEL and litogramma — corpus:{rest} reads plain prose"
                    ),
                }
            }
        },
    };
    let mut model = match kind.as_str() {
        "atd" | "atk" => quarb_text_koine::parse_lit_file_with(target, scheme.as_deref())
            .with_context(|| format!("reading {} as an atrep document", target.display()))?,
        "tei" | "usx" | "osis" | "fb2" | "usfm" | "rnc" | "opencorpora" | "proiel" => {
            quarb_text_koine::parse_lit_xml_as_with(
                &read_prose(target, ctx)?,
                &kind,
                scheme.as_deref(),
            )
            .with_context(|| format!("reading {} as {}", target.display(), kind.to_uppercase()))?
        }
        other => anyhow::bail!(
            "lit: reads TEI, USX, OSIS, USFM, RNC, OpenCorpora, PROIEL and litogramma — not {other} (koine:{rest} reads the other XML vocabularies)"
        ),
    };
    model.set_document_path(rest);
    finish_corpus(
        model,
        conllu,
        annotate,
        &desm,
        cast,
        places,
        modernize,
        notes,
        allow_shell,
        "lit",
        ctx,
    )
}

/// Run an annotator command under `sh -c` with `text` on its
/// stdin, returning its stdout — CoNLL-U, by the contract of
/// `corpus:…?annotate=`.
pub fn annotate_with(cmd: &str, text: &str) -> anyhow::Result<String> {
    use std::io::Write;
    let mut child = std::process::Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .with_context(|| format!("running annotator {cmd:?}"))?;
    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(text.as_bytes())
            .context("feeding the annotator")?;
    }
    let out = child
        .wait_with_output()
        .context("waiting for the annotator")?;
    anyhow::ensure!(
        out.status.success(),
        "annotator {cmd:?} failed: {}",
        out.status
    );
    String::from_utf8(out.stdout).context("annotator output is not UTF-8")
}

/// A prose document's text: the file, or standard input for `-`
/// (`text:-`, `corpus:-` — the pipe reading `text:/dev/stdin`
/// spelled the Unix way), a leading BOM dropped.
/// Read a source's text at the source boundary: `?encoding=` on
/// the target, then a byte-order mark, then what the bytes declare
/// (an XML prologue, an HTML meta charset), then strict UTF-8 —
/// refused, naming the option, when none applies. The record of
/// the decoding waits for `run` / `open_mount` to put it on the
/// root. Standard input (`-`) is a source like any other.
pub fn read_prose(target: &Path, ctx: &SourceCtx) -> anyhow::Result<String> {
    let explicit = ctx.encoding();
    let decoded = if target == Path::new("-") {
        let mut bytes = Vec::new();
        std::io::stdin()
            .read_to_end(&mut bytes)
            .context("reading standard input")?;
        quarb_compose::source_decoding(explicit.as_deref())
            .and_then(|d| d.decode("-", &bytes))
            .map_err(|e| anyhow::anyhow!("reading standard input: {e}"))?
    } else {
        quarb_compose::read_source(target, explicit.as_deref())
            .with_context(|| format!("reading {}", target.display()))?
            .map_err(|e| anyhow::anyhow!("reading {}: {e}", target.display()))?
    };
    ctx.set_record(decoded.encoding, decoded.replaced);
    Ok(decoded.text)
}

/// Take `?encoding=LABEL` off a plain target (one that is not a
/// path as written): the one option a file or directory mount
/// takes. The label is validated here, before anything is read.
/// Prefixed readers (`text:`, `corpus:`, `lit:`, `koine:`) parse
/// the option among their own.
pub fn take_encoding_option(p: &Path, ctx: &SourceCtx) -> anyhow::Result<PathBuf> {
    ctx.clear_encoding();
    ctx.set_csv(quarb_csv::CsvOptions::default());
    let Some(s) = p.to_str() else {
        return Ok(p.to_path_buf());
    };
    // A scheme or reader prefix (`text:`, `imap://`, `lines:`)
    // parses its own options; a path as written is taken as is.
    let prefixed = s.split_once(':').is_some_and(|(pre, _)| {
        !pre.is_empty()
            && pre
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c))
    });
    if p.exists() || !s.contains('?') || prefixed {
        return Ok(p.to_path_buf());
    }
    let (path, query) = s.rsplit_once('?').unwrap();
    for pair in query.split('&').filter(|p| !p.is_empty()) {
        match pair.split_once('=') {
            Some(("encoding", label)) => {
                quarb::source::encoding_for_label(label).ok_or_else(|| {
                    anyhow::anyhow!("{}: unknown encoding label {label:?}", p.display())
                })?;
                ctx.set_encoding(label);
            }
            Some(("delimiter", label)) => set_csv_option(|c| c.with_delimiter_label(label), ctx)?,
            Some(("decimal", label)) => set_csv_option(|c| c.with_decimal_label(label), ctx)?,
            _ => anyhow::bail!(
                "unknown option {pair:?} on {path} — a file or folder takes encoding=, \
                 delimiter=, decimal="
            ),
        }
    }
    Ok(PathBuf::from(path))
}

/// `delimiter=` / `decimal=` as read off a target: validated here,
/// before anything is read.
pub fn set_csv_option(
    f: impl FnOnce(quarb_csv::CsvOptions) -> Result<quarb_csv::CsvOptions, quarb_csv::CsvError>,
    ctx: &SourceCtx,
) -> anyhow::Result<()> {
    let next = f(ctx.csv()).map_err(|e| anyhow::anyhow!("{e}"))?;
    ctx.set_csv(next);
    Ok(())
}

/// The delimited-file options in force for the target being
/// opened (`?delimiter=` / `?decimal=`), for a front-end that
/// composes its own adapter with them.
pub fn csv_options(ctx: &SourceCtx) -> quarb_csv::CsvOptions {
    ctx.csv()
}

/// Put the decoding record, if a read made one, on the adapter's
/// root: `::::encoding` and `::::replaced` answer there.
pub fn take_source_record(ctx: &SourceCtx) -> Option<(String, usize)> {
    ctx.take_record()
}

/// `DIR[?options]` when the target is a folder of documents: a
/// directory that holds no treebank. The options ride to every
/// document in it.
pub fn document_dir(rest: &str) -> Option<(&Path, Option<String>)> {
    let (path, opts) = match rest.split_once('?') {
        Some((p, q)) => (p, Some(q.to_string())),
        None => (rest, None),
    };
    let dir = Path::new(path);
    (dir.is_dir() && !holds_treebank(dir) && holds_documents(dir)).then_some((dir, opts))
}

/// Whether a prose document lies anywhere beneath a directory — a
/// folder with neither a treebank nor a document is refused by the
/// treebank reading, naming what it looked for.
pub fn holds_documents(dir: &Path) -> bool {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return false;
    };
    entries.filter_map(|e| e.ok().map(|e| e.path())).any(|p| {
        if p.is_dir() {
            return holds_documents(&p);
        }
        let ext = p
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_ascii_lowercase());
        matches!(
            ext.as_deref(),
            Some(
                "md" | "markdown"
                    | "html"
                    | "htm"
                    | "txt"
                    | "tex"
                    | "xml"
                    | "fb2"
                    | "djvu"
                    | "rst"
                    | "org"
                    | "atd"
                    | "atk"
                    | "usfm"
                    | "sfm"
            )
        ) || binary_text_kind(&p).is_some()
    })
}

/// A folder of documents as one arbor: the folder tree, subfolders
/// walked, each document leaf read by `reader` (or, where it
/// declines, at the text level when the format is one the composed
/// view parses itself).
pub fn document_folder(
    dir: &Path,
    hidden: bool,
    no_ignore: bool,
    reader: quarb_compose::DocumentReader,
    ctx: &SourceCtx,
) -> anyhow::Result<ComposeAdapter<FsAdapter>> {
    let opts = FsOptions {
        hidden,
        respect_ignore: !no_ignore,
    };
    Ok(
        ComposeAdapter::with_source_paths(FsAdapter::with_options(dir, opts)?, |fs, n| {
            Some(fs.path(n))
        })
        .with_document_graft(DocumentGraft::Text)
        .with_document_reader(reader)
        .with_decoding(current_decoding(ctx)?)
        .with_csv_options(ctx.csv()),
    )
}

/// The reader a folder mount hands its document leaves to, by the
/// prefix's reading: `text` (the formats the composed view does not
/// parse itself), `corpus` (every prose format, with its tokens and
/// sentences), `lit` (the marked-up editions). A file the reading
/// does not take, or cannot read, stays a plain leaf.
pub fn folder_reader(
    level: &'static str,
    opts: Option<String>,
    allow_shell: bool,
    ctx: &SourceCtx,
) -> quarb_compose::DocumentReader {
    // The reader outlives the open: it carries its own copy of the
    // boundary (the options are what matter; a leaf's record stays
    // with its graft).
    let ctx = ctx.clone();
    Rc::new(move |path: &Path| {
        let ctx = &ctx;
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_ascii_lowercase())?;
        let spec = match &opts {
            Some(q) => format!("{}?{q}", path.display()),
            None => path.display().to_string(),
        };
        let edition = matches!(ext.as_str(), "atd" | "atk" | "usfm" | "sfm" | "fb2");
        match level {
            "corpus" => {
                let prose = matches!(
                    ext.as_str(),
                    "md" | "markdown"
                        | "html"
                        | "htm"
                        | "txt"
                        | "tex"
                        | "xml"
                        | "rst"
                        | "org"
                        | "rtf"
                ) || edition
                    || binary_text_kind(path).is_some();
                prose
                    .then(|| corpus_level(&spec, allow_shell, ctx).ok())
                    .flatten()
            }
            "lit" => (edition || ext == "xml")
                .then(|| lit_level(&spec, allow_shell, ctx).ok())
                .flatten(),
            _ => {
                if edition {
                    koine_level(&spec, ctx).ok()
                } else if let Some(kind) = binary_text_kind(path) {
                    binary_text_level(kind, &std::fs::read(path).ok()?, path, ctx).ok()
                } else if matches!(ext.as_str(), "tex" | "rst" | "org" | "rtf") {
                    let mut m = text_level(&read_prose(path, ctx).ok()?, Some(path), ctx).ok()?;
                    m.set_document_path(&path.display().to_string());
                    Some(m)
                } else {
                    None
                }
            }
        }
    })
}

/// Whether a directory holds a treebank — a `.conllu` file anywhere
/// beneath it. `text:DIR` reads the treebank when there is one, and
/// the folder's documents otherwise.
pub fn holds_treebank(dir: &Path) -> bool {
    quarb_text::read_conllu_dir(dir).is_ok_and(|files| !files.is_empty())
}

/// A directory's treebank files as one document (`text:DIR`): the
/// `.conllu` files beneath it, a level-1 section each, without
/// tokens — `corpus:DIR` is the same set with them.
pub fn conllu_dir_text(dir: &Path, ctx: &SourceCtx) -> anyhow::Result<quarb_text::TextModel> {
    let files = quarb_text::read_conllu_dir_with(dir, &current_decoding(ctx)?)
        .map_err(|e| anyhow::anyhow!("text: over a directory reads its treebank — {e}"))?;
    let refs: Vec<(&str, &str)> = files
        .iter()
        .map(|(n, t)| (n.as_str(), t.as_str()))
        .collect();
    let mut m = quarb_text::TextModel::parse_conllu_text_set(&refs)
        .map_err(|e| anyhow::anyhow!("reading {} as CoNLL-U: {e}", dir.display()))?;
    m.set_document_path(&dir.display().to_string());
    Ok(m)
}

pub fn text_level(
    text: &str,
    path: Option<&Path>,
    ctx: &SourceCtx,
) -> anyhow::Result<quarb_text::TextModel> {
    apply_references(text_level_inner(text, path)?, ctx)
}

pub fn text_level_inner(text: &str, path: Option<&Path>) -> anyhow::Result<quarb_text::TextModel> {
    let ext = path
        .and_then(|p| p.extension())
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    Ok(match ext.as_deref() {
        // A treebank read as a document: sections,
        // paragraphs and sentences from its comments.
        Some("conllu" | "conllup") => quarb_text::TextModel::parse_conllu_text(text)
            .map_err(|e| anyhow::anyhow!("reading CoNLL-U: {e}"))?,
        Some("html" | "htm") => quarb_text_html::parse(text),
        Some("md" | "markdown") => quarb_text_markdown::parse(text),
        Some("tex" | "latex") => quarb_text_latex::parse(text),
        Some("txt") => quarb_text::TextModel::parse_plain(text),
        // Rich Text: the declared outline levels, lists, tables,
        // notes and fields, its `\'xx` bytes in the code page the
        // header names.
        Some("rtf") => quarb_text_rtf::parse(text.as_bytes())
            .map_err(|e| anyhow::anyhow!("reading RTF: {e}"))?,
        // Scripture in USFM: through atrep's importer, as the
        // koine route reads it.
        Some("usfm" | "sfm") => quarb_text_koine::parse_xml_as(text, "usfm")
            .map_err(|e| anyhow::anyhow!("reading USFM: {e}"))?,
        // FictionBook: by its extension, or an `.xml` declaring the
        // FictionBook namespace — through the koine route.
        Some("fb2") => quarb_text_koine::parse_xml_as(text, "fb2")
            .map_err(|e| anyhow::anyhow!("reading FictionBook: {e}"))?,
        // An XML vocabulary (TEI, DocBook, OSIS, FictionBook, …)
        // reads through the koine route by its declared identity,
        // as `corpus:` and `lit:` read it; a `.xml` declaring none
        // is refused with the pointer rather than read as HTML.
        Some("xml" | "tei") => match quarb_text_koine::detect_xml_kind(text) {
            Some(kind) => quarb_text_koine::parse_xml_as(text, kind)
                .map_err(|e| anyhow::anyhow!("reading {kind} through the koine route: {e}"))?,
            None => anyhow::bail!(
                "the file declares no XML identity the text level knows (no namespace, \
                 DOCTYPE, or unambiguous root) — name the vocabulary with \
                 koine:FILE?format=tei|docbook|jats|usx|osis|fb2|rnc|opencorpora|proiel, \
                 or read the markup itself with xml:FILE"
            ),
        },
        _ if text.trim_start().starts_with('<')
            && let Some(kind) = quarb_text_koine::detect_xml_kind(text) =>
        {
            quarb_text_koine::parse_xml_as(text, kind)
                .map_err(|e| anyhow::anyhow!("reading {kind} through the koine route: {e}"))?
        }
        _ if text.trim_start().starts_with('<') => quarb_text_html::parse(text),
        // A pipe or an extensionless file carrying CoNLL-U.
        None if quarb_text::looks_like_conllu(text) => {
            quarb_text::TextModel::parse_conllu_text(text)
                .map_err(|e| anyhow::anyhow!("reading CoNLL-U: {e}"))?
        }
        _ => quarb_text::TextModel::parse_plain(text),
    })
}

/// An input argument's explicit mount alias: `NAME=TARGET` mounts
/// TARGET as `/NAME`. The prefix must look like a mount name (a
/// letter or `_`, then letters, digits, `_`, `-`; letters in any
/// script, so `достоевский=corpus:…` names its mount as a Russian
/// page would) and the argument must not name an existing file —
/// a real file called `a=b.json` still mounts by its stem.
pub fn split_alias(p: &Path) -> Option<(String, PathBuf)> {
    let s = p.to_str()?;
    let (name, target) = s.split_once('=')?;
    if target.is_empty() || p.exists() {
        return None;
    }
    let mut chars = name.chars();
    if !chars.next().is_some_and(|c| c.is_alphabetic() || c == '_') {
        return None;
    }
    if !chars.all(|c| c.is_alphanumeric() || c == '_' || c == '-') {
        return None;
    }
    Some((name.to_string(), PathBuf::from(target)))
}

/// A boxed adapter and its locator renderer, ready to mount.
/// What opening a target yields: the adapter behind the object-safe
/// trait, its locator renderer, and the adapter family it was
/// mounted through (`json`, `text`, `fs`, `sqlite`, `neo4j`, …),
/// the name a trace payload's meta carries.
pub struct Mounted {
    pub adapter: Box<dyn AstAdapter>,
    pub render: Box<dyn Fn(NodeId) -> String>,
    pub family: &'static str,
}

impl Mounted {
    /// Share `a` as the mounted adapter; the family is read off the
    /// adapter's type (its crate's name after `quarb-`, the inner
    /// type of a composed adapter).
    pub fn of<A: AstAdapter + 'static>(a: Rc<A>, render: Box<dyn Fn(NodeId) -> String>) -> Mounted {
        Mounted {
            adapter: Box::new(Shared(a)),
            render,
            family: family_of::<A>(),
        }
    }
}

/// The adapter family of a concrete adapter type: the last
/// `quarb_*` crate named in its type (so a composed adapter answers
/// for what it wraps), hyphenated, with the property-model readers
/// (YAML, TOML) under `json` as the session has always named them.
pub fn family_of<A: 'static>() -> &'static str {
    let name = std::any::type_name::<A>();
    let last = name
        .rmatch_indices("quarb")
        .next()
        .map(|(i, _)| &name[i..])
        .unwrap_or(name);
    let krate = last.split("::").next().unwrap_or(last);
    match krate {
        "quarb" => "quarb",
        "quarb_yaml" | "quarb_toml" | "quarb_cbor" => "json",
        "quarb_text_pdf" | "quarb_text_koine" | "quarb_text_djvu" => "text",
        other => Box::leak(
            other
                .strip_prefix("quarb_")
                .unwrap_or(other)
                .replace('_', "-")
                .into_boxed_str(),
        ),
    }
}

/// Mount a relational adapter with the same JSON-column graft the
/// single-input flow applies: `run_relational` wraps every
/// relational adapter in `ComposeAdapter`, and a mount must too —
/// otherwise a text column full of JSON navigates as a subtree in
/// one flow and comes back flat in the other. Under --no-graft
/// both flows skip the wrap.
pub fn mounted_relational<A: AstAdapter + 'static>(
    no_graft: bool,
    inner: A,
    outer_loc: impl Fn(&A, NodeId) -> String + 'static,
) -> Mounted {
    if no_graft {
        let a = Rc::new(inner);
        let r = a.clone();
        return Mounted::of(a, Box::new(move |n| outer_loc(&r, n)));
    }
    let a = Rc::new(ComposeAdapter::new(inner));
    let r = a.clone();
    Mounted::of(
        a,
        Box::new(move |n| r.locator(n, |o| outer_loc(r.outer(), o))),
    )
}

/// What opening a target consults — the subset of the CLI's flags
/// every front-end can supply.
#[derive(Default)]
pub struct OpenOpts {
    pub hidden: bool,
    pub no_ignore: bool,
    /// Opt directory mounts into grafting (the CLI's --graft).
    pub graft: bool,
    /// Disable grafting entirely (the CLI's --no-graft).
    pub no_graft: bool,
    pub refs: Option<PathBuf>,
    /// Let `sh(...)` / backtick stages run (the CLI's --allow-shell);
    /// the literary and corpus readings' `annotate=` needs it.
    pub allow_shell: bool,
}

/// Open any target qua speaks — filesystem paths, documents, and
/// the full adapter-scheme fleet (`gcl:`, `kafka:`, `neo4j://`,
/// …) — as a boxed adapter plus its locator renderer. The door
/// session tools use to mount what the CLI mounts.
pub fn open_target(target: &str, opts: &OpenOpts) -> anyhow::Result<Mounted> {
    open_mount(Path::new(target), opts)
}

/// Mount kaiv text by its extension's pipeline stage: `.kaiv` is
/// canonical, `.daiv` is authored (compile + denormalize), `.raiv`
/// is relational (denormalize). The file's directory anchors the
/// resolver, so `.!units` / `.!types` imports (and a sibling
/// `kaiv.kaiv`) resolve exactly as `kaiv build` there would.
pub fn parse_kaiv_ext(
    ext: &str,
    text: &str,
    dir: Option<&Path>,
) -> anyhow::Result<quarb_kaiv::KaivAdapter> {
    let parsed = match ext {
        "kaiv" => quarb_kaiv::KaivAdapter::parse_kaiv_at(text, dir),
        "raiv" => quarb_kaiv::KaivAdapter::parse_raiv_at(text, dir),
        _ => quarb_kaiv::KaivAdapter::parse_daiv_at(text, dir),
    };
    parsed.map_err(|e| anyhow::anyhow!("parsing {ext}: {e}"))
}

/// Open one input as a boxed adapter plus its locator renderer, for
/// mounting. Format detection matches the single-input flow. The
/// source boundary is applied here: `?encoding=` comes off the
/// target first, and the decoding record a read leaves goes on the
/// mount's root.
pub fn open_mount(p: &Path, cli: &OpenOpts) -> anyhow::Result<Mounted> {
    let ctx = &SourceCtx::default();
    let p = take_encoding_option(p, ctx)?;
    let _ = ctx.take_record();
    let m = open_mount_inner(&p, cli, ctx)?;
    ctx.clear_encoding();
    Ok(match take_source_record(ctx) {
        Some((encoding, replaced)) => Mounted {
            adapter: Box::new(quarb::source::Sourced {
                inner: m.adapter,
                encoding,
                replaced,
            }),
            render: m.render,
            family: m.family,
        },
        None => m,
    })
}

pub fn open_mount_inner(p: &Path, cli: &OpenOpts, ctx: &SourceCtx) -> anyhow::Result<Mounted> {
    if p.is_dir() {
        let opts = FsOptions {
            hidden: cli.hidden,
            respect_ignore: !cli.no_ignore,
        };
        if cli.graft {
            let a = Rc::new(
                ComposeAdapter::with_source_paths(FsAdapter::with_options(p, opts)?, |fs, n| {
                    Some(fs.path(n))
                })
                .with_decoding(current_decoding(ctx)?)
                .with_csv_options(ctx.csv()),
            );
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().path(o).display().to_string())),
            ));
        }
        let a = Rc::new(FsAdapter::with_options(p, opts)?);
        let r = a.clone();
        return Ok(Mounted::of(
            a,
            Box::new(move |n| r.path(n).display().to_string()),
        ));
    }
    if let Some(s) = p.to_str()
        && let Some(cmd) = s.strip_prefix("serve:")
    {
        let a = Rc::new(ServeAdapter::spawn(cmd).context("spawning served adapter")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // A `lines:` prefix mounts line atoms, matching the
    // single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("lines:")
        && !rest.is_empty()
    {
        let rest = &take_reader_encoding(rest, ctx)?;
        let target = Path::new(rest);
        let text = read_prose(target, ctx)?;
        let a = Rc::new(quarb_lines::LinesAdapter::parse(&text));
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // A `koine:` prefix takes the koine route, matching the
    // single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("koine:")
        && !rest.is_empty()
    {
        let a = Rc::new(koine_level(rest, ctx)?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("web:")
        && !rest.is_empty()
    {
        match web_level(rest, ctx)? {
            WebSite::Memory(a) => {
                let a = Rc::new(a);
                let r = a.clone();
                return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
            }
            WebSite::Sqlite(store) => {
                let a = Rc::new(quarb_web::WebAdapter::new(store));
                let r = a.clone();
                return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
            }
            WebSite::Postgres(store) => {
                let a = Rc::new(quarb_web::WebAdapter::new(store));
                let r = a.clone();
                return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
            }
        }
    }
    // The literary reading, matching the single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("lit:")
        && !rest.is_empty()
    {
        if let Some((dir, opts)) = document_dir(rest) {
            let a = Rc::new(document_folder(
                dir,
                cli.hidden,
                cli.no_ignore,
                folder_reader("lit", opts, cli.allow_shell, ctx),
                ctx,
            )?);
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().path(o).display().to_string())),
            ));
        }
        let a = Rc::new(lit_level(rest, cli.allow_shell, ctx)?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // The property reading (`json2:`), matching the single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("json2:")
        && !rest.is_empty()
    {
        let rest = &take_reader_encoding(rest, ctx)?;
        return Ok(match property_reading(Path::new(rest), ctx)? {
            PropertyDoc::Json(adapter) => {
                let a = Rc::new(adapter);
                let r = a.clone();
                Mounted::of(a, Box::new(move |n| r.pointer(n)))
            }
            PropertyDoc::Cbor(adapter) => {
                let a = Rc::new(adapter);
                let r = a.clone();
                Mounted::of(a, Box::new(move |n| r.pointer(n)))
            }
        });
    }
    // The corpus reading, matching the single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("corpus:")
        && !rest.is_empty()
    {
        if let Some((dir, opts)) = document_dir(rest) {
            let a = Rc::new(document_folder(
                dir,
                cli.hidden,
                cli.no_ignore,
                folder_reader("corpus", opts, cli.allow_shell, ctx),
                ctx,
            )?);
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().path(o).display().to_string())),
            ));
        }
        let a = Rc::new(corpus_level(rest, cli.allow_shell, ctx)?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // A `text:` prefix forces the text-level reading, matching the
    // single-input flow.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("text:")
        && !rest.is_empty()
    {
        let rest = &take_reader_encoding(rest, ctx)?;
        let target = Path::new(rest);
        // A directory of documents: the folder tree, each document
        // leaf read at the text level.
        if target.is_dir() && !holds_treebank(target) {
            let a = Rc::new(document_folder(
                target,
                cli.hidden,
                cli.no_ignore,
                folder_reader("text", None, cli.allow_shell, ctx),
                ctx,
            )?);
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().path(o).display().to_string())),
            ));
        }
        // A directory of treebank files: one document, a section
        // per file.
        if target.is_dir() {
            let a = Rc::new(conllu_dir_text(target, ctx)?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        // An archive at the text level, as on the single flow:
        // every marked-up member grafts as the reader's model.
        if is_archive(target) && binary_text_kind(target).is_none() {
            let a = Rc::new(
                ComposeAdapter::new(
                    ArchiveAdapter::open_with_decoding(target, current_decoding(ctx)?)
                        .context("opening archive")?,
                )
                .with_decoding(current_decoding(ctx)?)
                .with_csv_options(ctx.csv())
                .with_document_graft(DocumentGraft::Text),
            );
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().locator(o))),
            ));
        }
        // Native atrep files converge on the koine route.
        if target
            .extension()
            .and_then(|e| e.to_str())
            .is_some_and(|e| e.eq_ignore_ascii_case("atd") || e.eq_ignore_ascii_case("atk"))
        {
            let a = Rc::new(koine_level(rest, ctx)?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        if target
            .extension()
            .and_then(|e| e.to_str())
            .is_some_and(|e| e.eq_ignore_ascii_case("pdf"))
        {
            let bytes =
                std::fs::read(target).with_context(|| format!("reading {}", target.display()))?;
            let a = Rc::new(
                quarb_text_pdf::parse(&bytes)
                    .with_context(|| format!("reading {} as PDF", target.display()))?,
            );
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        if is_djvu(target) {
            let a = Rc::new(
                quarb_text_djvu::parse_file(target)
                    .with_context(|| format!("reading {} as DjVu", target.display()))?,
            );
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        if let Some(kind) = binary_text_kind(target) {
            let bytes =
                std::fs::read(target).with_context(|| format!("reading {}", target.display()))?;
            let a = Rc::new(
                binary_text_level(kind, &bytes, target, ctx)
                    .with_context(|| format!("reading {}", target.display()))?,
            );
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        // BibTeX / BibLaTeX: atrep's importer parses it into
        // bibliogramma, and the entries land as bib blocks.
        if target
            .extension()
            .and_then(|e| e.to_str())
            .is_some_and(|e| e.eq_ignore_ascii_case("bib"))
        {
            let text = read_prose(target, ctx)?;
            let a = Rc::new(quarb_text_koine::parse_bibtex(&text).with_context(|| {
                format!(
                    "reading {} as BibTeX through bibliogramma",
                    target.display()
                )
            })?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        let text = read_prose(target, ctx)?;
        let text = match text.strip_prefix('\u{feff}') {
            Some(rest) => rest.to_owned(),
            None => text,
        };
        let a = Rc::new(text_level(&text, Some(target), ctx)?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // A `code:` prefix forces the code-level reading, matching
    // the single-input flow; a directory mounts the composed,
    // grafted view with source leaves grafted at the code
    // level.
    if let Some(s) = p.to_str()
        && let Some(rest) = s.strip_prefix("code:")
        && !rest.is_empty()
    {
        let rest = &take_reader_encoding(rest, ctx)?;
        if cli.no_graft {
            anyhow::bail!(
                "--no-graft refuses the code: prefix: the prefix's whole \
                 meaning is the grafted code-level view"
            );
        }
        let target = Path::new(rest);
        if target.is_dir() {
            let opts = FsOptions {
                hidden: cli.hidden,
                respect_ignore: !cli.no_ignore,
            };
            let a = Rc::new(
                ComposeAdapter::with_source_paths(
                    FsAdapter::with_options(target, opts)?,
                    |fs, n| Some(fs.path(n)),
                )
                .with_source_graft(SourceGraft::Code)
                .with_decoding(current_decoding(ctx)?)
                .with_csv_options(ctx.csv()),
            );
            let r = a.clone();
            return Ok(Mounted::of(
                a,
                Box::new(move |n| r.locator(n, |o| r.outer().path(o).display().to_string())),
            ));
        }
        let a = Rc::new(
            quarb_code::CodeModel::open_with_decoding(target, &current_decoding(ctx)?)
                .with_context(|| format!("parsing {} at the code level", target.display()))?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("firestore://")
    {
        let a = Rc::new(FirestoreAdapter::connect(s).context("connecting to Firestore")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("datastore://")
    {
        let a = Rc::new(DatastoreAdapter::connect(s).context("connecting to Datastore")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && s.starts_with("mssql://")
    {
        return Ok(mounted_relational(
            cli.no_graft,
            MssqlAdapter::connect(s).context("connecting to SQL Server")?,
            |a, n| a.locator(n),
        ));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && s.starts_with("oracle://")
    {
        return Ok(mounted_relational(
            cli.no_graft,
            OracleAdapter::connect(s).context("connecting to Oracle")?,
            |a, n| a.locator(n),
        ));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("ldap://") || s.starts_with("ldaps://"))
    {
        let a = Rc::new(LdapAdapter::connect(s).context("connecting to LDAP")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("github:")
    {
        let a = Rc::new(GithubAdapter::connect(s).context("connecting to GitHub")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("gitlab:")
    {
        let a = Rc::new(GitlabAdapter::connect(s).context("connecting to GitLab")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("k8s:") || s.starts_with("kubernetes:"))
    {
        let a = Rc::new(KubernetesAdapter::connect(s).context("connecting to Kubernetes")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("gcl:") || s.starts_with("gcplogs:"))
    {
        let a = Rc::new(GclAdapter::open(s).context("reading Cloud Logging")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("cwl:") || s.starts_with("cloudwatch:"))
    {
        let a = Rc::new(CwlAdapter::open(s).context("reading CloudWatch Logs")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("ddl:") || s.starts_with("datadog:"))
    {
        let a = Rc::new(DdlAdapter::open(s).context("reading Datadog Logs")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("azl:") || s.starts_with("azlogs:"))
    {
        let a = Rc::new(AzlAdapter::open(s).context("reading Azure Monitor Logs")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && (s.starts_with("cfl:") || s.starts_with("cflogs:"))
    {
        let a = Rc::new(CflAdapter::open(s).context("reading Cloudflare logs")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && (s.starts_with("mongodb://") || s.starts_with("mongodb+srv://"))
    {
        let a = Rc::new(MongodbAdapter::connect(s).context("connecting to MongoDB")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("neo4j://")
    {
        let a = Rc::new(Neo4jAdapter::connect(s).context("connecting to Neo4j")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("dynamodb:")
    {
        let a = Rc::new(DynamodbAdapter::connect(s).context("connecting to DynamoDB")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("neptune://")
    {
        let a = Rc::new(NeptuneAdapter::connect(s).context("connecting to Neptune")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("redis://")
    {
        let a = Rc::new(RedisAdapter::connect(s).context("connecting to Redis")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("rediss://")
    {
        let a = Rc::new(RedisAdapter::connect(s).context("connecting to Redis")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("falkor://")
    {
        let a = Rc::new(FalkorAdapter::connect(s).context("connecting to FalkorDB")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("memgraph://")
    {
        let a = Rc::new(MemgraphAdapter::connect(s).context("connecting to Memgraph")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("arango://")
    {
        let a = Rc::new(ArangoAdapter::connect(s).context("connecting to ArangoDB")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("sparql:")
    {
        let a = Rc::new(SparqlAdapter::connect(s).context("connecting to the SPARQL endpoint")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("age://")
    {
        let a = Rc::new(AgeAdapter::connect(s).context("connecting to AGE")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(feature = "kuzu")]
    if let Some(s) = p.to_str()
        && s.starts_with("kuzu:")
    {
        let a = Rc::new(KuzuAdapter::open(s).context("opening Kuzu database")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("kafka:")
    {
        let a = Rc::new(KafkaAdapter::connect(s).context("connecting to Kafka")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(s) = p.to_str()
        && s.starts_with("cosmos://")
    {
        let a = Rc::new(CosmosAdapter::connect(s).context("connecting to Cosmos DB")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && s.starts_with("athena:")
    {
        return Ok(mounted_relational(
            cli.no_graft,
            AthenaAdapter::connect(s).context("connecting to Athena")?,
            |a, n| a.locator(n),
        ));
    }
    if let Some(s) = p.to_str()
        && let Some(repo) = s.strip_prefix("git:")
    {
        let a = Rc::new(
            GitAdapter::open_with_decoding(std::path::Path::new(repo), current_decoding(ctx)?)
                .context("opening git repository")?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(unix)]
    if let Some(s) = p.to_str()
        && let Some(vault) = s
            .strip_prefix("metatheca:")
            .or_else(|| s.strip_prefix("mt:"))
    {
        let a = Rc::new(
            MetathecaAdapter::open_with_decoding(
                std::path::Path::new(vault),
                current_decoding(ctx)?,
            )
            .context("opening metatheca vault")?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(not(unix))]
    if let Some(s) = p.to_str()
        && (s.starts_with("metatheca:") || s.starts_with("mt:"))
    {
        anyhow::bail!(
            "metatheca vaults open on unix only (their storage layer locks files the unix way)"
        );
    }
    if let Some(s) = p.to_str()
        && s.starts_with("firebase://")
    {
        let adapter = match &cli.refs {
            Some(f) => {
                let text = std::fs::read_to_string(f)
                    .with_context(|| format!("reading refs file {}", f.display()))?;
                let refs = quarb_firebase::parse_refs(&text).context("parsing refs")?;
                FirebaseAdapter::connect_with_refs(s, refs)
            }
            None => FirebaseAdapter::connect(s),
        }
        .context("connecting to Firebase")?;
        let a = Rc::new(adapter);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && s.starts_with("bigquery://")
    {
        return Ok(mounted_relational(
            cli.no_graft,
            BigqueryAdapter::connect(s).context("connecting to BigQuery")?,
            |a, n| a.locator(n),
        ));
    }
    #[cfg(feature = "heavy")]
    if let Some(s) = p.to_str()
        && s.starts_with("mysql://")
    {
        return Ok(mounted_relational(
            cli.no_graft,
            MysqlAdapter::connect(s).context("connecting to MySQL")?,
            |a, n| a.locator(n),
        ));
    }
    if let Some(s) = p.to_str()
        && is_pg_config(s)
    {
        return Ok(mounted_relational(
            cli.no_graft,
            PostgresAdapter::connect(s).context("connecting to PostgreSQL")?,
            |a, n| a.locator(n),
        ));
    }
    if let Some(t) = p.to_str()
        && let Some(mb) = t.strip_prefix("mail:")
    {
        let a = Rc::new(
            MaildirAdapter::open_with_decoding(std::path::Path::new(mb), mail_decoding(ctx)?)
                .context("opening mailbox")?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(t) = p.to_str()
        && t.starts_with("gsheet://")
    {
        let a = Rc::new(GsheetAdapter::connect(t).context("connecting to Google Sheets")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(t) = p.to_str()
        && (t.starts_with("gs://") || t.starts_with("s3://") || t.starts_with("az://"))
    {
        if cli.no_graft {
            let a = Rc::new(ObjstoreAdapter::connect(t).context("connecting to bucket")?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        let a = Rc::new(
            ComposeAdapter::new(ObjstoreAdapter::connect(t).context("connecting to bucket")?)
                .with_decoding(current_decoding(ctx)?)
                .with_csv_options(ctx.csv()),
        );
        let r = a.clone();
        return Ok(Mounted::of(
            a,
            Box::new(move |n| r.locator(n, |o| r.outer().locator(o))),
        ));
    }
    if let Some(t) = p.to_str()
        && (t.starts_with("imap://") || t.starts_with("imaps://"))
    {
        let a = Rc::new(
            ImapAdapter::connect_with_decoding(t, mail_decoding(ctx)?)
                .context("connecting to IMAP")?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // Source code: files with a tree-sitter grammar parse into
    // their syntax tree, matching the single-input flow.
    if p.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| quarb_tree_sitter::supported(&e.to_ascii_lowercase()))
    {
        let a = Rc::new(TreeSitterAdapter::open(p).context("parsing source file")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // Spreadsheets before the archive check — .xlsx/.ods ARE zips (PK
    // magic), but the sheets are the point, not the raw XML entries.
    if let Some(ext) = p.extension().and_then(|e| e.to_str())
        && matches!(ext.to_ascii_lowercase().as_str(), "xlsx" | "xls" | "ods")
    {
        let a = Rc::new(XlsxAdapter::open(p).context("opening workbook")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // DuckDB databases, by extension.
    #[cfg(feature = "heavy")]
    if p.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("duckdb") || e.eq_ignore_ascii_case("ddb"))
    {
        return Ok(mounted_relational(
            cli.no_graft,
            DuckdbAdapter::open(p).context("opening DuckDB database")?,
            |a, n| a.locator(n),
        ));
    }
    if is_djvu(p) {
        anyhow::bail!(
            "{}: a DjVu reads at the text level — text:{}",
            p.display(),
            p.display()
        );
    }
    if p.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("pdf"))
    {
        let bytes = std::fs::read(p).with_context(|| format!("reading {}", p.display()))?;
        let a = Rc::new(
            quarb_pdf::PdfAdapter::load(&bytes)
                .with_context(|| format!("reading {} as PDF", p.display()))?,
        );
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if is_archive(p) {
        if cli.no_graft {
            let a = Rc::new(
                ArchiveAdapter::open_with_decoding(p, current_decoding(ctx)?)
                    .context("opening archive")?,
            );
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        let a = Rc::new(
            ComposeAdapter::new(
                ArchiveAdapter::open_with_decoding(p, current_decoding(ctx)?)
                    .context("opening archive")?,
            )
            .with_decoding(current_decoding(ctx)?)
            .with_csv_options(ctx.csv()),
        );
        let r = a.clone();
        return Ok(Mounted::of(
            a,
            Box::new(move |n| r.locator(n, |o| r.outer().locator(o))),
        ));
    }
    // CBOR is binary: dispatch on the extension before the text
    // read, matching the single-input flow.
    if p.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("cbor"))
    {
        let bytes = std::fs::read(p).with_context(|| format!("reading {}", p.display()))?;
        let a = Rc::new(quarb_cbor::CborAdapter::parse(&bytes).context("parsing CBOR")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.pointer(n))));
    }
    if is_sqlite(p) {
        let refs = relational_refs(&cli.refs)?;
        return Ok(mounted_relational(
            cli.no_graft,
            SqliteAdapter::open_with_refs(p, &refs).context("opening SQLite database")?,
            |a, n| a.locator(n),
        ));
    }
    let text = read_prose(p, ctx)?;
    // Strip a leading UTF-8 BOM, as the single-input flow does: it
    // breaks JSON parsing and slips past the XML/HTML sniffers.
    let text = match text.strip_prefix('\u{feff}') {
        Some(rest) => rest.to_owned(),
        None => text,
    };
    let path = Some(p);
    if is_quarb(path) {
        let a = Rc::new(quarb::reflect::QueryArbor::parse(&text).context("parsing Quarb query")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(ext) = path.and_then(|p| p.extension()).and_then(|e| e.to_str())
        && matches!(ext.to_ascii_lowercase().as_str(), "daiv" | "kaiv" | "raiv")
    {
        let dir = path.and_then(|p| p.parent());
        let a = Rc::new(parse_kaiv_ext(&ext.to_ascii_lowercase(), &text, dir)?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    // YAML/TOML/Markdown are extension-only, matching the single-input
    // flow (YAML/TOML share the JSON pointer model; Markdown locates).
    if let Some(ext) = path.and_then(|p| p.extension()).and_then(|e| e.to_str()) {
        let ext = ext.to_ascii_lowercase();
        let ext = ext.as_str();
        if matches!(ext, "yaml" | "yml") {
            let a = Rc::new(quarb_yaml::parse(&text).context("parsing YAML")?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.pointer(n))));
        }
        if ext == "toml" {
            let a = Rc::new(quarb_toml::parse(&text).context("parsing TOML")?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.pointer(n))));
        }
        if matches!(ext, "md" | "markdown") {
            let a = Rc::new(quarb_markdown::parse(&text));
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        // Plain text mounts at the text level, matching the
        // single-input flow.
        if ext == "txt" {
            let a = Rc::new(quarb_text::TextModel::parse_plain(&text));
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        if matches!(ext, "conllu" | "conllup") {
            let a = Rc::new(
                quarb_text::TextModel::parse_conllu_text(&text)
                    .map_err(|e| anyhow::anyhow!("reading CoNLL-U: {e}"))?,
            );
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
        if matches!(ext, "jsonl" | "ndjson") {
            let a = Rc::new(JsonAdapter::parse_lines(&text).context("parsing JSONL")?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.pointer(n))));
        }
        if matches!(ext, "atd" | "atk" | "usfm" | "sfm") {
            let dir = path.and_then(|p| p.parent()).unwrap_or(Path::new("."));
            let a = Rc::new(AtrepAdapter::parse_str(&text, dir).context("parsing atrep document")?);
            let r = a.clone();
            return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
        }
    }
    if is_atrep(&text) {
        let dir = path
            .and_then(|p| p.parent())
            .unwrap_or_else(|| Path::new("."));
        let a = Rc::new(AtrepAdapter::parse_str(&text, dir).context("parsing atrep document")?);
        let r = a.clone();
        return Ok(Mounted::of(a, Box::new(move |n| r.locator(n))));
    }
    if let Some(delim) = csv_delimiter(path) {
        let a =
            Rc::new(CsvAdapter::parse_declared(&text, delim, ctx.csv()).context("parsing CSV")?);
        let r = a.clone();
        Ok(Mounted::of(a, Box::new(move |n| r.locator(n))))
    } else if is_xml(path, &text) {
        let a = Rc::new(XmlAdapter::parse(&text).context("parsing XML")?);
        let r = a.clone();
        Ok(Mounted::of(a, Box::new(move |n| r.locator(n))))
    } else if is_html(path, &text) {
        let a = Rc::new(HtmlAdapter::parse(&text));
        let r = a.clone();
        Ok(Mounted::of(a, Box::new(move |n| r.locator(n))))
    } else {
        // Whole-document JSON first, per-line (JSONL) second —
        // matching the single-input flow.
        let a = match JsonAdapter::parse(&text) {
            Ok(a) => Rc::new(a),
            Err(e) => match JsonAdapter::parse_lines(&text) {
                Ok(a) => Rc::new(a),
                Err(_) => return Err(e).context("parsing JSON"),
            },
        };
        let r = a.clone();
        Ok(Mounted::of(a, Box::new(move |n| r.pointer(n))))
    }
}