rd-ast 0.0.1

Canonical, producer-agnostic AST for R's Rd (documentation) format
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
//! Parsing for Sweave-style `\Sexpr` option lists.
//!
//! The option storage/view contract is in the crate's included `CONTRACT.md`.
//!
//! The grammar is a comma-separated list of scalar `key=value` entries. It
//! has no quoting, escaping, or nesting. Parsed pairs are preserved (with
//! surrounding ASCII whitespace removed) while unknown keys, duplicates, and
//! invalid typed values are collected as soft diagnostics. Typed resolution
//! uses the last valid value for each key. This grammar is distinct from the
//! `\link` and `\item` option shapes; those have their own semantic views.

use std::{borrow::Cow, fmt};

use crate::{RdNode, RdNodeKind, RdPath, RdShapeError, RdShapeErrorKind};

/// A parsed, borrowed Rd option list and its non-fatal diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RdOptionList<'a> {
    path: RdPath,
    pairs: Vec<RdOptionPair<'a>>,
    diagnostics: Vec<RdOptionError>,
    _nodes: std::marker::PhantomData<&'a [RdNode]>,
}

impl<'a> RdOptionList<'a> {
    /// Parses plain string leaves as a comma-separated scalar option list.
    ///
    /// R's `parse_Rd()` represents a `\\RdOpts` body as a `VERB` leaf while
    /// `\\Sexpr`'s `Rd_option` content is a `TEXT` leaf. Both are plain string
    /// leaves, so both kinds are concatenated in source order without
    /// normalization.
    ///
    /// `path` is the path of the option-content container supplied by the
    /// caller. A future `\\Sexpr` view passes `base_path.with_option()`; a
    /// future `\\RdOpts` view passes its own tagged-node path whose children
    /// are the option text. Pair positions are reported by `pair_index`, not
    /// by adding path segments.
    pub fn parse(nodes: &'a [RdNode], path: RdPath) -> Result<Self, RdOptionError> {
        let mut text = String::new();
        for (index, node) in nodes.iter().enumerate() {
            match node {
                RdNode::Text(value) | RdNode::Verb(value) => text.push_str(value),
                other => {
                    return Err(RdShapeError::new(
                        path.with_child(index),
                        None,
                        RdShapeErrorKind::UnexpectedContent {
                            actual: RdNodeKind::of(other),
                        },
                    )
                    .into());
                }
            }
        }

        let mut pairs = Vec::new();
        let mut diagnostics = Vec::new();
        if text.is_empty() {
            return Ok(Self {
                path,
                pairs,
                diagnostics,
                _nodes: std::marker::PhantomData,
            });
        }
        for (pair_index, entry) in text.split(',').enumerate() {
            if entry.is_empty() {
                return Err(RdOptionError::malformed(
                    path.clone(),
                    pair_index,
                    entry,
                    RdOptionPairErrorKind::EmptyPair,
                ));
            }
            let Some((raw_key, raw_value)) = entry.split_once('=') else {
                return Err(RdOptionError::malformed(
                    path.clone(),
                    pair_index,
                    entry,
                    RdOptionPairErrorKind::MissingEquals,
                ));
            };
            let key = raw_key.trim_ascii();
            let value = raw_value.trim_ascii();
            let reason = if key.is_empty() {
                Some(RdOptionPairErrorKind::EmptyKey)
            } else if value.is_empty() {
                Some(RdOptionPairErrorKind::EmptyValue)
            } else {
                None
            };
            if let Some(reason) = reason {
                return Err(RdOptionError::malformed(
                    path.clone(),
                    pair_index,
                    entry,
                    reason,
                ));
            }

            let pair = RdOptionPair {
                index: pair_index,
                key: Cow::Owned(key.to_string()),
                value: Cow::Owned(value.to_string()),
            };
            if RdSexprOptionKey::from_name(key).is_none() {
                diagnostics.push(RdOptionError::UnknownKey {
                    path: path.clone(),
                    pair_index,
                    key: key.to_string(),
                });
            }
            if let Some(first_pair_index) = pairs.iter().find(|p| p.key() == key).map(|p| p.index) {
                diagnostics.push(RdOptionError::DuplicateKey {
                    path: path.clone(),
                    pair_index,
                    key: key.to_string(),
                    first_pair_index,
                });
            }
            if let Some(known) = RdSexprOptionKey::from_name(key)
                && decode_value(known, value).is_err()
            {
                diagnostics.push(RdOptionError::InvalidValue {
                    path: path.clone(),
                    pair_index,
                    key: known,
                    value: value.to_string(),
                    expected: known.value_kind(),
                });
            }
            pairs.push(pair);
        }

        Ok(Self {
            path,
            pairs,
            diagnostics,
            _nodes: std::marker::PhantomData,
        })
    }

