logoxide 0.1.1

Bibles, commentaries, lexicons, books, and media as versioned packages: read from USX, USFM, and more, cached, and shared
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
/*!
Catalogs: lists of packages to search, so a Bible can be found by its name or language instead of
its URL. A catalog holds what each package is and where it comes from, never its text, so anyone
can host one (a file in a git repository is enough). This module reads **logoxide catalogs**
(`catalog.toml`): packages published as git repositories or as a registry's archives, each as its
`logoxide.toml` describes it (`lx catalog entry <url>` writes an entry). Other publishers' own
lists (eBible.org's `translations.csv`, [`EBIBLE_CATALOG`]; fetch.bible's `manifest.json`,
[`FETCH_BIBLE_CATALOG`]; CCEL's page of its Bibles, [`CCEL_CATALOG`]; a SWORD repository's
`mods.d.tar.gz`, [`SWORD_CATALOG`] CrossWire's) are read by the `logoxide-import` crate into the
same [`Catalog`], each entry an [`CatalogSource::External`] that says what to download; the
package manager knows no publisher, only what an entry says of itself.

```toml
# catalog.toml
name = "Bibles in English"

[[package]]
id = "bsb"
name = "Berean Standard Bible"
abbreviation = "BSB"
language = "en"
license = "LicenseRef-PublicDomain"
git = "https://github.com/someone/bsb"
```

A vault names its catalogs in `[settings] catalogs` (URLs or paths); without any, the four
publishers' lists are searched ([`DEFAULT_CATALOGS`]).

**Registries** are catalogs with a name (`[settings.registries]`), so a package can be named
through one: `dorean-local:dorean-collection` by its id, or `dorean-local:books/romans` by its
path there. A registry is a catalog, or a folder of package repositories: each git repository
with a `logoxide.toml` in it, at any depth, is a package ([`Catalog::from_packages`]), named by
its path in the folder and found at its `file://` URL.

```toml
[settings.registries]
dorean-local = "~/Documents/logoxide-dorean-repos"
```
*/

use serde::{Deserialize, Serialize};

use crate::{
    error::{LogoxideError, Result},
    manifest::{Direction, PackageKind},
};

/// eBible.org's list of the translations it publishes
pub const EBIBLE_CATALOG: &str = "https://ebible.org/Scriptures/translations.csv";

/// fetch.bible's list of the translations it publishes
pub const FETCH_BIBLE_CATALOG: &str = "https://v1.fetch.bible/manifest.json";

/// CCEL's page of the Bibles it publishes (HTML: it has no other list)
pub const CCEL_CATALOG: &str = "https://ccel.org/ccel/bible/";

/// CCEL's index of its commentaries (its genre `COM`)
pub const CCEL_COMMENTARIES_CATALOG: &str = "https://ccel.org/index/genre/gen-COM";

/// CrossWire's main SWORD repository's list of modules (every module's conf)
pub const SWORD_CATALOG: &str = "https://crosswire.org/ftpmirror/pub/sword/raw/mods.d.tar.gz";

/// The catalogs searched when a vault names none
pub const DEFAULT_CATALOGS: [&str; 5] = [
    EBIBLE_CATALOG,
    FETCH_BIBLE_CATALOG,
    CCEL_CATALOG,
    CCEL_COMMENTARIES_CATALOG,
    SWORD_CATALOG,
];

/// Where a listed package comes from
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CatalogSource {
    /// A logoxide package in a git repository
    Git { url: String },
    /// A package a registry publishes as archives (its entry's `releases`): `catalog` is the
    /// registry's catalog, which a dependency names as its `registry`
    Registry { catalog: String },
    /**
    Another publisher's download, imported into a package (by `logoxide-import`, which lists
    them): what the package manager needs to know of it, and nothing of the publisher
    */
    External {
        /// Its import name, as `lx import` takes it: a prefix naming the publisher
        /// (`ebible`, `fetch`, `ccel`, `sword`), a colon, and the publisher's own id
        /// (`ebible:eng-asv`, `sword:crosswire/KJV`)
        import: String,
        /// The download the import is made from (the first of a SWORD module's zips), which
        /// the import writes as `[package.upstream] url`, so [`Catalog::find_upstream`] can
        /// find an entry by it
        #[serde(default, skip_serializing_if = "Option::is_none")]
        upstream: Option<String>,
        /// Whether a newer download always has a newer `version` here, so an import at the
        /// catalog's version needn't be downloaded again to know it's current: eBible.org's
        /// update dates and SWORD modules' `Version`s do; fetch.bible's year and CCEL's
        /// (unlisted) edition don't
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        tracks_versions: bool,
        /// Each file the import needs, by name, with the URLs to try in turn (a zip; fetch.bible's
        /// books, each by its code), so every host downloads the same files the same way and
        /// hands their bytes, by name, to `logoxide-import`
        downloads: Vec<Download>,
    },
}

/// A file an import needs: `name` is what the import knows it by, and it comes from the first of
/// `urls` that answers
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Download {
    pub name: String,
    pub urls: Vec<String>,
}

/// One release a registry publishes: its archive and what it hashes to
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogRelease {
    pub version: String,
    /// Its `.tar.gz` (relative to the catalog in its file; whole once read)
    pub url: String,
    /// Other places serving the same archive, tried in order after `url` (relative to the
    /// catalog too)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub mirrors: Vec<String>,
    /// The SHA-256 of the archive's bytes (`sha256:` and hex)
    pub sha256: String,
    /// What the package's files hash to (`Package::checksum`), as a lock file has it
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checksum: Option<String>,
    /// The archive's size in bytes
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size: Option<u64>,
    /// Withdrawn (`lx yank`): new installs don't choose it, but a lock file that has it still
    /// installs it, so nobody's text changes under them
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub yanked: bool,
}

impl CatalogSource {
    /// The prefix that names an import's publisher (`ebible`, `fetch`); a git or registry
    /// package has none, since it's added, not imported
    pub fn prefix(&self) -> Option<&str> {
        Some(self.import_name()?.split_once(':')?.0)
    }

    /// The publisher's own id (`eng-asv`, `crosswire/KJV`), for an import
    pub fn import_id(&self) -> Option<&str> {
        Some(self.import_name()?.split_once(':')?.1)
    }

    /// Whether a newer download always has a newer version listed ([`CatalogSource::External`]'s
    /// `tracks_versions`); never for what isn't imported
    pub fn versions_track_updates(&self) -> bool {
        matches!(
            self,
            CatalogSource::External {
                tracks_versions: true,
                ..
            }
        )
    }

    /// What `lx import` takes for it (`ebible:eng-asv`, `fetch:eng_asv`); none for what isn't
    /// imported
    pub fn import_name(&self) -> Option<&str> {
        match self {
            CatalogSource::External { import, .. } => Some(import),
            CatalogSource::Git { .. } | CatalogSource::Registry { .. } => None,
        }
    }

    /// The download an import is made from, for an import
    pub fn upstream_url(&self) -> Option<&str> {
        match self {
            CatalogSource::External { upstream, .. } => upstream.as_deref(),
            CatalogSource::Git { .. } | CatalogSource::Registry { .. } => None,
        }
    }

    /// What an import downloads ([`CatalogSource::External`]'s `downloads`); nothing for what
    /// isn't imported
    pub fn downloads(&self) -> &[Download] {
        match self {
            CatalogSource::External { downloads, .. } => downloads,
            CatalogSource::Git { .. } | CatalogSource::Registry { .. } => &[],
        }
    }
}

