From d2fd252f9de23d5801b1ca10c067654bf7d6ef4f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Jan 2020 14:06:23 +0100 Subject: [PATCH] Simplify fixture parsing --- crates/test_utils/src/lib.rs | 41 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 265fcf8da0..5666445aa5 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -176,7 +176,7 @@ pub fn parse_fixture(fixture: &str) -> Vec { .next() .expect("empty fixture"); - let lines = fixture + let mut lines = fixture .split('\n') // don't use `.lines` to not drop `\r\n` .filter_map(|line| { if line.len() >= margin { @@ -189,29 +189,28 @@ pub fn parse_fixture(fixture: &str) -> Vec { }); let mut res = Vec::new(); - let mut buf = String::new(); - let mut meta: Option<&str> = None; - - macro_rules! flush { - () => { - if let Some(meta) = meta { - res.push(FixtureEntry { meta: meta.to_string(), text: buf.clone() }); - buf.clear(); + let mut meta = None; + loop { + let mut next_meta = None; + let mut text = String::new(); + for line in lines.by_ref() { + if line.starts_with("//-") { + next_meta = Some(line["//-".len()..].trim().to_string()); + break; } - }; - }; - - for line in lines { - if line.starts_with("//-") { - flush!(); - buf.clear(); - meta = Some(line["//-".len()..].trim()); - continue; + text.push_str(line); + text.push('\n'); + } + + if let Some(meta) = meta { + res.push(FixtureEntry { meta, text }); + } + meta = next_meta; + if meta.is_none() { + break; } - buf.push_str(line); - buf.push('\n'); } - flush!(); + res }