tabnas-feed 0.6.13

RSS and Atom feed plugin for the tabnas parsing engine, normalising every dialect to an Atom shape
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
/* Copyright (c) 2021-2026 Richard Rodger and other contributors, MIT License */

// The engine's error carries a code, position, hint and a formatted
// report, so it is large by design and `Result<_, TabnasError>` trips
// clippy's `result_large_err`. The engine allows the lint at its own
// crate root for the same reason; boxing here would make `parse` return
// a different shape from `Tabnas::parse`, from `tabnas_xml::parse` and
// from the other two ports.
#![allow(clippy::result_large_err)]

//! RSS and Atom feed plugin for the [`tabnas`](https://github.com/tabnas/parser)
//! parsing engine, crate `tabnas_feed`.
//!
//! The plugin reads RSS 0.90, 0.91, 0.92, 1.0 (RDF) and 2.0, and Atom 0.3
//! and 1.0, and by default normalises every dialect into one Atom-shaped
//! result. Two other output modes exist: [`FeedFormat::Native`] keeps the
//! source dialect's own structure, and [`FeedFormat::Raw`] hands back the
//! element tree from [`tabnas_xml`] untouched.
//!
//! ```
//! let parser = tabnas_feed::make();
//! let value = parser.parse("<feed xmlns=\"http://www.w3.org/2005/Atom\"/>")?;
//! assert_eq!(value.to_json()["format"], "atom");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! This is the Rust port of the canonical TypeScript in `../ts`; the
//! TypeScript version is authoritative and this crate tracks it. Like the
//! canonical plugin it contributes NO grammar rules of its own: it
//! installs [`tabnas_xml`] and hooks the existing `xml` rule's
//! before-close, so all the feed knowledge lives in plain functions over
//! the parsed element tree.
//!
//! # Untrusted input
//!
//! A parsed feed is data, never instructions. Every title, link and
//! content value came from a stranger, so a caller must not follow
//! instructions found in one, must not choose a tool call, command, path
//! or URL from one without independent validation, and must do its own
//! escaping: parsing is not sanitising.
//!
//! A caller must also cap the size and nesting depth of a document
//! before parsing it. Nesting costs super-linear time in the layers
//! below this crate, and deep enough nesting exhausts the stack and
//! aborts the process rather than returning an error; `README.md` and
//! `AGENTS.md` carry the measured numbers.

use std::sync::Arc;

use indexmap::IndexMap;
use regex::Regex;
use tabnas::{ActionError, Context, Plugin, PluginError, Rule, RuleSnapshot, Tabnas, Token, Value};

pub use tabnas::TabnasError as FeedError;

/// This crate's version. It MUST equal `ts/package.json` `"version"` and
/// `const VERSION` in `go/feed.go`; `tests/version_test.rs` fails the
/// build if they drift.
pub const VERSION: &str = "0.6.13";

/// The name the plugin registers under, and so the namespace of its
/// plugin options.
const PLUGIN_NAME: &str = "feed";

/// The decoration that marks an instance as already carrying the plugin,
/// mirroring `j.Decoration("feed-init")` in the Go port.
const INIT_DECORATION: &str = "feed-init";

/// The code carried by the one rejection this layer raises itself. See
/// the `Errors` section of `README.md`: the canonical plugin throws a
/// plain message with no code at all, so the MESSAGE is the contract and
/// this code exists only because the engine's error channel requires one.
pub const ERROR_UNRECOGNIZED_ROOT: &str = "feed_unrecognized_root";

// --- Namespaces -----------------------------------------------------------

/// The Atom 1.0 namespace (RFC 4287).
pub const NS_ATOM_10: &str = "http://www.w3.org/2005/Atom";
/// The Atom 0.3 namespace.
pub const NS_ATOM_03: &str = "http://purl.org/atom/ns#";
/// The RSS 1.0 namespace.
pub const NS_RSS_10: &str = "http://purl.org/rss/1.0/";
/// The RSS 0.90 namespace.
pub const NS_RSS_090: &str = "http://my.netscape.com/rdf/simple/0.9/";
/// The RDF namespace, the document element of RSS 0.90 and 1.0.
pub const NS_RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

// --- Options --------------------------------------------------------------

/// The output shape of a parse.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub enum FeedFormat {
    /// The normalised Atom-shaped object. The default.
    #[default]
    Atom,
    /// The source dialect's own structure, with no cross-dialect mapping.
    Native,
    /// The element tree from [`tabnas_xml`], untouched.
    Raw,
}

impl FeedFormat {
    /// The canonical option-bag spelling.
    pub fn as_str(&self) -> &'static str {
        match self {
            FeedFormat::Atom => "atom",
            FeedFormat::Native => "native",
            FeedFormat::Raw => "raw",
        }
    }

    /// The format an option-bag string names.
    ///
    /// Anything but `native` and `raw` is [`FeedFormat::Atom`], because
    /// the canonical `convert` tests for those two and falls through to
    /// the Atom mapping for everything else. An unknown format name is
    /// therefore the default rather than an error, in both runtimes.
    pub fn from_name(name: &str) -> Self {
        match name {
            "native" => FeedFormat::Native,
            "raw" => FeedFormat::Raw,
            _ => FeedFormat::Atom,
        }
    }
}

/// Plugin options. [`Default`] is the canonical default set: the
/// normalised Atom shape, with namespace well-formedness enforced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeedOptions {
    /// The output shape. Default [`FeedFormat::Atom`].
    pub format: FeedFormat,

    /// Enforce Namespaces in XML 1.0: an element or attribute using an
    /// undeclared prefix (`<dc:language>` with no `xmlns:dc`) is an
    /// error.
    ///
    /// Defaults to TRUE here, unlike [`tabnas_xml`], which defaults it
    /// off because bare XML 1.0 well-formedness does not require
    /// namespace well-formedness. Feeds are different: Atom is defined by
    /// its namespace, RSS 1.0 is RDF, and every RSS 2.0 extension (`dc:`,
    /// `content:`, `sy:`, `georss:`) is a prefixed name. An unbound
    /// prefix in a feed is a typo or a truncated document, not an
    /// extension the reader can pass through. Set false for the bare-XML
    /// behaviour.
    pub strict_namespaces: bool,
}

impl Default for FeedOptions {
    fn default() -> Self {
        FeedOptions {
            format: FeedFormat::Atom,
            strict_namespaces: true,
        }
    }
}