/// One package a catalog lists
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CatalogEntry {
    /// The package's id (`bsb`, `eng-asv`)
    pub id: String,
    pub name: String,
    /// What references name it by (`BSB`): a Bible or a commentary always has one; the other
    /// kinds may not ([`CatalogEntry::short_name`])
    pub abbreviation: Option<String>,
    pub aliases: Vec<String>,
    /// BCP 47 (`en`), or ISO 639-3 (`eng`) as eBible.org gives it; `und` for a kind with no
    /// language of its own that gives none
    pub language: String,
    /// The language's name, when the catalog gives it (`English`), or the publisher's own text
    /// for a language that isn't a tag (`und`)
    pub language_name: Option<String>,
    /// Its script (ISO 15924: `Latn`, `Hebr`), when the catalog says or its language tag does
    pub script: Option<String>,
    pub description: Option<String>,
    pub license: Option<String>,
    /// What its license allows, as the catalog says it (a `catalog.toml`'s `rights`, a
    /// publisher's terms); what's derived from `license` isn't here (see
    /// [`CatalogEntry::text_rights`])
    pub rights: crate::license::Rights,
    /// A copyright statement, when the catalog gives one
    pub copyright: Option<String>,
    /**
    The newest version, when the catalog says: a logoxide package's, or for an import the
    publisher's own, as it gives it (eBible.org's update date `2026-10-08`, a SWORD module's
    `Version`, fetch.bible's year), which an import keeps as its `[package.upstream] version`
    */
    pub version: Option<String>,
    pub kind: PackageKind,
    pub direction: Direction,
    pub source: CatalogSource,
    /// Its path in a registry folder (`books/romans`), which names it there as its id does
    pub path: Option<String>,
    /// The releases a registry publishes as archives, oldest first (none for other sources)
    pub releases: Vec<CatalogRelease>,
    /// Its manifest's `keywords`, `categories`, and `about-books` (a logoxide catalog's entries;
    /// other catalogs don't say)
    pub keywords: Vec<String>,
    pub categories: Vec<String>,
    pub about_books: Vec<String>,
    /// The work it's an edition of (`kjv`): a logoxide catalog's `work`, or for an import, the
    /// work logoxide's table knows its import name to be ([`crate::works`])
    pub work: Option<String>,
    /// Which edition (`1769-oxford`), and its year, when a logoxide catalog says
    pub edition: Option<String>,
    pub edition_year: Option<i32>,
    /// What a dictionary's keys are (`[package] keys`: Strong's Greek or Hebrew numbers, or
    /// headwords), when the catalog says; none for other kinds
    pub keys: Option<crate::manifest::Keys>,
}

impl CatalogEntry {
    /// What its license allows: its `rights`, with what they don't say derived from its license
    pub fn text_rights(&self) -> crate::license::Rights {
        (self.rights).or(crate::license::derived_rights(self.license.as_deref()))
    }

    /**
    Whether it's in the language `wanted` names: a BCP 47 range its tag matches
    ([`crate::language::matches_range`]: `en` is every English, `pt-BR` only Brazil's, `eng` is
    `en`), or the language's name, as the catalog gives it (`Español`) or in English
    ([`crate::language::language_name`]: `Spanish`), ignoring case and marks
    */
    pub fn in_language(&self, wanted: &str) -> bool {
        let wanted = fold(wanted.trim());
        crate::language::matches_range(&wanted, &self.language)
            || (self.language_name.as_deref())
                .into_iter()
                .chain(crate::language::language_name(&self.language))
                .any(|name| fold(name) == wanted)
    }

    /// What to call it in short: its abbreviation, else its id (a book or a collection, which
    /// references never name, may have none)
    pub fn short_name(&self) -> &str {
        self.abbreviation.as_deref().unwrap_or(&self.id)
    }

    /**
    Its language's name to show: the catalog's (`Español`), else its English name
    ([`crate::language::language_name`]), else its tag
    */
    pub fn language_shown(&self) -> &str {
        (self.language_name.as_deref())
            .or_else(|| crate::language::language_name(&self.language))
            .unwrap_or(&self.language)
    }

    /// Whether it's mainly about the book with this USFM code (`ROM`), as its `about-books` says
    pub fn is_about(&self, book: &str) -> bool {
        self.about_books.iter().any(|b| b == book)
    }

    /**
    The newest release `req` allows (without one, the newest that isn't a pre-release), from a
    registry's archives; a yanked one never
    */
    pub fn release(&self, req: Option<&semver::VersionReq>) -> Option<&CatalogRelease> {
        (self.releases.iter())
            .filter(|r| !r.yanked)
            .filter_map(|r| Some((r.version.parse::<semver::Version>().ok()?, r)))
            .filter(|(v, _)| match req {
                Some(req) => req.matches(v),
                None => v.pre.is_empty(),
            })
            .max_by(|a, b| a.0.cmp(&b.0))
            .map(|(_, r)| r)
    }
}

/// The `[[package]]` tables of a `catalog.toml`
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct TomlEntry {
    id: String,
    name: String,
    /// Required for a Bible or a commentary, as in a manifest
    #[serde(default)]
    abbreviation: Option<String>,
    #[serde(default)]
    aliases: Vec<String>,
    /// Required unless the kind has no language of its own, as in a manifest
    #[serde(default)]
    language: Option<String>,
    #[serde(default)]
    language_name: Option<String>,
    #[serde(default)]
    script: Option<String>,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    license: Option<String>,
    #[serde(default)]
    rights: crate::license::Rights,
    #[serde(default)]
    kind: PackageKind,
    /// Without one, its script's
    #[serde(default)]
    direction: Option<Direction>,
    #[serde(default)]
    version: Option<String>,
    #[serde(default)]
    git: Option<String>,
    /// A registry's archives (`[[package.release]]`)
    #[serde(default, rename = "release")]
    releases: Vec<CatalogRelease>,
    #[serde(default)]
    keywords: Vec<String>,
    #[serde(default)]
    categories: Vec<String>,
    #[serde(default)]
    about_books: Vec<String>,
    #[serde(default)]
    work: Option<String>,
    #[serde(default)]
    edition: Option<String>,
    #[serde(default)]
    edition_year: Option<i32>,
    #[serde(default)]
    keys: Option<crate::manifest::Keys>,
    /// Its path in the folder the catalog was built from (`lx catalog build`), which names it
    /// as its id does
    #[serde(default)]
    path: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
struct TomlCatalog {
    /// The layout's version ([`CATALOG_FORMAT`]); none is 1
    #[serde(default)]
    format: Option<u32>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default, rename = "package")]
    packages: Vec<TomlEntry>,
    /// Works it names (`[works.<id>]`), as logoxide's table does, without `imports`
    #[serde(default)]
    works: std::collections::BTreeMap<String, crate::works::Work>,
}

/**
The `catalog.toml` layout this logoxide reads and writes (`format = 1`; a catalog without it is 1).
A catalog of a later format is refused with a request to upgrade, rather than misread
*/
pub const CATALOG_FORMAT: u32 = 1;

/// A catalog's packages
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Catalog {
    pub name: String,
    pub entries: Vec<CatalogEntry>,
    /// The works it names beside logoxide's table (`[works.<id>]`; a folder registry's
    /// `works.toml`)
    pub works: Vec<crate::works::DeclaredWork>,
    /// What reading it passed over, and why (an entry whose id isn't one), for a warning
    pub warnings: Vec<String>,
}

impl Catalog {
    /// A `catalog.toml` read from `location` (a URL or a path): [`Catalog::parse_toml_at`].
    /// Other publishers' lists are read by `logoxide-import`'s `catalog::parse`, which falls
    /// back to this
    pub fn parse(location: &str, text: &str) -> Result<Catalog> {
        Catalog::parse_toml_at(Some(location), text)
    }

    pub fn parse_toml(text: &str) -> Result<Catalog> {
        Catalog::parse_toml_at(None, text)
    }

