extract fixture parsing

This commit is contained in:
Aleksey Kladov 2018-10-31 21:37:32 +03:00
parent b58ca6b1a6
commit 64ce895ef0
4 changed files with 51 additions and 22 deletions

View file

@ -87,3 +87,45 @@ pub fn add_cursor(text: &str, offset: TextUnit) -> String {
res.push_str(&text[offset..]);
res
}
#[derive(Debug)]
pub struct FixtureEntry {
pub meta: String,
pub text: String,
}
/// Parses text wich looks like this:
///
/// ```notrust
/// //- some meta
/// line 1
/// line 2
/// // - other meta
/// ```
pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
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();
}
};
};
for line in fixture.lines() {
if line.starts_with("//-") {
flush!();
buf.clear();
meta = Some(line["//-".len()..].trim());
continue;
}
buf.push_str(line);
buf.push('\n');
}
flush!();
res
}