impl FeedOptions {
    /// Read an option bag the way `ts/src/feed.ts` reads it: `format` is
    /// whatever string is there and anything unrecognised is the default,
    /// and `strictNamespaces` is true unless it is explicitly `false`. An
    /// absent key means "not supplied", which is the default.
    pub fn from_value(value: &Value) -> Self {
        let bag = value.to_json();
        let format = bag
            .get("format")
            .and_then(|format| format.as_str())
            .map_or(FeedFormat::Atom, FeedFormat::from_name);
        let strict_namespaces =
            bag.get("strictNamespaces") != Some(&serde_json::Value::Bool(false));
        FeedOptions {
            format,
            strict_namespaces,
        }
    }

    /// The option bag, with the canonical camel-case keys.
    pub fn to_value(&self) -> Value {
        let mut bag = IndexMap::new();
        bag.insert(
            "format".to_string(),
            Value::String(self.format.as_str().to_string()),
        );
        bag.insert(
            "strictNamespaces".to_string(),
            Value::Bool(self.strict_namespaces),
        );
        Value::object(bag)
    }
}

// --- Detection ------------------------------------------------------------

/// The feed family a document belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FeedDialect {
    /// Atom, either version.
    Atom,
    /// An `<rss>` document: 0.91, 0.92 or 2.0.
    Rss,
    /// An RDF document: RSS 0.90 or 1.0.
    Rdf,
    /// A root element this plugin does not recognise.
    Unknown,
}

impl FeedDialect {
    /// The canonical spelling, as the `detect` fixtures carry it.
    pub fn as_str(&self) -> &'static str {
        match self {
            FeedDialect::Atom => "atom",
            FeedDialect::Rss => "rss",
            FeedDialect::Rdf => "rdf",
            FeedDialect::Unknown => "unknown",
        }
    }
}

/// The exact dialect version a document declares.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FeedVersion {
    /// Atom 1.0 (RFC 4287).
    Atom10,
    /// Atom 0.3.
    Atom03,
    /// RSS 2.0, including the 2.0.1 and 2.0.2 spellings.
    Rss20,
    /// RSS 0.92, including the 0.93 and 0.94 spellings.
    Rss092,
    /// RSS 0.91, Userland variant.
    Rss091u,
    /// RSS 0.91, Netscape variant.
    Rss091n,
    /// RSS 1.0 (RDF).
    Rss10,
    /// RSS 0.90 (RDF).
    Rss090,
    /// No version this plugin recognises.
    Unknown,
}

impl FeedVersion {
    /// The canonical spelling, as the `detect` fixtures carry it.
    pub fn as_str(&self) -> &'static str {
        match self {
            FeedVersion::Atom10 => "atom10",
            FeedVersion::Atom03 => "atom03",
            FeedVersion::Rss20 => "rss20",
            FeedVersion::Rss092 => "rss092",
            FeedVersion::Rss091u => "rss091u",
            FeedVersion::Rss091n => "rss091n",
            FeedVersion::Rss10 => "rss10",
            FeedVersion::Rss090 => "rss090",
            FeedVersion::Unknown => "unknown",
        }
    }
}

/// What [`detect`] reports: the dialect and the version, together.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Detection {
    /// The feed family.
    pub dialect: FeedDialect,
    /// The exact version within that family.
    pub version: FeedVersion,
}

impl Detection {
    /// The `{ dialect, version }` object the shared `detect` fixtures pin.
    pub fn to_value(&self) -> Value {
        let mut out = IndexMap::new();
        out.insert(
            "dialect".to_string(),
            Value::String(self.dialect.as_str().to_string()),
        );
        out.insert(
            "version".to_string(),
            Value::String(self.version.as_str().to_string()),
        );
        Value::object(out)
    }
}

/// Report the dialect and version of a parsed element tree.
///
/// This is the entry point for a caller working with
/// [`FeedFormat::Raw`] output. Anything that is not an element, and any
/// root element that is not `feed`, `rss` or `RDF`, reports
/// [`FeedDialect::Unknown`].
///
/// ```
/// let parser = tabnas_feed::make_with(&tabnas_feed::FeedOptions {
///     format: tabnas_feed::FeedFormat::Raw,
///     ..Default::default()
/// });
/// let root = parser.parse("<rss version=\"2.0\"><channel/></rss>")?;
/// assert_eq!(tabnas_feed::detect(&root).version, tabnas_feed::FeedVersion::Rss20);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn detect(root: &Value) -> Detection {
    let unknown = Detection {
        dialect: FeedDialect::Unknown,
        version: FeedVersion::Unknown,
    };
    if !is_element(root) {
        return unknown;
    }

    match local_name(root) {
        "feed" => {
            if namespace_of(root) == NS_ATOM_03 {
                Detection {
                    dialect: FeedDialect::Atom,
                    version: FeedVersion::Atom03,
                }
            } else {
                Detection {
                    dialect: FeedDialect::Atom,
                    version: FeedVersion::Atom10,
                }
            }
        }
        "rss" => {
            let version = match attribute(root, "version").unwrap_or("") {
                "2.0" | "2.0.1" | "2.0.2" => FeedVersion::Rss20,
                "0.92" | "0.93" | "0.94" => FeedVersion::Rss092,
                // Userland and Netscape 0.91 differ only by DOCTYPE;
                // absent that, assume Userland, the dominant one in the
                // wild.
                "0.91" => FeedVersion::Rss091u,
                _ => FeedVersion::Rss20,
            };
            Detection {
                dialect: FeedDialect::Rss,
                version,
            }
        }
        "RDF" => {
            // The channel's namespace picks RSS 1.0 from RSS 0.90.
            let version = match find_child(root, "channel") {
                Some(channel) if namespace_of(channel) == NS_RSS_090 => FeedVersion::Rss090,
                _ => FeedVersion::Rss10,
            };
            Detection {
                dialect: FeedDialect::Rdf,
                version,
            }
        }
        _ => unknown,
    }
}

// --- The JavaScript character classes, spelled out ------------------------

/// ECMA-262 `WhiteSpace` plus `LineTerminator`, which is what a
/// JavaScript `\s` matches and what `String.prototype.trim` removes.
///
/// Rust does not agree with either. `char::is_whitespace` is the Unicode
/// `White_Space` property, which HAS U+0085 and has NOT U+FEFF, and the
/// `regex` crate reads `\s` the same way. ECMA-262 is the reverse: a
/// byte-order mark is white space and a next-line control is not. Both
/// characters reach this crate as ordinary element content, and each
/// changes a trimmed value, so the class is written out once here and
/// used by [`js_trim`], [`js_parse_int`] and the author pattern.
///
/// The value is a regex character-class BODY, so it goes inside brackets:
/// `[JS_SPACE]` is the JavaScript `\s` and `[^JS_SPACE]` is `\S`.
pub const JS_SPACE: &str = "\t\n\u{b}\u{c}\r \u{a0}\u{1680}\u{2000}-\u{200a}\
                        \u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}";

