Change description format

Descriptions must now be prefixed by a '#' symbol instead of indented.
This commit is contained in:
Joscha 2021-11-19 00:48:12 +01:00
parent 76eed7f30d
commit d7c843ed86
3 changed files with 24 additions and 27 deletions

View file

@ -213,7 +213,7 @@ pub struct Task {
pub until: Option<NaiveDate>,
pub except: Vec<NaiveDate>,
pub done: Vec<Done>,
pub desc: Option<String>,
pub desc: Vec<String>,
}
#[derive(Debug)]
@ -223,7 +223,7 @@ pub struct Note {
pub from: Option<NaiveDate>,
pub until: Option<NaiveDate>,
pub except: Vec<NaiveDate>,
pub desc: Option<String>,
pub desc: Vec<String>,
}
#[derive(Debug)]
@ -236,7 +236,7 @@ pub struct BirthdaySpec {
pub struct Birthday {
pub title: String,
pub when: BirthdaySpec,
pub desc: Option<String>,
pub desc: Vec<String>,
}
#[derive(Debug)]

View file

@ -24,7 +24,9 @@ fn fail<S: Into<String>, T>(span: Span, message: S) -> Result<T> {
fn parse_title(p: Pair<Rule>) -> Result<String> {
assert_eq!(p.as_rule(), Rule::title);
Ok(p.into_inner().next().unwrap().as_str().to_string())
let p = p.into_inner().next().unwrap();
assert_eq!(p.as_rule(), Rule::rest_some);
Ok(p.as_str().to_string())
}
fn parse_datum(p: Pair<Rule>) -> Result<NaiveDate> {
@ -94,23 +96,20 @@ fn parse_options(p: Pair<Rule>) -> Result<Options> {
Ok(opts)
}
fn parse_indented_line(p: Pair<Rule>) -> Result<String> {
assert_eq!(p.as_rule(), Rule::indented_line);
Ok(p.as_str().to_string())
fn parse_desc_line(p: Pair<Rule>) -> Result<String> {
assert_eq!(p.as_rule(), Rule::desc_line);
Ok(match p.into_inner().next() {
None => "".to_string(),
Some(p) => {
assert_eq!(p.as_rule(), Rule::rest_any);
p.as_str().to_string()
}
})
}
fn parse_description(p: Pair<Rule>) -> Result<Option<String>> {
fn parse_description(p: Pair<Rule>) -> Result<Vec<String>> {
assert_eq!(p.as_rule(), Rule::description);
let lines = p
.into_inner()
.map(parse_indented_line)
.collect::<Result<Vec<String>>>()?;
// TODO Strip whitespace prefix
let desc = lines.join("\n").trim_end().to_string();
Ok(if desc.is_empty() { None } else { Some(desc) })
p.into_inner().map(parse_desc_line).collect()
}
fn parse_task(p: Pair<Rule>) -> Result<Task> {

View file

@ -1,8 +1,9 @@
eol = _{ NEWLINE | EOI }
WHITESPACE = _{ !eol ~ WHITE_SPACE }
rest = { (!eol ~ ANY)+ }
rest_some = { (!eol ~ ANY)+ }
rest_any = { (!eol ~ ANY)* }
title = { WHITESPACE ~ rest ~ eol }
title = { WHITESPACE ~ rest_some ~ eol }
year = @{ ASCII_DIGIT{4} }
month = @{ ASCII_DIGIT{2} }
@ -72,12 +73,8 @@ except = !{ "EXCEPT" ~ datum ~ eol }
donedate = { "(" ~ datum ~ time ~ ")" }
done = !{ "DONE" ~ datum? ~ donedate? ~ 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* }
desc_line = { "#" ~ (" " ~ rest_any)? ~ eol }
description = { desc_line* }
task_options = { (date | from | until | except | done)* }
@ -104,6 +101,7 @@ birthday = {
~ description
}
empty_line = _{ WHITESPACE* ~ NEWLINE }
command = { task | note | birthday }
file = ${ SOI ~ NEWLINE* ~ command* ~ EOI }
file = ${ SOI ~ (empty_line* ~ command)* ~ empty_line* ~ WHITESPACE* ~ EOI }