    /// Returns the base path supplied to the parser.
    pub fn path(&self) -> &RdPath {
        &self.path
    }
    /// Returns all parsed pairs in source order.
    pub fn pairs(&self) -> &[RdOptionPair<'a>] {
        &self.pairs
    }
    /// Returns non-fatal diagnostics, in occurrence order.
    pub fn diagnostics(&self) -> &[RdOptionError] {
        &self.diagnostics
    }
    /// Resolves valid typed values, using the last valid occurrence per key.
    pub fn typed(&self) -> RdSexprOptionOverrides {
        let mut result = RdSexprOptionOverrides::empty();
        for pair in &self.pairs {
            if let Some(key) = pair.known_key()
                && let Ok(value) = decode_value(key, pair.value())
            {
                result.set(key, value);
            }
        }
        result
    }
}

/// One preserved key/value pair from an option list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RdOptionPair<'a> {
    index: usize,
    key: Cow<'a, str>,
    value: Cow<'a, str>,
}

impl<'a> RdOptionPair<'a> {
    /// Returns the zero-based pair index.
    pub fn index(&self) -> usize {
        self.index
    }
    /// Returns the trimmed key spelling.
    pub fn key(&self) -> &str {
        &self.key
    }
    /// Returns the trimmed value spelling.
    pub fn value(&self) -> &str {
        &self.value
    }
    /// Returns the typed vocabulary key, if recognized.
    pub fn known_key(&self) -> Option<RdSexprOptionKey> {
        RdSexprOptionKey::from_name(&self.key)
    }
}

/// The recognized names in a `\Sexpr` option list.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RdSexprOptionKey {
    /// Evaluation stage.
    Stage,
    /// Results rendering mode.
    Results,
    /// Echo source code.
    Echo,
    /// Evaluate the expression.
    Eval,
    /// Keep source code.
    KeepSource,
    /// Strip whitespace.
    StripWhite,
    /// Figure width.
    Width,
    /// Figure height.
    Height,
    /// Figure number.
    Fig,
}

impl RdSexprOptionKey {
    /// Converts an exact Rd option name to its vocabulary key.
    pub fn from_name(name: &str) -> Option<Self> {
        Some(match name {
            "stage" => Self::Stage,
            "results" => Self::Results,
            "echo" => Self::Echo,
            "eval" => Self::Eval,
            "keep.source" => Self::KeepSource,
            "strip.white" => Self::StripWhite,
            "width" => Self::Width,
            "height" => Self::Height,
            "fig" => Self::Fig,
            _ => return None,
        })
    }
    /// Returns the canonical Rd spelling of this key.
    pub const fn as_name(self) -> &'static str {
        match self {
            Self::Stage => "stage",
            Self::Results => "results",
            Self::Echo => "echo",
            Self::Eval => "eval",
            Self::KeepSource => "keep.source",
            Self::StripWhite => "strip.white",
            Self::Width => "width",
            Self::Height => "height",
            Self::Fig => "fig",
        }
    }
    fn value_kind(self) -> RdOptionValueKind {
        match self {
            Self::Stage => RdOptionValueKind::Stage,
            Self::Results => RdOptionValueKind::Results,
            Self::Echo | Self::Eval | Self::KeepSource => RdOptionValueKind::Boolean,
            Self::StripWhite => RdOptionValueKind::StripWhite,
            Self::Width | Self::Height | Self::Fig => RdOptionValueKind::Boolean,
        }
    }
}