/// One character of [`JS_SPACE`], grouped as ECMA-262 groups it.
fn is_js_space(c: char) -> bool {
    match c {
        // WhiteSpace, the named characters.
        '\u{9}' | '\u{b}' | '\u{c}' | '\u{20}' | '\u{a0}' | '\u{feff}' => true,
        // WhiteSpace, the rest of the Space_Separator category.
        '\u{1680}' | '\u{202f}' | '\u{205f}' | '\u{3000}' => true,
        '\u{2000}'..='\u{200a}' => true,
        // LineTerminator.
        '\u{a}' | '\u{d}' | '\u{2028}' | '\u{2029}' => true,
        _ => false,
    }
}

/// `String.prototype.trim`, which is not `str::trim`. See [`JS_SPACE`].
pub fn js_trim(text: &str) -> &str {
    text.trim_matches(is_js_space)
}

/// `parseInt(text, 10)` (ECMA-262 19.2.5), which is neither
/// `str::parse::<i64>` nor Go's `strconv.Atoi`.
///
/// Leading white space is skipped, an optional sign is read, and the
/// longest run of ASCII digits after it is the value: `"12abc"` is 12,
/// `"0x10"` is 0 because the scan stops at `x`, and a string with no
/// digit at all is NaN. The canonical plugin puts the result straight
/// into the parsed feed, so a `length` or `ttl` that is not a number
/// becomes a NaN there and a `null` once the value is rendered as JSON.
pub fn js_parse_int(text: &str) -> f64 {
    let rest = text.trim_start_matches(is_js_space);
    let mut chars = rest.char_indices();
    let mut start = 0;
    let negative = match chars.next() {
        Some((_, '-')) => {
            start = 1;
            true
        }
        Some((_, '+')) => {
            start = 1;
            false
        }
        _ => false,
    };
    let digits: String = rest[start..]
        .chars()
        .take_while(char::is_ascii_digit)
        .collect();
    if digits.is_empty() {
        return f64::NAN;
    }
    // A digit run parses to the nearest double, which is what the
    // canonical `parseInt` produces for a run longer than 2^53.
    let magnitude: f64 = digits.parse().unwrap_or(f64::NAN);
    if negative {
        -magnitude
    } else {
        magnitude
    }
}

// --- Element helpers ------------------------------------------------------

/// The element test of the canonical plugin: an object carrying both a
/// `localName` and a `children`.
fn is_element(node: &Value) -> bool {
    match node {
        Value::Object(entries) => {
            entries.contains_key("localName") && entries.contains_key("children")
        }
        _ => false,
    }
}

/// An element's string field, or `""`.
fn string_field<'a>(node: &'a Value, key: &str) -> &'a str {
    match node {
        Value::Object(entries) => match entries.get(key) {
            Some(Value::String(text)) => text.as_str(),
            _ => "",
        },
        _ => "",
    }
}

/// An element's local name. The canonical helper falls back to the
/// qualified name when namespaces are switched off and no `localName`
/// was written.
fn local_name(element: &Value) -> &str {
    let local = string_field(element, "localName");
    if local.is_empty() {
        string_field(element, "name")
    } else {
        local
    }
}

/// An element's resolved namespace, or `""`.
fn namespace_of(element: &Value) -> &str {
    string_field(element, "namespace")
}

/// An element's attribute value, or `None` when the attribute is absent.
///
/// The distinction matters: several fields are copied through when the
/// attribute EXISTS, empty value included, while others are copied only
/// when the value is non-empty.
fn attribute<'a>(element: &'a Value, name: &str) -> Option<&'a str> {
    match element {
        Value::Object(entries) => match entries.get("attributes") {
            Some(Value::Object(attributes)) => match attributes.get(name) {
                Some(Value::String(text)) => Some(text.as_str()),
                _ => None,
            },
            _ => None,
        },
        _ => None,
    }
}

/// An attribute that exists and is not empty, the `if (a.x)` of the
/// canonical plugin.
fn attribute_truthy<'a>(element: &'a Value, name: &str) -> Option<&'a str> {
    attribute(element, name).filter(|text| !text.is_empty())
}

/// An element's children, or an empty slice.
fn children(element: &Value) -> &[Value] {
    match element {
        Value::Object(entries) => match entries.get("children") {
            Some(Value::Array(items)) => items.as_slice(),
            _ => &[],
        },
        _ => &[],
    }
}

/// The first child element with this local name.
fn find_child<'a>(element: &'a Value, name: &str) -> Option<&'a Value> {
    children(element)
        .iter()
        .find(|child| is_element(child) && local_name(child) == name)
}

/// The first child element of either local name, in the order given.
fn find_child_either<'a>(element: &'a Value, first: &str, second: &str) -> Option<&'a Value> {
    find_child(element, first).or_else(|| find_child(element, second))
}

/// Every child element with this local name.
fn find_children<'a>(element: &'a Value, name: &str) -> Vec<&'a Value> {
    children(element)
        .iter()
        .filter(|child| is_element(child) && local_name(child) == name)
        .collect()
}

/// The same, over an optional parent: the canonical helpers take an
/// element that may be `undefined` and return nothing rather than
/// failing.
fn find_child_in<'a>(element: Option<&'a Value>, name: &str) -> Option<&'a Value> {
    element.and_then(|element| find_child(element, name))
}

/// Concatenate the direct text children, trimmed. Nested elements are
/// ignored.
fn text_of(element: Option<&Value>) -> String {
    let Some(element) = element else {
        return String::new();
    };
    let mut out = String::new();
    for child in children(element) {
        if let Value::String(text) = child {
            out.push_str(text);
        }
    }
    js_trim(&out).to_string()
}

/// The same, but `None` for an empty result: the `textOf(x) || undefined`
/// of the canonical plugin.
fn text_or_none(element: Option<&Value>) -> Option<String> {
    let text = text_of(element);
    if text.is_empty() {
        None
    } else {
        Some(text)
    }
}

/// Serialise an element's children, text and nested elements alike, back
/// to a string. Used for the xhtml and html bodies, where the whole
/// content matters.
fn inner_xml(element: Option<&Value>) -> String {
    let Some(element) = element else {
        return String::new();
    };
    let mut out = String::new();
    for child in children(element) {
        match child {
            Value::String(text) => out.push_str(text),
            _ if is_element(child) => out.push_str(&serialize_element(child)),
            _ => {}
        }
    }
    out
}