    /**
    A `catalog.toml` read from `location` (a URL or a path), which its relative archive URLs
    are relative to. A package needs `git` or releases
    */
    pub fn parse_toml_at(location: Option<&str>, text: &str) -> Result<Catalog> {
        let catalog: TomlCatalog =
            toml::from_str(text).map_err(|e| LogoxideError::Catalog(e.to_string()))?;
        let format = catalog.format.unwrap_or(1);
        if format > CATALOG_FORMAT {
            return Err(LogoxideError::Catalog(format!(
                "{}is format {format}, newer than this lx reads ({CATALOG_FORMAT}): upgrade lx",
                location.map(|l| format!("{l} ")).unwrap_or_default()
            )));
        }
        // A catalog from elsewhere names nothing on this machine: its packages, archives, and
        // mirrors are elsewhere too (a relative one is beside it)
        let remote = location.is_some_and(|l| !crate::git::is_local(l));
        let check = |id: &str, url: &str| match remote && crate::git::is_local(url) {
            true => Err(LogoxideError::Catalog(format!(
                "{id}: `{url}` is on this machine, but the catalog isn't"
            ))),
            false => Ok(()),
        };
        let (mut entries, mut warnings) = (vec![], vec![]);
        for e in catalog.packages {
            // An id names a folder in the library and a lock entry: one that isn't is passed
            // over, not the whole catalog
            let at = || location.map(|l| format!("{l}: ")).unwrap_or_default();
            if let Err(LogoxideError::Manifest(why)) = crate::manifest::check_id("package", &e.id) {
                warnings.push(format!("{}passed over {why}", at()));
                continue;
            }
            // What a manifest requires of the kind, a catalog's entry does too; one without is
            // passed over, not the whole catalog
            if e.abbreviation.is_none() && e.kind.needs_abbreviation() {
                warnings.push(format!(
                    "{}passed over {}: a {} needs an abbreviation",
                    at(),
                    e.id,
                    e.kind.name()
                ));
                continue;
            }
            let language = match e.language {
                Some(language) => language,
                None if !e.kind.needs_language() => String::from(crate::language::UNDETERMINED),
                None => {
                    warnings.push(format!(
                        "{}passed over {}: a {} needs a language",
                        at(),
                        e.id,
                        e.kind.name()
                    ));
                    continue;
                }
            };
            if let Some(git) = &e.git {
                check(&e.id, git)?;
            }
            let source = match &e.git {
                // `github:owner/repo` is listed by its URL
                Some(git) => CatalogSource::Git {
                    url: crate::git::expand_url(git),
                },
                None if e.releases.is_empty() => {
                    return Err(LogoxideError::Catalog(format!(
                        "{}: no `git` and no releases",
                        e.id
                    )));
                }
                None => CatalogSource::Registry {
                    catalog: location.unwrap_or_default().to_string(),
                },
            };
            let releases = (e.releases.into_iter())
                .map(|r| {
                    let whole = |url: &String| match location {
                        Some(base) => relative_url(base, url),
                        None => url.clone(),
                    };
                    CatalogRelease {
                        url: whole(&r.url),
                        mirrors: r.mirrors.iter().map(whole).collect(),
                        ..r
                    }
                })
                .collect::<Vec<CatalogRelease>>();
            for release in &releases {
                for url in std::iter::once(&release.url).chain(&release.mirrors) {
                    check(&e.id, url)?;
                }
            }
            let script = (e.script.clone()).or_else(|| crate::language::script_of_tag(&language));
            entries.push(CatalogEntry {
                id: e.id,
                name: e.name,
                abbreviation: e.abbreviation,
                aliases: e.aliases,
                language,
                language_name: e.language_name,
                direction: (e.direction)
                    .unwrap_or_else(|| crate::language::script_direction(script.as_deref())),
                script,
                description: e.description,
                license: e.license,
                rights: e.rights,
                copyright: None,
                version: e.version,
                kind: e.kind,
                source,
                path: e.path,
                releases,
                keywords: e.keywords,
                categories: e.categories,
                about_books: e.about_books,
                work: e.work,
                edition: e.edition,
                edition_year: e.edition_year,
                keys: e.keys,
            });
        }
        let name = catalog.name.unwrap_or_default();
        // Its works say where they're named: by its name, else where it is
        let named_by = match (name.as_str(), location) {
            ("", Some(location)) => location,
            (name, _) => name,
        };
        let (works, passed_over) = crate::works::parse_declared(named_by, location, catalog.works);
        warnings.extend(passed_over);
        Ok(Catalog {
            name,
            entries,
            works,
            warnings,
        })
    }

