aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorTeddy Wing2018-08-19 00:49:35 +0200
committerTeddy Wing2018-08-19 01:01:58 +0200
commitafe9548b1d8fddb5492f492336afb8cc9944919f (patch)
treecb7b7f1d7c7107edd479111936e3477a44c20dcd /src/lib.rs
parentf026d7521f17736fe4f44c1ab052328d423f3a5c (diff)
downloaddome-key-map-afe9548b1d8fddb5492f492336afb8cc9944919f.tar.bz2
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs39
1 files changed, 29 insertions, 10 deletions
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<Item = char>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
{
- let comment = comment().map(|_| ());
- let blank = skip_many(newline()).or(comment);
-
(
- blank,
- many::<Vec<Map>, _>(map()),
+ blank(),
+ many::<Vec<Map>, _>(map().skip(blank())),
).map(|(_, collection)| {
let mut maps = HashMap::new();
@@ -157,15 +154,24 @@ where
I: Stream<Item = char>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
{
- skip_many(
- (
- token('#'),
- skip_many(satisfy(|c| c != '\n')),
- )// .map(|_| ())
+ (
+ token('#'),
+ skip_many(satisfy(|c| c != '\n')),
)
}
+fn blank<I>() -> impl Parser<Input = I>
+where
+ I: Stream<Item = char>,
+ I::Error: ParseError<I::Item, I::Range, I::Position>,
+{
+ 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 <up><down> test
+map <play> salt and pepper
+
+# Another comment
cmd <down> /usr/bin/say 'hello'
";
let result = map_collection().easy_parse(text).map(|t| t.0);
@@ -255,6 +267,13 @@ cmd <down> /usr/bin/say 'hello'
},
);
expected.insert(
+ vec![HeadphoneButton::Play],
+ MapAction {
+ action: "salt and pepper".to_owned(),
+ kind: MapKind::Map,
+ },
+ );
+ expected.insert(
vec![HeadphoneButton::Down],
MapAction {
action: "/usr/bin/say 'hello'".to_owned(),