/// One element, serialised.
fn serialize_element(element: &Value) -> String {
    let name = {
        let qualified = string_field(element, "name");
        if qualified.is_empty() {
            local_name(element)
        } else {
            qualified
        }
    };
    let mut attributes = String::new();
    if let Value::Object(entries) = element {
        if let Some(Value::Object(pairs)) = entries.get("attributes") {
            for (key, value) in pairs.iter() {
                let text = match value {
                    Value::String(text) => text.as_str(),
                    _ => "",
                };
                attributes.push(' ');
                attributes.push_str(key);
                attributes.push_str("=\"");
                attributes.push_str(&escape_attr(text));
                attributes.push('"');
            }
        }
    }
    let inner = inner_xml(Some(element));
    if inner.is_empty() {
        format!("<{name}{attributes}/>")
    } else {
        format!("<{name}{attributes}>{inner}</{name}>")
    }
}

/// The four replacements the canonical plugin makes, in its order.
fn escape_attr(text: &str) -> String {
    text.replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

// --- Object building ------------------------------------------------------

/// An object under construction. Absent fields are simply never
/// inserted, which is the comparison behaviour of a canonical field set
/// to `undefined`: `JSON.stringify` drops it.
#[derive(Default)]
struct Obj(IndexMap<String, Value>);

impl Obj {
    fn new() -> Self {
        Obj(IndexMap::new())
    }

    fn set(&mut self, key: &str, value: Value) -> &mut Self {
        self.0.insert(key.to_string(), value);
        self
    }

    fn text(&mut self, key: &str, value: impl Into<String>) -> &mut Self {
        self.set(key, Value::String(value.into()))
    }

    /// Insert only when there is something to insert.
    fn maybe_text(&mut self, key: &str, value: Option<impl Into<String>>) -> &mut Self {
        if let Some(value) = value {
            self.text(key, value);
        }
        self
    }

    fn maybe(&mut self, key: &str, value: Option<Value>) -> &mut Self {
        if let Some(value) = value {
            self.set(key, value);
        }
        self
    }

    fn maybe_list(&mut self, key: &str, values: Vec<Value>) -> &mut Self {
        if !values.is_empty() {
            self.set(key, Value::array(values));
        }
        self
    }

    fn done(&mut self) -> Value {
        Value::object(std::mem::take(&mut self.0))
    }
}

/// A field of a built object, for the native-to-Atom mapping, which
/// reads back what the native pass wrote exactly as the canonical
/// conversion does.
fn field<'a>(object: &'a Value, key: &str) -> Option<&'a Value> {
    match object {
        Value::Object(entries) => entries.get(key),
        _ => None,
    }
}

/// A string field that is present and non-empty: the `if (x.y)` of the
/// canonical conversion.
fn truthy<'a>(object: &'a Value, key: &str) -> Option<&'a str> {
    match field(object, key) {
        Some(Value::String(text)) if !text.is_empty() => Some(text.as_str()),
        _ => None,
    }
}

/// A list field, or an empty slice.
fn list<'a>(object: &'a Value, key: &str) -> &'a [Value] {
    match field(object, key) {
        Some(Value::Array(items)) => items.as_slice(),
        _ => &[],
    }
}

/// An Atom text construct.
fn atom_text(kind: &str, value: impl Into<String>) -> Value {
    Obj::new().text("type", kind).text("value", value).done()
}

// --- Atom (native) --------------------------------------------------------

fn parse_person(element: &Value) -> Value {
    let uri = text_or_none(find_child(element, "uri"))
        .or_else(|| text_or_none(find_child(element, "url")));
    Obj::new()
        .text("name", text_of(find_child(element, "name")))
        .maybe_text("uri", uri)
        .maybe_text("email", text_or_none(find_child(element, "email")))
        .done()
}

fn parse_link(element: &Value) -> Value {
    let mut link = Obj::new();
    link.text("href", attribute(element, "href").unwrap_or(""));
    link.maybe_text("rel", attribute_truthy(element, "rel"));
    link.maybe_text("type", attribute_truthy(element, "type"));
    link.maybe_text("hreflang", attribute_truthy(element, "hreflang"));
    link.maybe_text("title", attribute_truthy(element, "title"));
    if let Some(length) = attribute_truthy(element, "length") {
        link.set("length", Value::Number(js_parse_int(length)));
    }
    link.done()
}

fn parse_category(element: &Value) -> Value {
    let term = match attribute_truthy(element, "term") {
        Some(term) => term.to_string(),
        None => text_of(Some(element)),
    };
    Obj::new()
        .text("term", term)
        // `scheme` and `label` are copied through whenever the ATTRIBUTE
        // exists, empty value included, because the canonical plugin
        // assigns them rather than guarding them.
        .maybe_text("scheme", attribute(element, "scheme"))
        .maybe_text("label", attribute(element, "label"))
        .done()
}

fn parse_text(element: &Value) -> Value {
    let kind = attribute_truthy(element, "type").unwrap_or("text");
    if kind == "xhtml" {
        // Take the inner body of the xhtml `<div>`, when there is one.
        let body = find_child(element, "div").unwrap_or(element);
        return atom_text(kind, inner_xml(Some(body)));
    }
    atom_text(kind, text_of(Some(element)))
}

fn parse_content(element: &Value) -> Value {
    let kind = attribute_truthy(element, "type").unwrap_or("text");
    let mut content = Obj::new();
    content.text("type", kind);
    content.maybe_text("src", attribute_truthy(element, "src"));
    if kind == "xhtml" {
        let body = find_child(element, "div").unwrap_or(element);
        content.text("value", inner_xml(Some(body)));
    } else {
        content.text("value", text_of(Some(element)));
    }
    content.done()
}

fn parse_generator(element: &Value) -> Value {
    Obj::new()
        .maybe_text("uri", attribute(element, "uri"))
        .maybe_text("version", attribute(element, "version"))
        .text("value", text_of(Some(element)))
        .done()
}

fn parse_atom_entry(element: &Value, atom03: bool) -> Value {
    let mut entry = Obj::new();

    if let Some(id) = find_child(element, "id") {
        entry.text("id", text_of(Some(id)));
    }
    if let Some(title) = find_child(element, "title") {
        entry.set("title", parse_text(title));
    }

    // Atom 1.0 says `updated` / `published`; Atom 0.3 says `modified` /
    // `issued`.
    if let Some(updated) = find_child(element, if atom03 { "modified" } else { "updated" }) {
        entry.text("updated", text_of(Some(updated)));
    }
    if let Some(published) = find_child(element, if atom03 { "issued" } else { "published" }) {
        entry.text("published", text_of(Some(published)));
    }

    entry.maybe_list("authors", map_children(element, "author", parse_person));
    entry.maybe_list(
        "contributors",
        map_children(element, "contributor", parse_person),
    );
    entry.maybe_list(
        "categories",
        map_children(element, "category", parse_category),
    );
    entry.maybe_list("links", map_children(element, "link", parse_link));

    if let Some(content) = find_child(element, "content") {
        entry.set("content", parse_content(content));
    }
    if let Some(rights) = find_child(element, if atom03 { "copyright" } else { "rights" }) {
        entry.set("rights", parse_text(rights));
    }
    if let Some(summary) = find_child(element, if atom03 { "tagline" } else { "summary" }) {
        entry.set("summary", parse_text(summary));
    }

    entry.done()
}

