Skip to content

Commit d2fd252

Browse files
committed
Simplify fixture parsing
1 parent 7cc0a86 commit d2fd252

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

crates/test_utils/src/lib.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
176176
.next()
177177
.expect("empty fixture");
178178

179-
let lines = fixture
179+
let mut lines = fixture
180180
.split('\n') // don't use `.lines` to not drop `\r\n`
181181
.filter_map(|line| {
182182
if line.len() >= margin {
@@ -189,29 +189,28 @@ pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
189189
});
190190

191191
let mut res = Vec::new();
192-
let mut buf = String::new();
193-
let mut meta: Option<&str> = None;
194-
195-
macro_rules! flush {
196-
() => {
197-
if let Some(meta) = meta {
198-
res.push(FixtureEntry { meta: meta.to_string(), text: buf.clone() });
199-
buf.clear();
192+
let mut meta = None;
193+
loop {
194+
let mut next_meta = None;
195+
let mut text = String::new();
196+
for line in lines.by_ref() {
197+
if line.starts_with("//-") {
198+
next_meta = Some(line["//-".len()..].trim().to_string());
199+
break;
200200
}
201-
};
202-
};
201+
text.push_str(line);
202+
text.push('\n');
203+
}
203204

204-
for line in lines {
205-
if line.starts_with("//-") {
206-
flush!();
207-
buf.clear();
208-
meta = Some(line["//-".len()..].trim());
209-
continue;
205+
if let Some(meta) = meta {
206+
res.push(FixtureEntry { meta, text });
207+
}
208+
meta = next_meta;
209+
if meta.is_none() {
210+
break;
210211
}
211-
buf.push_str(line);
212-
buf.push('\n');
213212
}
214-
flush!();
213+
215214
res
216215
}
217216

0 commit comments

Comments
 (0)