    /**
    [`Catalog::search`]'s entries grouped by the work each is an edition of
    ([`group_by_work`]): `search_grouped("king james")` gives one group of every KJV edition the
    catalogs list, then the rest
    */
    pub fn search_grouped(&self, query: &str) -> Vec<WorkGroup<'_>> {
        self.group_by_work(self.search(query))
    }

    /// Names it `name` (a registry by its name in the project), the works it names too
    pub fn rename(&mut self, name: &str) {
        self.name = name.to_string();
        for declared in &mut self.works {
            declared.catalog = name.to_string();
        }
    }

    /**
    The work with this id: logoxide's table's ([`crate::works::get`]), else the one this catalog
    (or one merged into it) names
    */
    pub fn work(&self, id: &str) -> Option<&crate::works::Work> {
        crate::works::get(id).or_else(|| (self.works.iter()).map(|d| &d.work).find(|w| w.id == id))
    }

    /**
    Entries grouped by the work each is an edition of, in the order of each work's first entry
    (so a ranked list stays ranked), entries in their order within a group; an entry with no
    work is a group of its own. Each group's name is its work's ([`Catalog::work`]), else its id
    */
    pub fn group_by_work<'a>(
        &self,
        entries: impl IntoIterator<Item = &'a CatalogEntry>,
    ) -> Vec<WorkGroup<'a>> {
        let mut groups: Vec<WorkGroup<'a>> = vec![];
        for entry in entries {
            match (entry.work.as_ref())
                .and_then(|work| groups.iter_mut().find(|g| g.work.as_ref() == Some(work)))
            {
                Some(group) => group.entries.push(entry),
                None => groups.push(WorkGroup {
                    work: entry.work.clone(),
                    name: (entry.work.as_deref())
                        .map(|id| self.work(id).map_or(id, |w| w.name.as_str()).to_string()),
                    entries: vec![entry],
                }),
            }
        }
        groups
    }

    /**
    Entries grouped by language, in the order of each language's first entry (so a ranked list
    stays ranked), entries in their order within a group, each group named in English where
    logoxide knows the language's name, so catalogs naming it otherwise agree. A language is its
    tag's ([`crate::language::primary_language`]: `en`, `en-US`, and `eng` are one), and an entry
    whose language isn't a tag (`und`) is grouped by the name its catalog gives
    */
    pub fn group_by_language<'a>(
        entries: impl IntoIterator<Item = &'a CatalogEntry>,
    ) -> Vec<LanguageGroup<'a>> {
        let mut groups: Vec<LanguageGroup<'a>> = vec![];
        for entry in entries {
            let language = crate::language::primary_language(&entry.language);
            let named = (language == "und")
                .then(|| entry.language_name.as_deref().map(fold))
                .flatten();
            match (groups.iter_mut()).find(|g| g.language == language && g.key == named) {
                Some(group) => {
                    if group.name.is_none() {
                        group.name = entry.language_name.clone();
                    }
                    group.entries.push(entry);
                }
                None => groups.push(LanguageGroup {
                    name: (crate::language::language_name(&language).map(String::from))
                        .or_else(|| entry.language_name.clone()),
                    language,
                    key: named,
                    entries: vec![entry],
                }),
            }
        }
        groups
    }

    /**
    Every work there is to name, as these catalogs see it: logoxide's table, then the works the
    catalogs name, then ids entries claim that nothing names, each with the entries claiming it.
    With `query`, only works whose id, name, or aliases hold each of its words (ignoring case and
    marks, as [`Catalog::search`] does)
    */
    pub fn works_listing(&self, query: &str) -> Vec<WorkListing<'_>> {
        let words: Vec<String> = query.split_whitespace().map(fold).collect();
        let claiming = |id: &str| -> Vec<&CatalogEntry> {
            (self.entries.iter())
                .filter(|e| e.work.as_deref() == Some(id))
                .collect()
        };
        let mut listed: Vec<WorkListing<'_>> = (crate::works::works().iter())
            .map(|w| (w, WorkFrom::Logoxide))
            .chain((self.works.iter()).map(|d| (&d.work, WorkFrom::Catalog(d.catalog.clone()))))
            .map(|(work, from)| WorkListing {
                id: work.id.clone(),
                name: Some(work.name.clone()),
                aliases: work.aliases.clone(),
                wikidata: work.wikidata.clone(),
                from,
                entries: claiming(&work.id),
            })
            .collect();
        for entry in &self.entries {
            if let Some(id) = &entry.work
                && !listed.iter().any(|l| &l.id == id)
            {
                listed.push(WorkListing {
                    id: id.clone(),
                    name: None,
                    aliases: vec![],
                    wikidata: None,
                    from: WorkFrom::Unnamed,
                    entries: claiming(id),
                });
            }
        }
        listed.retain(|l| {
            let names: Vec<String> = std::iter::once(&l.id)
                .chain(&l.name)
                .chain(&l.aliases)
                .map(|n| fold(n))
                .collect();
            (words.iter()).all(|w| names.iter().any(|n| n.contains(w.as_str())))
        });
        listed
    }

    /**
    Adds works another catalog names after this one's: a work with an id already here is passed
    over, with a warning when the two differ
    */
    pub fn add_works(&mut self, works: Vec<crate::works::DeclaredWork>) {
        for declared in works {
            match (self.works.iter()).find(|d| d.work.id == declared.work.id) {
                Some(kept) if kept.work != declared.work => self.warnings.push(format!(
                    "work `{}` is named by {} and {}: {}'s is used",
                    declared.work.id, kept.catalog, declared.catalog, kept.catalog
                )),
                Some(_) => {}
                None => self.works.push(declared),
            }
        }
    }

    /// The entries `query` matches, best first: every word of it must be in the entry's id,
    /// name, abbreviation, aliases, language, keywords, categories, its work's names, or
    /// description (ignoring case), or name a book it's about (`romans`); an exact abbreviation
    /// or id comes first, then names, languages, keywords, categories, and books, then
    /// descriptions
    pub fn search(&self, query: &str) -> Vec<&CatalogEntry> {
        let words: Vec<(String, Option<String>)> = (query.split_whitespace())
            .map(|word| (fold(word), crate::manifest::book_code(word)))
            .collect();
        let mut found: Vec<(u32, &CatalogEntry)> = (self.entries.iter())
            .filter_map(|entry| {
                let work = (entry.work.as_deref()).and_then(|id| self.work(id));
                Some((score(entry, work, &words)?, entry))
            })
            .collect();
        found.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.name.cmp(&b.1.name)));
        found.into_iter().map(|(_, e)| e).collect()
    }

    /// The entry an import name (`ebible:eng-asv`, as [`CatalogSource::import_name`] writes it)
    /// names, if this catalog lists it
    pub fn find_import(&self, name: &str) -> Option<&CatalogEntry> {
        (self.entries.iter()).find(|e| e.source.import_name() == Some(name))
    }

    /// The entry an imported package was made from, by the `[package.upstream]` its import wrote:
    /// its import name (`ebible:eng-asv`), else its download's URL
    pub fn find_upstream(&self, upstream: &crate::manifest::Upstream) -> Option<&CatalogEntry> {
        (upstream.import.as_deref())
            .and_then(|name| self.find_import(name.trim()))
            .or_else(|| {
                let url = upstream.url.as_deref()?.trim();
                (self.entries.iter()).find(|e| e.source.upstream_url() == Some(url))
            })
    }

    /**
    A registry folder's packages: each `(path, manifest)` is a repository at that path in the
    folder (`books/romans`, `/`-separated) and its `logoxide.toml`, at `<base_url>/<path>`. A
    manifest without `[package]` (a vault's) isn't a package
    */
    pub fn from_packages(
        name: &str,
        base_url: &str,
        packages: Vec<(String, crate::manifest::Manifest)>,
    ) -> Catalog {
        let base = base_url.trim_end_matches('/');
        let mut entries: Vec<CatalogEntry> = (packages.into_iter())
            .filter_map(|(path, manifest)| {
                let info = manifest.package?;
                Some(CatalogEntry {
                    script: info.text_script(),
                    direction: info.text_direction(),
                    id: info.id,
                    name: info.name,
                    abbreviation: info.abbreviation,
                    aliases: info.aliases,
                    language: info.language,
                    language_name: info.language_name,
                    description: info.description,
                    license: info.license,
                    rights: info.rights.unwrap_or_default(),
                    copyright: None,
                    version: Some(info.version.to_string()),
                    kind: info.kind,
                    source: CatalogSource::Git {
                        url: format!("{base}/{path}"),
                    },
                    path: Some(path),
                    releases: vec![],
                    keywords: info.keywords,
                    categories: info.categories,
                    about_books: info.about_books,
                    work: info.work,
                    edition: info.edition,
                    edition_year: info.edition_year,
                    keys: info.keys,
                })
            })
            .collect();
        entries.sort_by(|a, b| a.path.cmp(&b.path));
        Catalog {
            name: name.to_string(),
            entries,
            works: vec![],
            warnings: vec![],
        }
    }

    /**
    The entry `name` names in this registry: the one at that path (`books/romans`), else the
    one with that id. An id two entries share names neither: give the path
    */
    pub fn find(&self, name: &str) -> Result<Option<&CatalogEntry>> {
        let name = name.trim_matches('/');
        if let Some(entry) = (self.entries.iter()).find(|e| e.path.as_deref() == Some(name)) {
            return Ok(Some(entry));
        }
        let found: Vec<&CatalogEntry> = (self.entries.iter()).filter(|e| e.id == name).collect();
        match found.as_slice() {
            [] => Ok(None),
            [entry] => Ok(Some(entry)),
            many => {
                let paths: Vec<&str> = (many.iter())
                    .map(|e| e.path.as_deref().unwrap_or("?"))
                    .collect();
                Err(LogoxideError::Catalog(format!(
                    "{} packages are `{name}` in {}: name one by its path ({})",
                    many.len(),
                    self.name,
                    paths.join(", ")
                )))
            }
        }
    }

    /**
    The ids more than one of `catalogs` lists, and the names of those that list each, in order:
    merged ([`Catalog::merge`]), the first one's entry is kept and the others' are hidden, which a
    caller may warn of
    */
    pub fn shadowed(catalogs: &[Catalog]) -> Vec<(String, Vec<String>)> {
        let mut listed: indexmap::IndexMap<&str, Vec<String>> = indexmap::IndexMap::new();
        for catalog in catalogs {
            let mut seen = std::collections::BTreeSet::new();
            for entry in &catalog.entries {
                if seen.insert(entry.id.as_str()) {
                    listed
                        .entry(entry.id.as_str())
                        .or_default()
                        .push(catalog.name.clone());
                }
            }
        }
        (listed.into_iter())
            .filter(|(_, names)| names.len() > 1)
            .map(|(id, names)| (id.to_string(), names))
            .collect()
    }

    /// This catalog with another's entries (and warnings) after its own (an id listed twice
    /// keeps the first)
    pub fn merge(mut self, other: Catalog) -> Catalog {
        for entry in other.entries {
            if !self.entries.iter().any(|e| e.id == entry.id) {
                self.entries.push(entry);
            }
        }
        self.warnings.extend(other.warnings);
        self.add_works(other.works);
        self
    }

    /// A `[[package]]` table for a `catalog.toml`, from a package's manifest and its repository
    pub fn toml_entry(info: &crate::manifest::Package, git: &str) -> String {
        Catalog::toml_document(None, vec![Catalog::entry_table(info, git)])
    }

    /// A `catalog.toml`: its name, if given, and these `[[package]]` tables
    /// ([`Catalog::entry_table`]), in order
    pub fn toml_document(name: Option<&str>, tables: Vec<toml_edit::Table>) -> String {
        let mut doc = toml_edit::DocumentMut::new();
        doc.insert("format", toml_edit::value(i64::from(CATALOG_FORMAT)));
        if let Some(name) = name {
            doc.insert("name", toml_edit::value(name));
        }
        let mut packages = toml_edit::ArrayOfTables::new();
        for table in tables {
            packages.push(table);
        }
        doc.insert("package", toml_edit::Item::ArrayOfTables(packages));
        doc.to_string()
    }

    /// A package's `[[package]]` table, from its manifest (a release's) and its repository
    pub fn entry_table(info: &crate::manifest::Package, git: &str) -> toml_edit::Table {
        let mut table = toml_edit::Table::new();
        table.insert("id", toml_edit::value(&info.id));
        table.insert("name", toml_edit::value(&info.name));
        if let Some(abbreviation) = &info.abbreviation {
            table.insert("abbreviation", toml_edit::value(abbreviation));
        }
        if !info.aliases.is_empty() {
            let aliases: toml_edit::Array = info.aliases.iter().map(String::as_str).collect();
            table.insert("aliases", toml_edit::value(aliases));
        }
        table.insert("language", toml_edit::value(&info.language));
        if let Some(name) = &info.language_name {
            table.insert("language-name", toml_edit::value(name));
        }
        if let Some(script) = &info.text_script() {
            table.insert("script", toml_edit::value(script));
        }
        if let Some(description) = &info.description {
            table.insert("description", toml_edit::value(description));
        }
        table.insert("version", toml_edit::value(info.version.to_string()));
        if let Some(license) = &info.license {
            table.insert("license", toml_edit::value(license));
        }
        if let Some(rights) = info.rights.filter(|r| !r.is_empty())
            && let Ok(toml::Value::Table(said)) = toml::Value::try_from(rights)
        {
            let mut inline = toml_edit::InlineTable::new();
            for (key, value) in said {
                let value = match value {
                    toml::Value::Boolean(b) => toml_edit::Value::from(b),
                    other => toml_edit::Value::from(other.as_str().unwrap_or_default()),
                };
                inline.insert(&key, value);
            }
            table.insert("rights", toml_edit::value(inline));
        }
        if info.kind != PackageKind::Bible {
            let kind = toml::Value::try_from(info.kind)
                .ok()
                .and_then(|v| v.as_str().map(String::from))
                .unwrap_or_default();
            table.insert("kind", toml_edit::value(kind));
        }
        if info.text_direction() == Direction::Rtl {
            table.insert("direction", toml_edit::value("rtl"));
        }
        for (key, value) in [("work", &info.work), ("edition", &info.edition)] {
            if let Some(value) = value {
                table.insert(key, toml_edit::value(value));
            }
        }
        if let Some(year) = info.edition_year {
            table.insert("edition-year", toml_edit::value(i64::from(year)));
        }
        if let Some(keys) = info.keys {
            table.insert("keys", toml_edit::value(keys.name()));
        }
        for (key, list) in [
            ("keywords", &info.keywords),
            ("categories", &info.categories),
            ("about-books", &info.about_books),
        ] {
            if !list.is_empty() {
                let list: toml_edit::Array = list.iter().map(String::as_str).collect();
                table.insert(key, toml_edit::value(list));
            }
        }
        table.insert("git", toml_edit::value(git));
        table
    }
}