/// An Rd `stage` value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdSexprStage {
    /// Build stage.
    Build,
    /// Install stage.
    Install,
    /// Render stage.
    Render,
}
/// An Rd `results` value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdSexprResults {
    /// Text results.
    Text,
    /// Verbatim results.
    Verbatim,
    /// Rd results.
    Rd,
    /// Hidden results.
    Hide,
}
/// An Rd `strip.white` value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdStripWhite {
    /// Do not trim whitespace.
    None,
    /// Trim ordinary whitespace.
    Trim,
    /// Trim all whitespace.
    All,
}

/// Typed overrides extracted from an Rd option list.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RdSexprOptionOverrides {
    /// Stage override.
    pub stage: Option<RdSexprStage>,
    /// Results override.
    pub results: Option<RdSexprResults>,
    /// Echo override.
    pub echo: Option<bool>,
    /// Evaluation override.
    pub eval: Option<bool>,
    /// Keep-source override.
    pub keep_source: Option<bool>,
    /// Strip-whitespace override.
    pub strip_white: Option<RdStripWhite>,
}
impl RdSexprOptionOverrides {
    /// Creates an override set with no values.
    pub const fn empty() -> Self {
        Self {
            stage: None,
            results: None,
            echo: None,
            eval: None,
            keep_source: None,
            strip_white: None,
        }
    }
    /// Applies present overrides to effective options.
    pub fn apply_to(self, options: &mut RdEffectiveSexprOptions) {
        if let Some(v) = self.stage {
            options.stage = v;
        }
        if let Some(v) = self.results {
            options.results = v;
        }
        if let Some(v) = self.echo {
            options.echo = v;
        }
        if let Some(v) = self.eval {
            options.eval = v;
        }
        if let Some(v) = self.keep_source {
            options.keep_source = v;
        }
        if let Some(v) = self.strip_white {
            options.strip_white = v;
        }
    }
    fn set(&mut self, key: RdSexprOptionKey, value: DecodedValue) {
        match (key, value) {
            (RdSexprOptionKey::Stage, DecodedValue::Stage(v)) => self.stage = Some(v),
            (RdSexprOptionKey::Results, DecodedValue::Results(v)) => self.results = Some(v),
            (RdSexprOptionKey::Echo, DecodedValue::Boolean(v)) => self.echo = Some(v),
            (RdSexprOptionKey::Eval, DecodedValue::Boolean(v)) => self.eval = Some(v),
            (RdSexprOptionKey::KeepSource, DecodedValue::Boolean(v)) => self.keep_source = Some(v),
            (RdSexprOptionKey::StripWhite, DecodedValue::StripWhite(v)) => {
                self.strip_white = Some(v)
            }
            _ => {}
        }
    }
}

/// Fully resolved options for an Rd `\Sexpr` expression.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RdEffectiveSexprOptions {
    /// Evaluation stage.
    pub stage: RdSexprStage,
    /// Results rendering mode.
    pub results: RdSexprResults,
    /// Echo source code.
    pub echo: bool,
    /// Evaluate the expression.
    pub eval: bool,
    /// Keep source code.
    pub keep_source: bool,
    /// Strip whitespace.
    pub strip_white: RdStripWhite,
}
impl Default for RdEffectiveSexprOptions {
    fn default() -> Self {
        Self {
            stage: RdSexprStage::Install,
            results: RdSexprResults::Text,
            echo: false,
            eval: true,
            keep_source: true,
            strip_white: RdStripWhite::Trim,
        }
    }
}

/// A hard or soft option-list diagnostic.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdOptionError {
    /// A node had an invalid shape.
    Shape(RdShapeError),
    /// A scalar pair was malformed.
    MalformedPair {
        path: RdPath,
        pair_index: usize,
        text: String,
        reason: RdOptionPairErrorKind,
    },
    /// The key is not recognized.
    UnknownKey {
        path: RdPath,
        pair_index: usize,
        key: String,
    },
    /// The key occurred previously.
    DuplicateKey {
        path: RdPath,
        pair_index: usize,
        key: String,
        first_pair_index: usize,
    },
    /// A recognized typed value could not be decoded.
    InvalidValue {
        path: RdPath,
        pair_index: usize,
        key: RdSexprOptionKey,
        value: String,
        expected: RdOptionValueKind,
    },
}
impl RdOptionError {
    fn malformed(
        path: RdPath,
        pair_index: usize,
        text: &str,
        reason: RdOptionPairErrorKind,
    ) -> Self {
        Self::MalformedPair {
            path,
            pair_index,
            text: text.to_string(),
            reason,
        }
    }

