From afe9548b1d8fddb5492f492336afb8cc9944919f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 19 Aug 2018 00:49:35 +0200 Subject: map_collection(): Handle whitespace between map definitions Add support for blank, whitespace, and comment lines between and around map definition lines. Fiddled with the existing functions I had for this, but finally managed to get it working, still using Combine's `examples/ini.rs` as a reference. --- src/lib.rs | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index c912897..df490e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,12 +129,9 @@ where I: Stream, I::Error: ParseError, { - let comment = comment().map(|_| ()); - let blank = skip_many(newline()).or(comment); - ( - blank, - many::, _>(map()), + blank(), + many::, _>(map().skip(blank())), ).map(|(_, collection)| { let mut maps = HashMap::new(); @@ -157,15 +154,24 @@ where I: Stream, I::Error: ParseError, { - skip_many( - ( - token('#'), - skip_many(satisfy(|c| c != '\n')), - )// .map(|_| ()) + ( + token('#'), + skip_many(satisfy(|c| c != '\n')), ) } +fn blank() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + let comment = comment().map(|_| ()); + let whitespace = whitespace_separator().map(|_| ()); + skip_many(skip_many1(newline()).or(whitespace).or(comment)) +} + + #[cfg(test)] mod tests { use super::*; @@ -241,7 +247,13 @@ mod tests { #[test] fn map_collection_parses_maps() { let text = " +# Test comment + # continued + map test +map salt and pepper + +# Another comment cmd /usr/bin/say 'hello' "; let result = map_collection().easy_parse(text).map(|t| t.0); @@ -254,6 +266,13 @@ cmd /usr/bin/say 'hello' kind: MapKind::Map, }, ); + expected.insert( + vec![HeadphoneButton::Play], + MapAction { + action: "salt and pepper".to_owned(), + kind: MapKind::Map, + }, + ); expected.insert( vec![HeadphoneButton::Down], MapAction { -- cgit v1.2.3