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
use crate::{Parser, Result, Value};
impl<'a> Parser<'a> {
/// Parse a block-style sequence at the given indent
///
/// Input:
/// ```yaml
/// - a
/// - b
/// ```
///
/// Output: `Value::Seq([String("a"), String("b")])`
///
/// Stops at EOF or a line whose indent doesn't match `indent` (or whose
/// first non-blank char isn't a sequence dash).
pub(super) fn parse_block_seq(&mut self, indent: usize) -> Result<Value<'a>> {
let mut items = Vec::new();
loop {
self.skip_blank_and_comment_lines();
if self.at_eof() {
// Stop when reaching EOF
break;
}
// Normalize: if not already at dash, advance past indent
if self.peek() != Some(b'-') {
if self.current_indent()? != indent {
// Confirm current indentation
break;
}
for _ in 0..indent {
self.advance();
}
}
if self.peek() != Some(b'-') || !self.is_seq_dash() {
// Need start with dash
break;
}
self.advance(); // eat `-`
self.skip_spaces();
items.push(self.parse_node(indent + 1)?);
}
Ok(Value::Seq(items))
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_seq {
($yaml:expr, $expected:expr) => {
let mut p = Parser::new($yaml);
let v = p.parse_block_seq(0).unwrap();
match v {
Value::Seq(items) => {
let strs: Vec<&str> = items
.iter()
.map(|i| match i {
Value::String(s) => s.as_ref(),
_ => panic!("expected string, got {:?}", i),
})
.collect();
assert_eq!(strs, $expected);
}
_ => panic!("expected Seq"),
}
};
}
#[test]
fn seq_two_items() {
assert_seq!("- a\n- b\n", vec!["a", "b"]);
}
#[test]
fn seq_one_item() {
assert_seq!("- only\n", vec!["only"]);
}
#[test]
fn seq_no_trailing_newline() {
assert_seq!("- a\n- b", vec!["a", "b"]);
}
#[test]
fn seq_with_blank_lines() {
assert_seq!("- a\n\n- b\n", vec!["a", "b"]);
}
#[test]
fn seq_with_comment_lines() {
assert_seq!("- a\n# comment\n- b\n", vec!["a", "b"]);
}
#[test]
fn seq_quoted_values() {
assert_seq!(
r#"- "foo"
- 'bar'
"#,
vec!["foo", "bar"]
);
}
#[test]
fn seq_with_spaces_in_items() {
assert_seq!("- hello world\n- foo bar\n", vec!["hello world", "foo bar"]);
}
#[test]
fn seq_dashes_in_value() {
// "-foo" is a plain scalar; the dash is part of the value because no space follows
// here it would NOT be parsed as a nested seq item
// but the outer seq should still get "value"
assert_seq!("- -value\n- other\n", vec!["-value", "other"]);
}
#[test]
fn seq_stops_at_lesser_indent() {
// when reading at indent 2, a "- " at indent 0 ends our scope
let mut p = Parser::new(" - a\n - b\nother: x\n");
let v = p.parse_block_seq(2).unwrap();
match v {
Value::Seq(items) => assert_eq!(items.len(), 2),
_ => panic!(),
}
// cursor should be at 'o' of "other"
assert_eq!(p.peek(), Some(b'o'));
}
#[test]
fn seq_empty_at_eof() {
// if nothing follows, we get an empty seq
let mut p = Parser::new("");
let v = p.parse_block_seq(0).unwrap();
assert!(matches!(v, Value::Seq(items) if items.is_empty()));
}
}