/// Entries of one work ([`Catalog::group_by_work`]), or one entry of none
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkGroup<'a> {
    /// The work's id (`kjv`), if its entries name one
    pub work: Option<String>,
    /// The work's name (logoxide's table's, else a catalog's, else its id), if it has a work
    pub name: Option<String>,
    pub entries: Vec<&'a CatalogEntry>,
}

/// Entries in one language ([`Catalog::group_by_language`])
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LanguageGroup<'a> {
    /// The language's subtag (`en`, `grc`), `und` when the entries' isn't a tag
    pub language: String,
    /// The language's English name (`Spanish`: [`crate::language::language_name`]), else the
    /// first a catalog gives (`Koine Greek`)
    pub name: Option<String>,
    /// For `und`: the name its entries share, folded
    key: Option<String>,
    pub entries: Vec<&'a CatalogEntry>,
}

/// Where a work in [`Catalog::works_listing`] is named
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WorkFrom {
    /// logoxide's own table (`data/works.toml`)
    Logoxide,
    /// The catalog of this name (`[works.<id>]`)
    Catalog(String),
    /// Nowhere: only entries claim it
    Unnamed,
}

/// A work, where it's named, and the entries claiming it ([`Catalog::works_listing`])
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkListing<'a> {
    pub id: String,
    /// None when nothing names it
    pub name: Option<String>,
    pub aliases: Vec<String>,
    pub wikidata: Option<String>,
    pub from: WorkFrom,
    pub entries: Vec<&'a CatalogEntry>,
}

/// Gives imports the works logoxide's table knows their import names to be
/// ([`crate::works::of_source`]), where the entries don't say; what a publisher's list is read
/// into (`logoxide-import`)
pub fn known_works(entries: &mut [CatalogEntry]) {
    for entry in entries {
        if entry.work.is_none() {
            entry.work = crate::works::of_source(&entry.source);
        }
    }
}

/**
`url` as seen from a catalog at `base`: a URL or an absolute path stays as it is; a relative one
is beside the catalog (`https://example.org/r/catalog.toml` and `bsb/bsb-1.0.0.tar.gz` are
`https://example.org/r/bsb/bsb-1.0.0.tar.gz`)
*/
pub fn relative_url(base: &str, url: &str) -> String {
    if url.contains("://") || url.starts_with('/') {
        return url.to_string();
    }
    match base.rfind('/') {
        Some(slash) => format!("{}/{url}", &base[..slash]),
        None => url.to_string(),
    }
}

/// Names a registry can't have: what URLs and other names already start with
pub const RESERVED_REGISTRY_NAMES: [&str; 15] = [
    "github", "gitlab", "codeberg", "ebible", "fetch", "ccel", "sword", "door43", "burrito", "dbl",
    "http", "https", "file", "ssh", "git",
];

/// Whether `name` can name a registry: lowercase letters, digits, `-`, and `_`, not reserved
pub fn check_registry_name(name: &str) -> Result<()> {
    let valid = !name.is_empty()
        && (name.chars()).all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || "-_".contains(c));
    if !valid {
        return Err(LogoxideError::Catalog(format!(
            "registry name `{name}`: use lowercase letters, digits, `-`, and `_`"
        )));
    }
    if RESERVED_REGISTRY_NAMES.contains(&name) {
        return Err(LogoxideError::Catalog(format!(
            "registry name `{name}` is taken (it starts URLs or other names)"
        )));
    }
    Ok(())
}

/// A name through a registry, `dorean-local:books/romans`, split into the registry (one of
/// `registries`) and the package's name there
pub fn split_registry_name<'a>(
    text: &'a str,
    registries: &std::collections::BTreeMap<String, String>,
) -> Option<(&'a str, &'a str)> {
    let (registry, name) = text.split_once(':')?;
    (registries.contains_key(registry) && !name.is_empty()).then_some((registry, name))
}

/**
Text as search matches it: lowercase, decomposed (NFD) so that every combining mark (accents,
Greek breathings, Hebrew points and cantillation, Arabic vowels) can be dropped, then composed
again, with a final sigma as any other. `Ἰησοῦς` matches `ιησους`, `בְּרֵאשִׁית` `בראשית`, and
`José` `jose`
*/
fn fold(text: &str) -> String {
    use unicode_normalization::{UnicodeNormalization, char::is_combining_mark};
    text.to_lowercase()
        .nfd()
        .filter(|c| !is_combining_mark(*c))
        .map(|c| if c == 'ς' { 'σ' } else { c })
        .nfc()
        .collect()
}

