2023-09-05 02:39:24 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::take_till;
|
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::space0;
|
|
|
|
use nom::character::complete::space1;
|
|
|
|
use nom::combinator::eof;
|
|
|
|
use nom::combinator::opt;
|
|
|
|
use nom::combinator::verify;
|
|
|
|
use nom::multi::separated_list0;
|
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
|
|
|
use crate::error::Res;
|
|
|
|
|
2023-09-06 15:45:35 +00:00
|
|
|
// ref https://orgmode.org/manual/Per_002dfile-keywords.html
|
|
|
|
// ref https://orgmode.org/manual/Workflow-states.html
|
|
|
|
// Case is significant.
|
|
|
|
|
2023-09-05 02:39:24 +00:00
|
|
|
/// Parses the text in the value of a #+TODO keyword.
|
|
|
|
///
|
|
|
|
/// Example input: "foo bar baz | lorem ipsum"
|
2023-09-11 17:13:28 +00:00
|
|
|
pub(crate) fn todo_keywords<'s>(input: &'s str) -> Res<&'s str, (Vec<&'s str>, Vec<&'s str>)> {
|
2023-09-06 15:45:35 +00:00
|
|
|
let (remaining, mut before_pipe_words) = separated_list0(space1, todo_keyword_word)(input)?;
|
2023-09-05 02:39:24 +00:00
|
|
|
let (remaining, after_pipe_words) = opt(tuple((
|
|
|
|
tuple((space0, tag("|"), space0)),
|
2023-09-06 15:45:35 +00:00
|
|
|
separated_list0(space1, todo_keyword_word),
|
2023-09-05 02:39:24 +00:00
|
|
|
)))(remaining)?;
|
|
|
|
let (remaining, _eol) = alt((line_ending, eof))(remaining)?;
|
|
|
|
if let Some((_pipe, after_pipe_words)) = after_pipe_words {
|
|
|
|
Ok((remaining, (before_pipe_words, after_pipe_words)))
|
|
|
|
} else if !before_pipe_words.is_empty() {
|
|
|
|
// If there was no pipe, then the last word becomes a completion state instead.
|
|
|
|
let mut after_pipe_words = Vec::with_capacity(1);
|
|
|
|
after_pipe_words.push(
|
|
|
|
before_pipe_words
|
|
|
|
.pop()
|
|
|
|
.expect("If-statement proves this is Some."),
|
|
|
|
);
|
|
|
|
Ok((remaining, (before_pipe_words, after_pipe_words)))
|
|
|
|
} else {
|
|
|
|
// No words founds
|
|
|
|
Ok((remaining, (Vec::new(), Vec::new())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 15:45:35 +00:00
|
|
|
fn todo_keyword_word<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
2023-09-14 06:24:06 +00:00
|
|
|
let (remaining, keyword) = verify(take_till(|c| "( \t\r\n|".contains(c)), |result: &str| {
|
2023-09-05 02:39:24 +00:00
|
|
|
!result.is_empty()
|
2023-09-14 06:24:06 +00:00
|
|
|
})(input)?;
|
|
|
|
|
|
|
|
let (remaining, _) = opt(tuple((
|
|
|
|
tag("("),
|
|
|
|
take_till(|c| "() \t\r\n|".contains(c)),
|
|
|
|
tag(")"),
|
|
|
|
)))(remaining)?;
|
|
|
|
|
|
|
|
Ok((remaining, keyword))
|
2023-09-05 02:39:24 +00:00
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn before_and_after() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let input = "foo bar baz | lorem ipsum";
|
2023-09-06 15:45:35 +00:00
|
|
|
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
2023-09-05 02:39:24 +00:00
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(before_pipe_words, vec!["foo", "bar", "baz"]);
|
|
|
|
assert_eq!(after_pipe_words, vec!["lorem", "ipsum"]);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let input = "foo bar baz";
|
2023-09-06 15:45:35 +00:00
|
|
|
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
2023-09-05 02:39:24 +00:00
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(before_pipe_words, vec!["foo", "bar"]);
|
|
|
|
assert_eq!(after_pipe_words, vec!["baz"]);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn early_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let input = "| foo bar baz";
|
2023-09-06 15:45:35 +00:00
|
|
|
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
2023-09-05 02:39:24 +00:00
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(before_pipe_words, Vec::<&str>::new());
|
|
|
|
assert_eq!(after_pipe_words, vec!["foo", "bar", "baz"]);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn late_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let input = "foo bar baz |";
|
2023-09-06 15:45:35 +00:00
|
|
|
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
2023-09-05 02:39:24 +00:00
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(before_pipe_words, vec!["foo", "bar", "baz"]);
|
|
|
|
assert_eq!(after_pipe_words, Vec::<&str>::new());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|