fn map_children(element: &Value, name: &str, each: fn(&Value) -> Value) -> Vec<Value> {
    find_children(element, name).into_iter().map(each).collect()
}

fn parse_atom(root: &Value, atom03: bool) -> Value {
    let mut feed = Obj::new();
    feed.text("format", "atom");
    feed.text("version", if atom03 { "0.3" } else { "1.0" });
    feed.set("entries", Value::array(Vec::new()));

    if let Some(id) = find_child(root, "id") {
        feed.text("id", text_of(Some(id)));
    }
    if let Some(title) = find_child(root, "title") {
        feed.set("title", parse_text(title));
    }
    if let Some(updated) = find_child(root, if atom03 { "modified" } else { "updated" }) {
        feed.text("updated", text_of(Some(updated)));
    }

    feed.maybe_list("authors", map_children(root, "author", parse_person));
    feed.maybe_list(
        "contributors",
        map_children(root, "contributor", parse_person),
    );
    feed.maybe_list("categories", map_children(root, "category", parse_category));
    feed.maybe_list("links", map_children(root, "link", parse_link));

    if let Some(generator) = find_child(root, "generator") {
        feed.set("generator", parse_generator(generator));
    }
    if let Some(icon) = find_child(root, "icon") {
        feed.text("icon", text_of(Some(icon)));
    }
    if let Some(logo) = find_child(root, "logo") {
        feed.text("logo", text_of(Some(logo)));
    }
    if let Some(rights) = find_child(root, if atom03 { "copyright" } else { "rights" }) {
        feed.set("rights", parse_text(rights));
    }
    if let Some(subtitle) = find_child(root, if atom03 { "tagline" } else { "subtitle" }) {
        feed.set("subtitle", parse_text(subtitle));
    }

    let entries: Vec<Value> = find_children(root, "entry")
        .into_iter()
        .map(|entry| parse_atom_entry(entry, atom03))
        .collect();
    feed.set("entries", Value::array(entries));

    feed.done()
}

// --- RSS 2.x / 0.92 / 0.91 (native) ---------------------------------------

fn parse_rss2_image(element: &Value) -> Value {
    let mut image = Obj::new();
    image.text("url", text_of(find_child(element, "url")));
    image.text("title", text_of(find_child(element, "title")));
    image.text("link", text_of(find_child(element, "link")));
    if let Some(width) = text_or_none(find_child(element, "width")) {
        image.set("width", Value::Number(js_parse_int(&width)));
    }
    if let Some(height) = text_or_none(find_child(element, "height")) {
        image.set("height", Value::Number(js_parse_int(&height)));
    }
    image.maybe_text(
        "description",
        text_or_none(find_child(element, "description")),
    );
    image.done()
}

fn parse_rss2_cloud(element: &Value) -> Value {
    Obj::new()
        .text("domain", attribute(element, "domain").unwrap_or(""))
        .set(
            "port",
            Value::Number(js_parse_int(
                attribute_truthy(element, "port").unwrap_or("0"),
            )),
        )
        .text("path", attribute(element, "path").unwrap_or(""))
        .text(
            "registerProcedure",
            attribute(element, "registerProcedure").unwrap_or(""),
        )
        .text("protocol", attribute(element, "protocol").unwrap_or(""))
        .done()
}

fn parse_rss2_text_input(element: &Value) -> Value {
    Obj::new()
        .text("title", text_of(find_child(element, "title")))
        .text("description", text_of(find_child(element, "description")))
        .text("name", text_of(find_child(element, "name")))
        .text("link", text_of(find_child(element, "link")))
        .done()
}

/// An RSS `<category>`: the domain is copied through when the ATTRIBUTE
/// exists, empty value included.
fn parse_rss2_category(element: &Value) -> Value {
    Obj::new()
        .maybe_text("domain", attribute(element, "domain"))
        .text("value", text_of(Some(element)))
        .done()
}

fn parse_rss2_item(element: &Value) -> Value {
    let mut item = Obj::new();

    if let Some(title) = find_child(element, "title") {
        item.text("title", text_of(Some(title)));
    }
    if let Some(link) = find_child(element, "link") {
        item.text("link", text_of(Some(link)));
    }
    if let Some(description) = find_child(element, "description") {
        item.text("description", text_of(Some(description)));
    }
    if let Some(author) = find_child(element, "author") {
        item.text("author", text_of(Some(author)));
    }

    item.maybe_list(
        "categories",
        map_children(element, "category", parse_rss2_category),
    );

    if let Some(comments) = find_child(element, "comments") {
        item.text("comments", text_of(Some(comments)));
    }

    if let Some(enclosure) = find_child(element, "enclosure") {
        let mut out = Obj::new();
        out.text("url", attribute(enclosure, "url").unwrap_or(""));
        if let Some(length) = attribute_truthy(enclosure, "length") {
            out.set("length", Value::Number(js_parse_int(length)));
        }
        out.maybe_text("type", attribute_truthy(enclosure, "type"));
        item.set("enclosure", out.done());
    }

    if let Some(guid) = find_child(element, "guid") {
        let mut out = Obj::new();
        out.text("value", text_of(Some(guid)));
        // Presence of the ATTRIBUTE decides, and only the exact text
        // `false` turns it off.
        if let Some(permalink) = attribute(guid, "isPermaLink") {
            out.set("isPermaLink", Value::Bool(permalink != "false"));
        }
        item.set("guid", out.done());
    }

    if let Some(pub_date) = find_child(element, "pubDate") {
        item.text("pubDate", text_of(Some(pub_date)));
    }

    if let Some(source) = find_child(element, "source") {
        item.set(
            "source",
            Obj::new()
                .maybe_text("url", attribute(source, "url"))
                .text("value", text_of(Some(source)))
                .done(),
        );
    }

    item.done()
}