    /// Returns the diagnostic path.
    pub fn path(&self) -> &RdPath {
        match self {
            Self::Shape(error) => error.path(),
            Self::MalformedPair { path, .. }
            | Self::UnknownKey { path, .. }
            | Self::DuplicateKey { path, .. }
            | Self::InvalidValue { path, .. } => path,
        }
    }
}
impl From<RdShapeError> for RdOptionError {
    fn from(error: RdShapeError) -> Self {
        Self::Shape(error)
    }
}
impl fmt::Display for RdOptionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Shape(e) => e.fmt(f),
            Self::MalformedPair {
                path,
                pair_index,
                reason,
                ..
            } => write!(f, "malformed option pair {pair_index}: {reason} at {path}"),
            Self::UnknownKey {
                path,
                pair_index,
                key,
            } => write!(
                f,
                "unknown option key '{key}' (pair {pair_index}) at {path}"
            ),
            Self::DuplicateKey {
                path,
                pair_index,
                key,
                first_pair_index,
            } => write!(
                f,
                "duplicate option key '{key}' (pair {pair_index}; first pair {first_pair_index}) at {path}"
            ),
            Self::InvalidValue {
                path,
                pair_index,
                key,
                value,
                expected,
            } => write!(
                f,
                "invalid value '{value}' for option {} (pair {pair_index}; expected {expected}) at {path}",
                key.as_name()
            ),
        }
    }
}
impl std::error::Error for RdOptionError {}

/// Reasons a scalar option pair is malformed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdOptionPairErrorKind {
    /// The entry was empty.
    EmptyPair,
    /// No equals separator was present.
    MissingEquals,
    /// The key was empty.
    EmptyKey,
    /// The value was empty.
    EmptyValue,
}
/// Expected typed value categories for known options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RdOptionValueKind {
    /// A boolean.
    Boolean,
    /// A stage name.
    Stage,
    /// A results name.
    Results,
    /// A strip-white name.
    StripWhite,
}
impl fmt::Display for RdOptionPairErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::EmptyPair => "empty pair",
            Self::MissingEquals => "missing '='",
            Self::EmptyKey => "empty key",
            Self::EmptyValue => "empty value",
        })
    }
}
impl fmt::Display for RdOptionValueKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Boolean => "boolean",
            Self::Stage => "stage",
            Self::Results => "results",
            Self::StripWhite => "strip.white",
        })
    }
}