/// How well `words` (each with the book it names, if any) match `entry` and its `work`'s names,
/// if every one does
fn score(
    entry: &CatalogEntry,
    work: Option<&crate::works::Work>,
    words: &[(String, Option<String>)],
) -> Option<u32> {
    if words.is_empty() {
        return Some(0);
    }
    let exact = [fold(entry.short_name()), fold(&entry.id)];
    // Its work's names (`Authorized Version` finds every edition of the KJV)
    let names: Vec<String> = [&entry.name]
        .into_iter()
        .chain(&entry.aliases)
        .chain(&entry.language_name)
        .chain(&entry.keywords)
        .chain(work.map(|w| &w.name))
        .chain(work.into_iter().flat_map(|w| &w.aliases))
        .map(|s| fold(s))
        // Its language by any of its codes (`eng` finds `en`, and `en` finds `eng`)
        .chain(crate::language::search_codes(&entry.language))
        // `church-history` is two words
        .chain(entry.categories.iter().map(|c| fold(&c.replace('-', " "))))
        .collect();
    let description = entry.description.as_deref().map(fold).unwrap_or_default();
    let mut total = 0;
    for (word, book) in words {
        let about = book.as_ref().is_some_and(|b| entry.about_books.contains(b));
        total += if exact.iter().any(|e| e == word) {
            100
        } else if exact.iter().any(|e| e.contains(word.as_str())) {
            40
        } else if about
            || names
                .iter()
                .any(|n| n.split_whitespace().any(|w| w == word))
        {
            30
        } else if names.iter().any(|n| n.contains(word.as_str())) {
            20
        } else if description.contains(word.as_str()) {
            5
        } else {
            return None;
        };
    }
    Some(total)
}

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

    /// Entries as logoxide-import reads eBible.org's list into (its ASV and the WLC), so the
    /// package manager's side is tested without a publisher
    fn ebible() -> Catalog {
        let entry = |id: &str, name: &str, abbreviation: &str, language: &str, name_of: &str| {
            let import = format!("ebible:{id}");
            CatalogEntry {
                id: format!("ebible-{id}"),
                name: name.into(),
                abbreviation: Some(abbreviation.into()),
                aliases: vec![],
                language: language.into(),
                language_name: Some(name_of.into()),
                script: None,
                description: None,
                license: Some("LicenseRef-eBible".into()),
                rights: Default::default(),
                copyright: None,
                version: Some("2026-10-08".into()),
                kind: PackageKind::Bible,
                direction: Direction::Ltr,
                source: CatalogSource::External {
                    upstream: Some(format!("https://ebible.org/Scriptures/{id}_usfm.zip")),
                    import,
                    tracks_versions: true,
                    downloads: vec![],
                },
                path: None,
                releases: vec![],
                keywords: vec![],
                categories: vec![],
                about_books: vec![],
                work: None,
                edition: None,
                edition_year: None,
                keys: None,
            }
        };
        let mut entries = vec![
            entry(
                "eng-asv",
                "American Standard Version (1901)",
                "ASV",
                "en",
                "English",
            ),
            entry(
                "hebwlc",
                "Westminster Leningrad Codex",
                "WLC",
                "he",
                "Hebrew",
            ),
        ];
        known_works(&mut entries);
        Catalog {
            name: String::from("eBible.org"),
            entries,
            works: vec![],
            warnings: vec![],
        }
    }

    #[test]
    fn entries_are_found_and_grouped_by_language() {
        let mut catalog = ebible();
        let mut us = catalog.entries[0].clone();
        us.id = String::from("us");
        us.language = String::from("en-US");
        us.language_name = None;
        let mut odd = catalog.entries[0].clone();
        odd.id = String::from("odd");
        odd.language = String::from("und");
        odd.language_name = Some(String::from("Ancient Ones"));
        let mut korean = catalog.entries[0].clone();
        korean.id = String::from("korean");
        korean.language = String::from("ko");
        korean.language_name = None;
        catalog.entries.extend([us, odd, korean]);
        let ids = |wanted: &str| -> Vec<&str> {
            (catalog.entries.iter())
                .filter(|e| e.in_language(wanted))
                .map(|e| e.id.as_str())
                .collect()
        };
        assert_eq!(ids("en"), ["ebible-eng-asv", "us"]);
        assert_eq!(ids("eng"), ["ebible-eng-asv", "us"]);
        assert_eq!(ids("en-US"), ["us"]);
        assert_eq!(ids("hebrew"), ["ebible-hebwlc"]);
        assert_eq!(ids("ancient ones"), ["odd"]);
        // A language the catalog doesn't name is found and shown by its English name
        assert_eq!(ids("korean"), ["korean"]);
        assert_eq!(catalog.entries[4].language_shown(), "Korean");
        assert_eq!(catalog.entries[3].language_shown(), "Ancient Ones");
        let groups = Catalog::group_by_language(&catalog.entries);
        let summary: Vec<(&str, Option<&str>, usize)> = (groups.iter())
            .map(|g| (g.language.as_str(), g.name.as_deref(), g.entries.len()))
            .collect();
        assert_eq!(
            summary,
            [
                ("en", Some("English"), 2),
                ("he", Some("Hebrew"), 1),
                ("und", Some("Ancient Ones"), 1),
                ("ko", Some("Korean"), 1)
            ]
        );
    }

    #[test]
    fn catalogs_name_works_of_their_own() {
        let mine = Catalog::parse_toml_at(
            Some("https://example.org/catalog.toml"),
            r#"
name = "mine"

[works.gill]
name = "John Gill's Exposition of the Bible"
aliases = ["Gill's Exposition"]
wikidata = "Q42"

[works.kjv]
name = "Something else"

[[package]]
id = "gill-1810"
name = "Gill, 1810"
abbreviation = "GILL"
language = "en"
kind = "commentary"
work = "gill"
git = "https://example.org/gill"

[[package]]
id = "gill-modern"
name = "Gill, modernized"
abbreviation = "GILLM"
language = "en"
kind = "commentary"
work = "gill"
git = "https://example.org/gill-modern"

[[package]]
id = "mystery"
name = "A mystery"
abbreviation = "MYS"
language = "en"
work = "mystery-text"
git = "https://example.org/mystery"
"#,
        )
        .unwrap();
        // logoxide's table keeps `kjv`
        assert_eq!(mine.works.len(), 1);
        assert_eq!(mine.warnings.len(), 1, "{:?}", mine.warnings);
        assert!(mine.warnings[0].contains("logoxide's table names it"));
        // Its name finds its editions, gathered under it
        let groups = mine.search_grouped("exposition");
        assert_eq!(groups.len(), 1, "{groups:?}");
        assert_eq!(
            groups[0].name.as_deref(),
            Some("John Gill's Exposition of the Bible")
        );
        assert_eq!(groups[0].entries.len(), 2);
        // Another catalog naming it otherwise is warned of; the first is used
        let other = Catalog::parse_toml(
            "name = \"other\"\n[works.gill]\nname = \"Gill\"\n[works.henry]\nname = \"Matthew Henry\"\n",
        )
        .unwrap();
        let all = mine.merge(other);
        assert_eq!(all.work("gill").unwrap().aliases, ["Gill's Exposition"]);
        assert_eq!(all.work("henry").unwrap().name, "Matthew Henry");
        assert_eq!(all.work("kjv").unwrap().name, "King James Version");
        assert!(
            all.warnings
                .iter()
                .any(|w| w.contains("named by mine and other"))
        );
        // Every work: logoxide's, the catalogs', then what only entries claim
        let listing = all.works_listing("");
        assert!(listing.len() > 7);
        let gill = listing.iter().find(|l| l.id == "gill").unwrap();
        assert_eq!(gill.from, WorkFrom::Catalog("mine".into()));
        assert_eq!(gill.entries.len(), 2);
        assert_eq!(gill.wikidata.as_deref(), Some("Q42"));
        let last = listing.last().unwrap();
        assert_eq!(
            (last.id.as_str(), &last.from),
            ("mystery-text", &WorkFrom::Unnamed)
        );
        assert_eq!(last.name, None);
        let kjv = listing.iter().find(|l| l.id == "kjv").unwrap();
        assert_eq!(kjv.from, WorkFrom::Logoxide);
        assert!(kjv.entries.is_empty());
        // A query keeps works whose id, name, or aliases match each word
        let ids: Vec<String> = (all.works_listing("authorized").iter())
            .map(|l| l.id.clone())
            .collect();
        assert_eq!(ids, ["kjv"]);
        assert_eq!(all.works_listing("mystery").len(), 1);
        assert_eq!(all.works_listing("gill exposition").len(), 1);
    }

    #[test]
    fn editions_are_grouped_by_work() {
        let mine = Catalog::parse_toml(
            r#"
[[package]]
id = "asv-1901"
name = "ASV, the 1901 printing"
abbreviation = "ASV1901"
language = "en"
work = "asv"
edition = "1901-nelson"
edition-year = 1901
git = "https://example.org/asv"

[[package]]
id = "bsb"
name = "Berean Standard Bible"
abbreviation = "BSB"
language = "en"
git = "https://example.org/bsb"
"#,
        )
        .unwrap();
        let all = mine.merge(ebible());
        // eBible.org's ASV is the ASV by its import name
        let asv = all.find_import("ebible:eng-asv").unwrap();
        assert_eq!(asv.work.as_deref(), Some("asv"));
        assert_eq!(all.entries[0].edition_year, Some(1901));
        // Found by its work's name, and gathered under it
        let groups = all.search_grouped("american standard");
        assert_eq!(groups.len(), 1, "{groups:?}");
        assert_eq!(groups[0].work.as_deref(), Some("asv"));
        assert_eq!(groups[0].name.as_deref(), Some("American Standard Version"));
        let ids: Vec<&str> = groups[0].entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, ["asv-1901", "ebible-eng-asv"]);
        // An entry of no work is a group of its own
        let groups = all.search_grouped("");
        assert_eq!(
            (groups.iter())
                .map(|g| (g.work.as_deref(), g.entries.len()))
                .collect::<Vec<_>>(),
            [(Some("asv"), 2), (None, 1), (None, 1)]
        );
        // A catalog writes them back
        let info = crate::Manifest::parse(
            "[package]\nid = \"kjv\"\nname = \"KJV\"\nabbreviation = \"KJV\"\nversion = \"1.0.0\"\n\
             language = \"en\"\nwork = \"kjv\"\nedition = \"1769-oxford\"\nedition-year = 1769\n\
             [formats]\nusx = { path = \"usx/\" }\n",
        )
        .unwrap()
        .package
        .unwrap();
        let text = Catalog::toml_entry(&info, "https://example.org/kjv");
        let entry = &Catalog::parse_toml(&text).unwrap().entries[0];
        assert_eq!(
            (
                entry.work.as_deref(),
                entry.edition.as_deref(),
                entry.edition_year
            ),
            (Some("kjv"), Some("1769-oxford"), Some(1769))
        );
    }

    #[test]
    fn a_dictionarys_keys_are_listed() {
        let info = crate::Manifest::parse(
            "[package]\nid = \"strongs\"\nname = \"Strong's\"\nabbreviation = \"SG\"\n\
             version = \"1.0.0\"\nlanguage = \"en\"\nkind = \"lexicon\"\n\
             keys = \"strongs-greek\"\n[formats]\ndictionary = { path = \"entries/\" }\n",
        )
        .unwrap()
        .package
        .unwrap();
        let text = Catalog::toml_entry(&info, "https://example.org/strongs");
        let entry = &Catalog::parse_toml(&text).unwrap().entries[0];
        assert_eq!(entry.keys, Some(crate::manifest::Keys::StrongsGreek));
        // A folder registry's entries carry them too
        let manifest = crate::Manifest {
            package: Some(info),
            ..crate::Manifest::default()
        };
        let folder = Catalog::from_packages("local", "/r", vec![("strongs".into(), manifest)]);
        assert_eq!(
            folder.entries[0].keys,
            Some(crate::manifest::Keys::StrongsGreek)
        );
        // A Bible's entry has none
        let bible = Catalog::parse_toml(
            "[[package]]\nid = \"b\"\nname = \"B\"\nabbreviation = \"B\"\nlanguage = \"en\"\n\
             git = \"https://example.org/b\"\n",
        )
        .unwrap();
        assert_eq!(bible.entries[0].keys, None);
    }

    #[test]
    fn finds_imports_by_prefix() {
        let catalog = ebible();
        let asv = catalog.find_import("ebible:eng-asv").unwrap();
        assert_eq!(asv.source.import_name(), Some("ebible:eng-asv"));
        assert_eq!(asv.source.prefix(), Some("ebible"));
        assert_eq!(asv.source.import_id(), Some("eng-asv"));
        assert!(asv.source.versions_track_updates());
        // Another kind's prefix, or none, finds nothing
        assert!(catalog.find_import("fetch:eng-asv").is_none());
        assert!(catalog.find_import("eng-asv").is_none());
        let git = CatalogSource::Git {
            url: "https://example.org/bsb".into(),
        };
        assert_eq!(git.import_name(), None);
    }

    #[test]
    fn lists_short_names_by_url() {
        let catalog = Catalog::parse_toml(
            "[[package]]\nid = \"kjv\"\nname = \"KJV\"\nabbreviation = \"KJV\"\nlanguage = \"en\"\ngit = \"github:someone/kjv\"\n",
        )
        .unwrap();
        assert_eq!(
            catalog.entries[0].source,
            CatalogSource::Git {
                url: "https://github.com/someone/kjv".into()
            }
        );
    }

    #[test]
    fn folds_accents_points_and_final_sigma() {
        assert_eq!(fold("Ἰησοῦς"), fold("ιησους"));
        assert_eq!(fold("ΙΗΣΟΥΣ"), "ιησουσ");
        assert_eq!(fold("בְּרֵאשִׁית"), fold("בראשית"));
        assert_eq!(fold("José"), "jose");
        // Composed again: a Korean word stays whole syllables
        assert_eq!(fold("성경"), "성경");
        let catalog = Catalog::parse_toml(
            r#"
[[package]]
id = "tr"
name = "Ἡ Καινὴ Διαθήκη"
abbreviation = "TR"
language = "grc"
keywords = ["Ἰησοῦς"]
git = "https://example.org/tr"

[[package]]
id = "wlc"
name = "תּוֹרָה נְבִיאִים וּכְתוּבִים"
abbreviation = "WLC"
language = "hbo"
keywords = ["בְּרֵאשִׁית"]
git = "https://example.org/wlc"

[[package]]
id = "rv"
name = "Reina-Valera"
abbreviation = "RV"
language = "es"
description = "Traducción de Casiodoro de Reina, revisada por Cipriano de Valera; José"
git = "https://example.org/rv"

[[package]]
id = "luther"
name = "Lutherbibel"
abbreviation = "LUT"
language = "deu"
git = "https://example.org/luther"
"#,
        )
        .unwrap();
        let ids = |q: &str| (catalog.search(q).iter().map(|e| e.id.clone())).collect::<Vec<_>>();
        assert_eq!(ids("ιησους"), ["tr"]);
        assert_eq!(ids("καινη διαθηκη"), ["tr"]);
        assert_eq!(ids("בראשית"), ["wlc"]);
        assert_eq!(ids("תורה"), ["wlc"]);
        assert_eq!(ids("jose"), ["rv"]);
        assert_eq!(ids("traduccion"), ["rv"]);
        // A language by any of its codes: `spa` finds `es`, and `de` finds a package that
        // says `deu`
        assert_eq!(ids("spa"), ["rv"]);
        assert_eq!(ids("ger"), ["luther"]);
        assert_eq!(ids("de").first().map(String::as_str), Some("luther"));
    }

    #[test]
    fn searches_and_merges() {
        let ebible = ebible();
        let mine = Catalog::parse_toml(
            r#"
name = "Mine"
[[package]]
id = "bsb"
name = "Berean Standard Bible"
abbreviation = "BSB"
language = "en"
language-name = "English"
git = "https://example.org/bsb"
"#,
        )
        .unwrap();
        let all = mine.merge(ebible);
        let ids = |q: &str| {
            all.search(q)
                .iter()
                .map(|e| e.id.clone())
                .collect::<Vec<_>>()
        };
        assert_eq!(ids("asv"), ["ebible-eng-asv"]);
        assert_eq!(ids("english"), ["ebible-eng-asv", "bsb"]);
        assert_eq!(ids("hebrew codex"), ["ebible-hebwlc"]);
        assert_eq!(ids("berean"), ["bsb"]);
        assert!(ids("nothing like this").is_empty());
        assert_eq!(ids("").len(), 3);
        assert!(Catalog::parse_toml("[[package]]\nid = 1").is_err());
    }

    #[test]
    fn finds_what_a_package_is_about() {
        let info = crate::manifest::Manifest::parse(
            r#"[package]
id = "gill-romans"
name = "Gill on Romans"
abbreviation = "GILL"
version = "1.0.0"
language = "en"
kind = "commentary"
keywords = ["justification"]
categories = ["church-history"]
about-books = ["ROM"]
[formats]
usx = { path = "usx/" }
"#,
        )
        .unwrap()
        .package
        .unwrap();
        // A catalog keeps them, and reads them back
        let text = Catalog::toml_entry(&info, "https://example.org/gill");
        assert!(text.contains("about-books = [\"ROM\"]"), "{text}");
        let catalog = Catalog::parse_toml(&text).unwrap().merge(ebible());
        let gill = &catalog.entries[0];
        assert_eq!(
            (
                &gill.keywords[..],
                &gill.categories[..],
                &gill.about_books[..]
            ),
            (
                &["justification".to_string()][..],
                &["church-history".to_string()][..],
                &["ROM".to_string()][..]
            )
        );
        assert!(gill.is_about("ROM") && !gill.is_about("GAL"));
        let ids = |q: &str| {
            catalog
                .search(q)
                .iter()
                .map(|e| e.id.clone())
                .collect::<Vec<_>>()
        };
        // By the book's name or code, a keyword, or a category's words
        assert_eq!(ids("romans"), ["gill-romans"]);
        assert_eq!(ids("rom"), ["gill-romans"]);
        assert_eq!(ids("justification"), ["gill-romans"]);
        assert_eq!(ids("church history"), ["gill-romans"]);
        assert!(ids("galatians").is_empty());
    }

    #[test]
    fn writes_an_entry() {
        let manifest = crate::Manifest::parse(
            "[package]\nid = \"bsb\"\nname = \"Berean Standard Bible\"\nabbreviation = \"BSB\"\nversion = \"1.0.0\"\nlanguage = \"en\"\nlicense = \"CC0-1.0\"\n[formats]\nusx = { path = \"usx/\" }\n",
        )
        .unwrap();
        let text = Catalog::toml_entry(
            manifest.package.as_ref().unwrap(),
            "https://example.org/bsb",
        );
        let catalog = Catalog::parse_toml(&text).unwrap();
        assert_eq!(catalog.entries[0].id, "bsb");
        assert_eq!(
            catalog.entries[0].source,
            CatalogSource::Git {
                url: "https://example.org/bsb".into()
            }
        );
    }

    #[test]
    fn a_registry_folder_names_packages_by_path_or_id() {
        let package = |id: &str| {
            crate::manifest::Manifest::parse(&format!(
                "[package]\nid = \"{id}\"\nname = \"{id}\"\nabbreviation = \"X\"\n\
                 version = \"1.0.0\"\nlanguage = \"en\"\n\
                 [formats]\nusx = {{ path = \"usx/\" }}\n"
            ))
            .unwrap()
        };
        let vault = crate::manifest::Manifest::parse("[dependencies]\n").unwrap();
        let catalog = Catalog::from_packages(
            "local",
            "file:///packages/",
            vec![
                (String::from("kjv"), package("kjv")),
                (String::from("books/romans"), package("romans")),
                (String::from("forks/romans"), package("romans")),
                (String::from("vault"), vault),
            ],
        );
        assert_eq!(catalog.entries.len(), 3);
        let url = |name: &str| match &catalog.find(name).unwrap().unwrap().source {
            CatalogSource::Git { url } => url.clone(),
            other => panic!("{other:?}"),
        };
        assert_eq!(url("kjv"), "file:///packages/kjv");
        assert_eq!(url("books/romans"), "file:///packages/books/romans");
        assert_eq!(url("/forks/romans/"), "file:///packages/forks/romans");
        // Two packages are `romans`: the path has to say which
        let error = catalog.find("romans").unwrap_err().to_string();
        assert!(error.contains("books/romans, forks/romans"), "{error}");
        assert!(catalog.find("asv").unwrap().is_none());
    }

    #[test]
    fn registry_names_are_checked_and_split() {
        assert!(check_registry_name("dorean-local").is_ok());
        assert!(check_registry_name("github").is_err());
        assert!(check_registry_name("Dorean").is_err());
        assert!(check_registry_name("").is_err());
        let registries = std::collections::BTreeMap::from([(
            String::from("dorean-local"),
            String::from("~/repos"),
        )]);
        assert_eq!(
            split_registry_name("dorean-local:books/romans", &registries),
            Some(("dorean-local", "books/romans"))
        );
        assert_eq!(split_registry_name("github:a/b", &registries), None);
        assert_eq!(split_registry_name("dorean-local:", &registries), None);
    }

    #[test]
    fn a_built_catalog_names_its_packages_by_path() {
        let manifest = crate::manifest::Manifest::parse(
            "[package]\nid = \"romans\"\nname = \"Romans\"\nabbreviation = \"ROM\"\n\
             version = \"1.2.0\"\nlanguage = \"en\"\nkind = \"book\"\n\
             [formats]\nepub = { path = \"epub/\" }\n",
        )
        .unwrap();
        let info = manifest.package.unwrap();
        let mut table = Catalog::entry_table(&info, "file:///repos/books/romans");
        table.insert("path", toml_edit::value("books/romans"));
        table.insert("commit", toml_edit::value("3f9c2a1b"));
        let text = Catalog::toml_document(Some("mine"), vec![table]);
        let catalog = Catalog::parse("catalog.toml", &text).unwrap();
        assert_eq!(catalog.name, "mine");
        let entry = catalog.find("books/romans").unwrap().unwrap();
        assert_eq!(entry.id, "romans");
        assert_eq!(entry.version.as_deref(), Some("1.2.0"));
        assert_eq!(entry.kind, PackageKind::Book);
        assert_eq!(catalog.find("romans").unwrap(), Some(entry));
    }

    const ENTRY: &str =
        "[[package]]\nid = \"kjv\"\nname = \"KJV\"\nabbreviation = \"KJV\"\nlanguage = \"en\"\n";

    fn release(url: &str) -> String {
        format!(
            "{ENTRY}[[package.release]]\nversion = \"1.0.0\"\nurl = \"{url}\"\nsha256 = \"sha256:00\"\n"
        )
    }

    #[test]
    fn a_remote_catalog_names_nothing_local() {
        let remote = "https://example.org/r/catalog.toml";
        for url in ["/etc/passwd", "file:///etc/passwd"] {
            let error = Catalog::parse(remote, &release(url)).unwrap_err();
            assert!(error.to_string().contains("on this machine"), "{error}");
            // A local catalog may
            assert!(Catalog::parse("/srv/r/catalog.toml", &release(url)).is_ok());
        }
        let mirrored = format!(
            "{}mirrors = [\"/srv/kjv.tar.gz\"]\n",
            release("kjv-1.0.0.tar.gz")
        );
        assert!(Catalog::parse(remote, &mirrored).is_err());
        let git = format!("{ENTRY}git = \"/srv/kjv\"\n");
        assert!(Catalog::parse(remote, &git).is_err());
        // Relative ones are beside it, so remote too
        let catalog = Catalog::parse(remote, &release("kjv/kjv-1.0.0.tar.gz")).unwrap();
        assert_eq!(
            catalog.entries[0].releases[0].url,
            "https://example.org/r/kjv/kjv-1.0.0.tar.gz"
        );
    }

    #[test]
    fn catalogs_say_their_format() {
        assert!(Catalog::parse_toml(&format!("format = 1\n{ENTRY}git = \"x\"\n")).is_ok());
        let error = Catalog::parse_toml(&format!("format = 2\n{ENTRY}git = \"x\"\n")).unwrap_err();
        assert!(error.to_string().contains("upgrade lx"), "{error}");
        let written = Catalog::toml_document(Some("mine"), vec![]);
        assert!(written.starts_with("format = 1\n"), "{written}");
        assert!(Catalog::parse_toml(&written).is_ok());
    }

    #[test]
    fn entries_whose_ids_arent_are_passed_over() {
        let entry = |id: &str| {
            ENTRY.replace("\"kjv\"", &format!("\"{id}\"")) + "git = \"https://example.com/x\"\n"
        };
        let text: String = ["kjv", "../../etc", "KJV", "", "bsb"].map(entry).concat();
        let catalog =
            Catalog::parse_toml_at(Some("https://example.com/catalog.toml"), &text).unwrap();
        let ids: Vec<&str> = catalog.entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, ["kjv", "bsb"]);
        assert_eq!(catalog.warnings.len(), 3, "{:?}", catalog.warnings);
        assert!(
            catalog.warnings[0].starts_with("https://example.com/catalog.toml: passed over")
                && catalog.warnings[0].contains("`../../etc`"),
            "{:?}",
            catalog.warnings
        );
        // Merging keeps every catalog's warnings
        let merged = Catalog::default().merge(catalog);
        assert_eq!(merged.warnings.len(), 3);
    }

    #[test]
    fn entries_follow_the_manifests_rules_by_kind() {
        let text = "[[package]]\nid = \"tdp\"\nname = \"The Dorean Principle\"\nlanguage = \"en\"\n\
                    kind = \"book\"\ngit = \"https://example.com/tdp\"\n\n\
                    [[package]]\nid = \"shelf\"\nname = \"A Shelf\"\nkind = \"collection\"\n\
                    git = \"https://example.com/shelf\"\n\n\
                    [[package]]\nid = \"kjv\"\nname = \"King James\"\nlanguage = \"en\"\n\
                    git = \"https://example.com/kjv\"\n\n\
                    [[package]]\nid = \"gill\"\nname = \"Gill\"\nabbreviation = \"GILL\"\n\
                    kind = \"commentary\"\ngit = \"https://example.com/gill\"\n";
        let catalog = Catalog::parse_toml(text).unwrap();
        let ids: Vec<&str> = catalog.entries.iter().map(|e| e.id.as_str()).collect();
        // A book needs no abbreviation, a collection no language; a Bible without an
        // abbreviation and a commentary without a language are passed over
        assert_eq!(ids, ["tdp", "shelf"]);
        assert_eq!(catalog.entries[0].short_name(), "tdp");
        assert_eq!(catalog.entries[1].language, "und");
        assert_eq!(catalog.warnings.len(), 2, "{:?}", catalog.warnings);
        assert!(catalog.warnings[0].contains("kjv: a bible needs an abbreviation"));
        assert!(catalog.warnings[1].contains("gill: a commentary needs a language"));
    }

    #[test]
    fn shadowed_ids_are_found() {
        let catalog = |name: &str, ids: &[&str]| {
            let entries: String = (ids.iter())
                .map(|id| ENTRY.replace("\"kjv\"", &format!("\"{id}\"")) + "git = \"x\"\n")
                .collect();
            let mut catalog = Catalog::parse_toml(&entries).unwrap();
            catalog.name = name.to_string();
            catalog
        };
        let catalogs = [
            catalog("mine", &["kjv", "bsb"]),
            catalog("ebible", &["web", "kjv"]),
            catalog("other", &["kjv", "web", "asv"]),
        ];
        let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::<Vec<_>>();
        assert_eq!(
            Catalog::shadowed(&catalogs),
            [
                (String::from("kjv"), names(&["mine", "ebible", "other"])),
                (String::from("web"), names(&["ebible", "other"])),
            ]
        );
        // Merging still keeps the first
        let merged = (catalogs.iter().cloned()).reduce(Catalog::merge).unwrap();
        assert_eq!(merged.entries.len(), 4);
        let ids: Vec<&str> = merged.entries.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(ids, ["kjv", "bsb", "web", "asv"]);
    }
}