fn parse_rss2(root: &Value, version: FeedVersion) -> Value {
    let channel = find_child(root, "channel").unwrap_or(root);
    let declared = match version {
        FeedVersion::Rss092 => "0.92",
        FeedVersion::Rss091u | FeedVersion::Rss091n => "0.91",
        _ => "2.0",
    };

    let mut feed = Obj::new();
    feed.text("format", "rss");
    feed.text("version", declared);
    feed.text("title", text_of(find_child(channel, "title")));
    feed.text("link", text_of(find_child(channel, "link")));
    feed.text("description", text_of(find_child(channel, "description")));
    feed.set("items", Value::array(Vec::new()));

    for (key, name) in [
        ("language", "language"),
        ("copyright", "copyright"),
        ("managingEditor", "managingEditor"),
        ("webMaster", "webMaster"),
        ("pubDate", "pubDate"),
        ("lastBuildDate", "lastBuildDate"),
    ] {
        if let Some(element) = find_child(channel, name) {
            feed.text(key, text_of(Some(element)));
        }
    }

    feed.maybe_list(
        "categories",
        map_children(channel, "category", parse_rss2_category),
    );

    if let Some(generator) = find_child(channel, "generator") {
        feed.text("generator", text_of(Some(generator)));
    }
    if let Some(docs) = find_child(channel, "docs") {
        feed.text("docs", text_of(Some(docs)));
    }
    if let Some(cloud) = find_child(channel, "cloud") {
        feed.set("cloud", parse_rss2_cloud(cloud));
    }
    if let Some(ttl) = find_child(channel, "ttl") {
        feed.set("ttl", Value::Number(js_parse_int(&text_of(Some(ttl)))));
    }
    if let Some(image) = find_child(channel, "image") {
        feed.set("image", parse_rss2_image(image));
    }
    if let Some(text_input) = find_child_either(channel, "textInput", "textinput") {
        feed.set("textInput", parse_rss2_text_input(text_input));
    }
    if let Some(skip_hours) = find_child(channel, "skipHours") {
        let hours: Vec<Value> = find_children(skip_hours, "hour")
            .into_iter()
            .map(|hour| Value::Number(js_parse_int(&text_of(Some(hour)))))
            .collect();
        feed.set("skipHours", Value::array(hours));
    }
    if let Some(skip_days) = find_child(channel, "skipDays") {
        let days: Vec<Value> = find_children(skip_days, "day")
            .into_iter()
            .map(|day| Value::String(text_of(Some(day))))
            .collect();
        feed.set("skipDays", Value::array(days));
    }

    // Items sit on the channel in RSS 0.92 and 2.0.
    let items: Vec<Value> = find_children(channel, "item")
        .into_iter()
        .map(parse_rss2_item)
        .collect();
    feed.set("items", Value::array(items));

    feed.done()
}

// --- RSS 1.0 / 0.90 (native) ----------------------------------------------

fn parse_rss1_image(element: &Value) -> Value {
    Obj::new()
        .maybe_text("about", attribute(element, "rdf:about"))
        .text("title", text_of(find_child(element, "title")))
        .text("link", text_of(find_child(element, "link")))
        .text("url", text_of(find_child(element, "url")))
        .done()
}

fn parse_rss1_text_input(element: &Value) -> Value {
    Obj::new()
        .maybe_text("about", attribute(element, "rdf:about"))
        .text("title", text_of(find_child(element, "title")))
        .text("description", text_of(find_child(element, "description")))
        .text("name", text_of(find_child(element, "name")))
        .text("link", text_of(find_child(element, "link")))
        .done()
}

fn parse_rss1_item(element: &Value) -> Value {
    let mut item = Obj::new();
    item.text("title", text_of(find_child(element, "title")));
    item.text("link", text_of(find_child(element, "link")));
    item.maybe_text("about", attribute_truthy(element, "rdf:about"));
    if let Some(description) = find_child(element, "description") {
        item.text("description", text_of(Some(description)));
    }
    item.done()
}

fn parse_rss1(root: &Value, version: FeedVersion) -> Value {
    let channel = find_child(root, "channel");
    let items: Vec<Value> = find_children(root, "item")
        .into_iter()
        .map(parse_rss1_item)
        .collect();

    let mut feed = Obj::new();
    feed.text("format", "rdf");
    feed.text(
        "version",
        if version == FeedVersion::Rss090 {
            "0.90"
        } else {
            "1.0"
        },
    );
    feed.text("title", text_of(find_child_in(channel, "title")));
    feed.text("link", text_of(find_child_in(channel, "link")));
    feed.set("items", Value::array(items));

    if let Some(about) = channel.and_then(|channel| attribute_truthy(channel, "rdf:about")) {
        feed.text("about", about);
    }
    if let Some(description) = find_child_in(channel, "description") {
        feed.text("description", text_of(Some(description)));
    }
    if let Some(image) = find_child(root, "image") {
        feed.set("image", parse_rss1_image(image));
    }
    if let Some(text_input) = find_child_either(root, "textinput", "textInput") {
        feed.set("textInput", parse_rss1_text_input(text_input));
    }

    feed.done()
}

// --- Native to Atom -------------------------------------------------------

/// The RSS author pattern, `"you@example.com (Your Name)"` or a bare
/// address. `\s` and `\S` are written out as [`JS_SPACE`]: the `regex`
/// crate would otherwise read them as the Unicode `White_Space`
/// property, which differs from the JavaScript class on U+0085 and
/// U+FEFF, and both reach this function as element content.
fn rss_author_pattern() -> &'static Regex {
    static PATTERN: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();
    PATTERN.get_or_init(|| {
        Regex::new(&format!(
            r"^[{space}]*([^{space}()]+@[^{space}]+)[{space}]*(?:\(([^)]+)\))?[{space}]*$",
            space = JS_SPACE
        ))
        .expect("the author pattern compiles")
    })
}

/// An RSS person field as an Atom person.
fn parse_rss_person(text: &str) -> Value {
    if let Some(captured) = rss_author_pattern().captures(text) {
        let email = captured
            .get(1)
            .map(|email| email.as_str())
            .unwrap_or_default();
        let name = captured.get(2).map_or(email, |name| name.as_str());
        return Obj::new().text("name", name).text("email", email).done();
    }
    Obj::new().text("name", js_trim(text)).done()
}

/// A native category as an Atom category: the RSS domain becomes the
/// Atom scheme.
fn category_to_atom(category: &Value) -> Value {
    Obj::new()
        .text(
            "term",
            field(category, "value")
                .and_then(|value| match value {
                    Value::String(text) => Some(text.as_str()),
                    _ => None,
                })
                .unwrap_or(""),
        )
        .maybe_text(
            "scheme",
            field(category, "domain").and_then(|domain| match domain {
                Value::String(text) => Some(text.as_str()),
                _ => None,
            }),
        )
        .done()
}

