From 76eed7f30dbb9aa50170a741608391d2c6beb243 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 18 Nov 2021 23:57:10 +0100 Subject: [PATCH] Parse descriptions properly --- src/parse.rs | 12 +++--------- src/parse/todayfile.pest | 8 ++++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index ee1b550..6cc1bdc 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -96,13 +96,7 @@ fn parse_options(p: Pair) -> Result { fn parse_indented_line(p: Pair) -> Result { assert_eq!(p.as_rule(), Rule::indented_line); - Ok(match p.into_inner().next() { - Some(rest) => { - assert_eq!(rest.as_rule(), Rule::rest); - rest.as_str().to_string() - } - None => "".to_string(), - }) + Ok(p.as_str().to_string()) } fn parse_description(p: Pair) -> Result> { @@ -115,8 +109,8 @@ fn parse_description(p: Pair) -> Result> { // TODO Strip whitespace prefix - let desc = lines.join("\n"); - Ok(Some(desc).filter(|s| !s.is_empty())) + let desc = lines.join("\n").trim_end().to_string(); + Ok(if desc.is_empty() { None } else { Some(desc) }) } fn parse_task(p: Pair) -> Result { diff --git a/src/parse/todayfile.pest b/src/parse/todayfile.pest index 79a44a9..a19b7e4 100644 --- a/src/parse/todayfile.pest +++ b/src/parse/todayfile.pest @@ -72,10 +72,10 @@ except = !{ "EXCEPT" ~ datum ~ eol } donedate = { "(" ~ datum ~ time ~ ")" } done = !{ "DONE" ~ datum? ~ donedate? ~ eol } -// I need to use `nl` for the empty line here. Otherwise, the parser gets into -// an endless loop at the `EOI` since `indented*` can appear at the end of the -// file and would just repeatedly match the empty string. -indented_line = { NEWLINE | WHITESPACE ~ rest ~ eol } +// I need to use `NEWLINE` for the empty line here. Otherwise, the parser gets +// into an endless loop at the `EOI` since `indented*` can appear at the end of +// the file and would just repeatedly match the empty string. +indented_line = @{ NEWLINE | WHITESPACE ~ rest ~ eol } description = { indented_line* }