#[derive(Debug, Clone, Copy)]
enum DecodedValue {
    Boolean(bool),
    Stage(RdSexprStage),
    Results(RdSexprResults),
    StripWhite(RdStripWhite),
}
fn decode_value(key: RdSexprOptionKey, value: &str) -> Result<DecodedValue, ()> {
    match key {
        RdSexprOptionKey::Echo | RdSexprOptionKey::Eval | RdSexprOptionKey::KeepSource => value
            .eq_ignore_ascii_case("true")
            .then_some(DecodedValue::Boolean(true))
            .or_else(|| {
                value
                    .eq_ignore_ascii_case("false")
                    .then_some(DecodedValue::Boolean(false))
            })
            .ok_or(()),
        RdSexprOptionKey::Stage => match value {
            "build" => Ok(DecodedValue::Stage(RdSexprStage::Build)),
            "install" => Ok(DecodedValue::Stage(RdSexprStage::Install)),
            "render" => Ok(DecodedValue::Stage(RdSexprStage::Render)),
            _ => Err(()),
        },
        RdSexprOptionKey::Results => match value {
            "text" => Ok(DecodedValue::Results(RdSexprResults::Text)),
            "verbatim" => Ok(DecodedValue::Results(RdSexprResults::Verbatim)),
            "rd" => Ok(DecodedValue::Results(RdSexprResults::Rd)),
            "hide" => Ok(DecodedValue::Results(RdSexprResults::Hide)),
            _ => Err(()),
        },
        RdSexprOptionKey::StripWhite => {
            if value.eq_ignore_ascii_case("true") {
                Ok(DecodedValue::StripWhite(RdStripWhite::Trim))
            } else if value.eq_ignore_ascii_case("all") {
                Ok(DecodedValue::StripWhite(RdStripWhite::All))
            } else if value.eq_ignore_ascii_case("false") {
                Ok(DecodedValue::StripWhite(RdStripWhite::None))
            } else {
                Err(())
            }
        }
        RdSexprOptionKey::Width | RdSexprOptionKey::Height | RdSexprOptionKey::Fig => {
            Ok(DecodedValue::Boolean(false))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{RdPathSegment, RdTag};

    fn path() -> RdPath {
        RdPath::new(vec![RdPathSegment::TopLevel(4)])
    }
    fn text(value: &str) -> RdNode {
        RdNode::Text(value.to_string())
    }

    #[test]
    fn concatenates_text_nodes_and_preserves_pairs() {
        let nodes = [text(" stage = build,echo=true"), text(", results = rd ")];
        let parsed = RdOptionList::parse(&nodes, path()).unwrap();
        assert_eq!(
            parsed.pairs().iter().map(|p| p.index()).collect::<Vec<_>>(),
            [0, 1, 2]
        );
        assert_eq!(parsed.pairs()[0].key(), "stage");
        assert_eq!(parsed.pairs()[0].value(), "build");
        assert_eq!(parsed.typed().stage, Some(RdSexprStage::Build));
        assert_eq!(parsed.typed().echo, Some(true));
        assert_eq!(parsed.typed().results, Some(RdSexprResults::Rd));
    }

    #[test]
    fn accepts_verb_and_mixed_string_leaves() {
        let verb = [RdNode::Verb("stage=build".to_string())];
        assert_eq!(
            RdOptionList::parse(&verb, path()).unwrap().typed().stage,
            Some(RdSexprStage::Build)
        );
        let mixed = [
            RdNode::Text("stage=build,".to_string()),
            RdNode::Verb("results=rd".to_string()),
        ];
        let parsed = RdOptionList::parse(&mixed, path()).unwrap();
        assert_eq!(parsed.typed().stage, Some(RdSexprStage::Build));
        assert_eq!(parsed.typed().results, Some(RdSexprResults::Rd));
    }

    #[test]
    fn malformed_reasons_are_hard_errors() {
        for (input, expected) in [
            ("", RdOptionPairErrorKind::EmptyPair),
            ("noequals", RdOptionPairErrorKind::MissingEquals),
            (" =value", RdOptionPairErrorKind::EmptyKey),
            ("key= ", RdOptionPairErrorKind::EmptyValue),
        ] {
            let nodes = [text(input)];
            let result = RdOptionList::parse(&nodes, path());
            if input.is_empty() {
                assert!(result.is_ok());
            } else {
                assert_eq!(result.as_ref().unwrap_err().path(), &path());
                assert!(
                    matches!(result, Err(RdOptionError::MalformedPair { reason, .. }) if reason == expected)
                );
            }
        }
        let error = RdOptionList::parse(&[text("a=1,,b=2")], path()).unwrap_err();
        assert_eq!(error.path(), &path());
        assert!(matches!(
            error,
            RdOptionError::MalformedPair {
                reason: RdOptionPairErrorKind::EmptyPair,
                ..
            }
        ));
    }

    #[test]
    fn rejects_non_text_at_node_path() {
        let nodes = [RdNode::tagged(RdTag::Title, None, vec![])];
        let error = RdOptionList::parse(&nodes, path()).unwrap_err();
        assert!(
            matches!(error, RdOptionError::Shape(shape) if shape.path() == &path().with_child(0) && matches!(shape.kind(), RdShapeErrorKind::UnexpectedContent { actual: RdNodeKind::Tagged }))
        );
    }

    #[test]
    fn decodes_all_typed_keys_and_case_insensitive_values() {
        let nodes = [text(
            "stage=render,results=verbatim,echo=TrUe,eval=false,keep.source=FALSE,strip.white=TRUE",
        )];
        let typed = RdOptionList::parse(&nodes, path()).unwrap().typed();
        assert_eq!(typed.stage, Some(RdSexprStage::Render));
        assert_eq!(typed.results, Some(RdSexprResults::Verbatim));
        assert_eq!(typed.echo, Some(true));
        assert_eq!(typed.eval, Some(false));
        assert_eq!(typed.keep_source, Some(false));
        assert_eq!(typed.strip_white, Some(RdStripWhite::Trim));
        for value in ["false", "all", "true"] {
            let nodes = [text(&format!("strip.white={value}"))];
            let parsed = RdOptionList::parse(&nodes, path()).unwrap();
            assert_eq!(
                parsed.typed().strip_white,
                Some(match value {
                    "false" => RdStripWhite::None,
                    "all" => RdStripWhite::All,
                    _ => RdStripWhite::Trim,
                })
            );
        }
    }

    #[test]
    fn collects_soft_diagnostics_and_keeps_pairs() {
        let nodes = [text("unknown=x,echo=true,echo=maybe,echo=false")];
        let parsed = RdOptionList::parse(&nodes, path()).unwrap();
        assert_eq!(parsed.pairs().len(), 4);
        assert!(
            parsed
                .diagnostics()
                .iter()
                .any(|e| matches!(e, RdOptionError::UnknownKey { key, .. } if key == "unknown"))
        );
        assert!(
            parsed
                .diagnostics()
                .iter()
                .all(|error| error.path() == &path())
        );
        assert!(parsed.diagnostics().iter().any(|e| matches!(
            e,
            RdOptionError::DuplicateKey {
                first_pair_index: 1,
                pair_index: 2,
                ..
            }
        )));
        assert!(parsed.diagnostics().iter().any(|e| matches!(
            e,
            RdOptionError::InvalidValue {
                pair_index: 2,
                expected: RdOptionValueKind::Boolean,
                ..
            }
        )));
        assert_eq!(parsed.typed().echo, Some(false));
    }

    #[test]
    fn formats_soft_diagnostics_with_pair_indices() {
        let nodes = [text("unknown=x,echo=true,echo=maybe")];
        let parsed = RdOptionList::parse(&nodes, path()).unwrap();
        let messages: Vec<_> = parsed
            .diagnostics()
            .iter()
            .map(ToString::to_string)
            .collect();
        assert_eq!(
            messages,
            [
                "unknown option key 'unknown' (pair 0) at top-level[4]",
                "duplicate option key 'echo' (pair 2; first pair 1) at top-level[4]",
                "invalid value 'maybe' for option echo (pair 2; expected boolean) at top-level[4]",
            ]
        );
    }

    #[test]
    fn invalid_duplicate_does_not_erase_last_valid_value() {
        let nodes = [text("stage=build,stage=bad")];
        let parsed = RdOptionList::parse(&nodes, path()).unwrap();
        assert_eq!(parsed.typed().stage, Some(RdSexprStage::Build));
    }

    #[test]
    fn figure_keys_are_known_but_raw_only() {
        let nodes = [text("width=bad,height=also-bad,fig=anything")];
        let parsed = RdOptionList::parse(&nodes, path()).unwrap();
        assert_eq!(
            parsed
                .pairs()
                .iter()
                .map(|p| p.known_key())
                .collect::<Vec<_>>(),
            [
                Some(RdSexprOptionKey::Width),
                Some(RdSexprOptionKey::Height),
                Some(RdSexprOptionKey::Fig)
            ]
        );
        assert_eq!(parsed.typed(), RdSexprOptionOverrides::empty());
        assert!(parsed.diagnostics().is_empty());
    }

    #[test]
    fn empty_inputs_and_defaults_are_valid() {
        assert!(RdOptionList::parse(&[], path()).unwrap().pairs().is_empty());
        assert!(
            RdOptionList::parse(&[text("")], path())
                .unwrap()
                .pairs()
                .is_empty()
        );
        let defaults = RdEffectiveSexprOptions::default();
        assert_eq!(
            defaults,
            RdEffectiveSexprOptions {
                stage: RdSexprStage::Install,
                results: RdSexprResults::Text,
                echo: false,
                eval: true,
                keep_source: true,
                strip_white: RdStripWhite::Trim
            }
        );
    }

    #[test]
    fn option_error_is_displayable_standard_error() {
        let error = RdOptionList::parse(&[text("bad")], path()).unwrap_err();
        assert!(error.to_string().contains("missing '='"));
        let _: Box<dyn std::error::Error> = Box::new(error);
    }
}