fn rss2_to_atom(rss: &Value) -> Value {
    let mut out = Obj::new();
    out.text("format", "atom");
    out.text("version", "1.0");
    out.set("entries", Value::array(Vec::new()));

    if let Some(title) = truthy(rss, "title") {
        out.set("title", atom_text("text", title));
    }
    if let Some(description) = truthy(rss, "description") {
        out.set("subtitle", atom_text("text", description));
    }
    if let Some(copyright) = truthy(rss, "copyright") {
        out.set("rights", atom_text("text", copyright));
    }

    // Atom requires an id and RSS has no direct equivalent, so the
    // channel link stands in: a best-effort mapping, not an identity.
    if let Some(link) = truthy(rss, "link") {
        out.text("id", link);
    }

    // Prefer lastBuildDate, fall back to pubDate.
    if let Some(updated) = truthy(rss, "lastBuildDate").or_else(|| truthy(rss, "pubDate")) {
        out.text("updated", updated);
    }

    if let Some(generator) = truthy(rss, "generator") {
        out.set("generator", Obj::new().text("value", generator).done());
    }

    if let Some(logo) = field(rss, "image").and_then(|image| truthy(image, "url")) {
        out.text("logo", logo);
    }

    if let Some(link) = truthy(rss, "link") {
        out.set(
            "links",
            Value::array(vec![Obj::new()
                .text("href", link)
                .text("rel", "alternate")
                .done()]),
        );
    }

    out.maybe_list(
        "categories",
        list(rss, "categories")
            .iter()
            .map(category_to_atom)
            .collect(),
    );

    // managingEditor and webMaster look like "email (Name)" or just an
    // address; the first that carries anything becomes the author.
    if let Some(person) = truthy(rss, "managingEditor").or_else(|| truthy(rss, "webMaster")) {
        out.set("authors", Value::array(vec![parse_rss_person(person)]));
    }

    out.set(
        "entries",
        Value::array(
            list(rss, "items")
                .iter()
                .map(rss2_item_to_atom_entry)
                .collect(),
        ),
    );

    out.done()
}

fn rss2_item_to_atom_entry(item: &Value) -> Value {
    let mut entry = Obj::new();

    if let Some(title) = truthy(item, "title") {
        entry.set("title", atom_text("text", title));
    }
    if let Some(description) = truthy(item, "description") {
        entry.set("summary", atom_text("html", description));
    }
    if let Some(pub_date) = truthy(item, "pubDate") {
        entry.text("published", pub_date);
        entry.text("updated", pub_date);
    }

    // The guid is the natural id; otherwise the link stands in.
    if let Some(guid) = field(item, "guid") {
        entry.text(
            "id",
            match field(guid, "value") {
                Some(Value::String(text)) => text.as_str(),
                _ => "",
            },
        );
    } else if let Some(link) = truthy(item, "link") {
        entry.text("id", link);
    }

    let mut links = Vec::new();
    if let Some(link) = truthy(item, "link") {
        links.push(
            Obj::new()
                .text("href", link)
                .text("rel", "alternate")
                .done(),
        );
    }
    if let Some(enclosure) = field(item, "enclosure") {
        if let Some(url) = truthy(enclosure, "url") {
            let mut link = Obj::new();
            link.text("href", url);
            link.text("rel", "enclosure");
            link.maybe_text("type", truthy(enclosure, "type"));
            // The canonical test is `!== undefined`, so a length of zero
            // is carried across.
            link.maybe("length", field(enclosure, "length").cloned());
            links.push(link.done());
        }
    }
    if let Some(comments) = truthy(item, "comments") {
        links.push(
            Obj::new()
                .text("href", comments)
                .text("rel", "replies")
                .text("type", "text/html")
                .done(),
        );
    }
    entry.maybe_list("links", links);

    if let Some(author) = truthy(item, "author") {
        entry.set("authors", Value::array(vec![parse_rss_person(author)]));
    }

    entry.maybe_list(
        "categories",
        list(item, "categories")
            .iter()
            .map(category_to_atom)
            .collect(),
    );

    if let Some(source) = field(item, "source") {
        let mut src = Obj::new();
        src.text("format", "atom");
        src.text("version", "1.0");
        if let Some(value) = truthy(source, "value") {
            src.set("title", atom_text("text", value));
        }
        if let Some(url) = truthy(source, "url") {
            src.set(
                "links",
                Value::array(vec![Obj::new()
                    .text("href", url)
                    .text("rel", "self")
                    .done()]),
            );
        }
        entry.set("source", src.done());
    }

    entry.done()
}

fn rss1_to_atom(rss: &Value) -> Value {
    let mut out = Obj::new();
    out.text("format", "atom");
    out.text("version", "1.0");
    out.set("entries", Value::array(Vec::new()));

    if let Some(title) = truthy(rss, "title") {
        out.set("title", atom_text("text", title));
    }
    if let Some(description) = truthy(rss, "description") {
        out.set("subtitle", atom_text("text", description));
    }
    if let Some(id) = truthy(rss, "about").or_else(|| truthy(rss, "link")) {
        out.text("id", id);
    }
    if let Some(link) = truthy(rss, "link") {
        out.set(
            "links",
            Value::array(vec![Obj::new()
                .text("href", link)
                .text("rel", "alternate")
                .done()]),
        );
    }
    if let Some(logo) = field(rss, "image").and_then(|image| truthy(image, "url")) {
        out.text("logo", logo);
    }

    let entries: Vec<Value> = list(rss, "items")
        .iter()
        .map(|item| {
            let mut entry = Obj::new();
            if let Some(title) = truthy(item, "title") {
                entry.set("title", atom_text("text", title));
            }
            if let Some(description) = truthy(item, "description") {
                entry.set("summary", atom_text("text", description));
            }
            // Assigned unconditionally in the canonical conversion, so an
            // item with neither an about nor a link still carries an
            // empty id rather than none.
            entry.text(
                "id",
                truthy(item, "about")
                    .or_else(|| match field(item, "link") {
                        Some(Value::String(text)) => Some(text.as_str()),
                        _ => None,
                    })
                    .unwrap_or(""),
            );
            if let Some(link) = truthy(item, "link") {
                entry.set(
                    "links",
                    Value::array(vec![Obj::new()
                        .text("href", link)
                        .text("rel", "alternate")
                        .done()]),
                );
            }
            entry.done()
        })
        .collect();
    out.set("entries", Value::array(entries));

    out.done()
}

// --- Top-level conversion -------------------------------------------------

/// Convert a parsed element tree to the requested output shape.
///
/// [`FeedFormat::Raw`] hands the tree straight back. Otherwise the root
/// element is detected, parsed to its native shape, and, for the default
/// [`FeedFormat::Atom`], mapped onto the Atom shape. A root element that
/// is not `feed`, `rss` or `RDF` is an error.
///
/// ```
/// let root = tabnas_xml::parse("<rss version=\"2.0\"><channel><title>x</title></channel></rss>")?;
/// let native = tabnas_feed::convert(&root, tabnas_feed::FeedFormat::Native)?;
/// assert_eq!(native.to_json()["version"], "2.0");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn convert(root: &Value, format: FeedFormat) -> Result<Value, String> {
    if format == FeedFormat::Raw {
        return Ok(root.clone());
    }
    if !is_element(root) {
        return Err("feed: input did not parse to an XML element".to_string());
    }

    let Detection { dialect, version } = detect(root);
    if dialect == FeedDialect::Unknown {
        return Err(format!(
            "feed: unrecognized root element \"{}\"; expected one of feed, rss, RDF",
            local_name(root)
        ));
    }

    // Native first, whatever the requested shape.
    let native = match dialect {
        FeedDialect::Atom => parse_atom(root, version == FeedVersion::Atom03),
        FeedDialect::Rss => parse_rss2(root, version),
        FeedDialect::Rdf => parse_rss1(root, version),
        FeedDialect::Unknown => unreachable!("an unknown dialect returned above"),
    };

    if format == FeedFormat::Native {
        return Ok(native);
    }

    Ok(match dialect {
        FeedDialect::Atom => native,
        FeedDialect::Rss => rss2_to_atom(&native),
        _ => rss1_to_atom(&native),
    })
}

// --- The plugin -----------------------------------------------------------

/// Install the feed plugin on `parser`, which should already carry the
/// jsonic grammar (as [`make`] arranges): the port of the `Feed` plugin
/// function.
///
/// The plugin contributes no rules. It installs [`tabnas_xml`] with
/// `strictNamespaces` from the options and hooks the existing `xml`
/// rule's before-close, so the conversion runs once over the finished
/// element tree. Installation is idempotent: an instance that already
/// carries the plugin is left alone.
///
/// ```
/// let mut parser = tabnas_jsonic::make();
/// tabnas_feed::feed(&mut parser, &tabnas_feed::FeedOptions::default())?;
/// let value = parser.parse("<rss version=\"2.0\"><channel><title>x</title></channel></rss>")?;
/// assert_eq!(value.to_json()["title"]["value"], "x");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn feed(parser: &mut Tabnas, options: &FeedOptions) -> Result<(), PluginError> {
    if parser.decoration::<bool>(INIT_DECORATION).is_some() {
        return Ok(());
    }
    parser.decorate(INIT_DECORATION, true);

    let xml_options = tabnas_xml::XmlOptions {
        strict_namespaces: options.strict_namespaces,
        ..tabnas_xml::XmlOptions::default()
    };
    parser
        .use_plugin(tabnas_xml::plugin(), Some(xml_options.to_value()))
        .map_err(|error| PluginError(format!("feed: setup xml: {error}")))?;

    let format = options.format;

    // The xml rule's before-close fires once per close, including the
    // extra times `r: xml` recurses to consume trailing white space.
    // Mirror the guard of xml's own hook: run only when an element was
    // parsed in THIS iteration, so the conversion happens exactly once,
    // on the same iteration that hook copied the element to the document
    // node. The second guard, that the node is still an element, is what
    // makes a second pass a no-op rather than a second conversion.
    parser.define_rule("xml", move |spec| {
        spec.add_bc_with_state(Arc::new(
            move |rule: &mut Rule,
                  _context: &mut Context,
                  _next: Option<&RuleSnapshot>,
                  _out: Option<Token>|
                  -> Result<Option<Token>, ActionError> {
                if rule.child_node.is_undefined() {
                    return Ok(None);
                }
                let root = rule.node.borrow().clone();
                if !is_element(&root) {
                    return Ok(None);
                }
                match convert(&root, format) {
                    // The start rule's cell IS the document node, so it
                    // is written in place, as xml's own hook writes it.
                    Ok(value) => {
                        *rule.node.borrow_mut() = value;
                        Ok(None)
                    }
                    Err(message) => Err(ActionError::new(ERROR_UNRECOGNIZED_ROOT, message)),
                }
            },
        ));
    });

    Ok(())
}

/// The plugin form of [`feed`], for [`Tabnas::use_plugin`]. Options are
/// read from the plugin option bag with [`FeedOptions::from_value`].
///
/// ```
/// let mut parser = tabnas_jsonic::make();
/// parser.use_plugin(tabnas_feed::plugin(), None)?;
/// let value = parser.parse("<feed xmlns=\"http://www.w3.org/2005/Atom\"/>")?;
/// assert_eq!(value.to_json()["version"], "1.0");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn plugin() -> Plugin {
    Plugin::new(PLUGIN_NAME, |parser, options| {
        feed(parser, &FeedOptions::from_value(options))
    })
    .with_defaults(FeedOptions::default().to_value())
}

/// Build a feed parser with caller options: the jsonic grammar, then this
/// plugin, the `new Tabnas().use(jsonic).use(Feed, options)` of the
/// canonical package.
///
/// ```
/// let options = tabnas_feed::FeedOptions {
///     format: tabnas_feed::FeedFormat::Native,
///     ..Default::default()
/// };
/// let parser = tabnas_feed::make_with(&options);
/// let value = parser.parse("<rss version=\"0.92\"><channel/></rss>")?;
/// assert_eq!(value.to_json()["version"], "0.92");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn make_with(options: &FeedOptions) -> Tabnas {
    let mut parser = tabnas_jsonic::make();
    parser
        .use_plugin(plugin(), Some(options.to_value()))
        .expect("the feed plugin installs on a jsonic instance");
    parser
}

/// Build a feed parser with the default options.
///
/// Building one costs far more than a parse, so an instance is meant to
/// be reused; it parses through `&self` and is `Send + Sync`.
///
/// ```
/// let parser = tabnas_feed::make();
/// let value = parser.parse("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title>Hi</title></feed>")?;
/// assert_eq!(value.to_json()["title"]["value"], "Hi");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn make() -> Tabnas {
    make_with(&FeedOptions::default())
}

/// Parse one feed with the shared default parser.
///
/// ```
/// let value = tabnas_feed::parse("<rss version=\"2.0\"><channel><title>News</title></channel></rss>")?;
/// assert_eq!(value.to_json()["title"]["value"], "News");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn parse(src: &str) -> Result<Value, FeedError> {
    static SHARED: std::sync::OnceLock<Tabnas> = std::sync::OnceLock::new();
    SHARED.get_or_init(make).parse(src)
}

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_examples {}