1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::task::TaskPriority;
use chrono::NaiveDateTime;
use color_eyre::eyre::{bail, Context, Result};
use nom::{
    branch::alt,
    bytes::complete::{tag, take_while1},
    character::{complete::char, is_newline, is_space},
    combinator::{all_consuming, map},
    sequence::{preceded, separated_pair},
    IResult,
};
use std::str::FromStr;
use thiserror::Error;

// https://imfeld.dev/writing/parsing_with_nom
// https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md
#[derive(Debug, PartialEq, Eq)]
enum ExpressionPrototype<'a> {
    Description(&'a str),
    Project(&'a str),
    Tag(&'a str),
    Metadata { key: &'a str, value: &'a str },
    Priority(&'a str),
    Duedate(&'a str),
}

/// Expression components
#[derive(Debug, PartialEq, Eq)]
pub enum Expression {
    /// Description read from task definition string. Matches everything not matched elsewhere.
    Description(String),
    /// Project component from task definition string
    Project(String),
    /// Tag component from task definition string
    Tag(String),
    /// Metadata key=value pair component from task definition string
    Metadata {
        /// Key of the metadata value
        key: String,
        /// Value of the metadata
        value: String,
    },
    /// Priority component from task definition string
    Priority(TaskPriority),
    /// Duedate component from task definition string
    Duedate(NaiveDateTime),
}

impl Expression {
    fn from_prototype(prototype: &ExpressionPrototype) -> Result<Self> {
        Ok(match prototype {
            ExpressionPrototype::Description(text) => Expression::Description(String::from(*text)),
            ExpressionPrototype::Project(text) => Expression::Project(String::from(*text)),
            ExpressionPrototype::Tag(text) => Expression::Tag(String::from(*text)),
            ExpressionPrototype::Metadata { key, value } => Expression::Metadata {
                key: String::from(*key),
                value: String::from(*value),
            },
            ExpressionPrototype::Priority(text) => {
                let mut prio_string = text.to_string().to_lowercase();
                prio_string = prio_string[0..1].to_uppercase() + &prio_string[1..];
                Expression::Priority(
                    TaskPriority::from_str(&prio_string)
                        .with_context(|| "invalid priority specified in descriptor")?,
                )
            }
            ExpressionPrototype::Duedate(text) => Expression::Duedate(
                NaiveDateTime::from_str(text)
                    .with_context(|| "invalid date time format for duedate in descriptor")?,
            ),
        })
    }
}

fn nonws_char(c: char) -> bool {
    !is_space(c as u8) && !is_newline(c as u8)
}

fn allowed_meta_character(c: char) -> bool {
    nonws_char(c) && c != '='
}

fn word(input: &str) -> IResult<&str, &str> {
    take_while1(nonws_char)(input)
}

fn meta_word(input: &str) -> IResult<&str, &str> {
    take_while1(allowed_meta_character)(input)
}

fn metadata_pair(input: &str) -> IResult<&str, (&str, &str)> {
    separated_pair(meta_word, char('='), meta_word)(input)
}

fn hashtag(input: &str) -> IResult<&str, &str> {
    preceded(char('#'), word)(input)
}

fn hashtag2(input: &str) -> IResult<&str, &str> {
    preceded(alt((tag("tag:"), tag("TAG:"))), word)(input)
}

fn project(input: &str) -> IResult<&str, &str> {
    preceded(char('@'), word)(input)
}

fn project2(input: &str) -> IResult<&str, &str> {
    preceded(
        alt((tag("prj:"), tag("proj:"), tag("PRJ:"), tag("PROJ:"))),
        word,
    )(input)
}

fn metadata(input: &str) -> IResult<&str, (&str, &str)> {
    preceded(char('%'), metadata_pair)(input)
}

fn metadata2(input: &str) -> IResult<&str, (&str, &str)> {
    preceded(alt((tag("META:"), tag("meta:"))), metadata_pair)(input)
}

fn priority(input: &str) -> IResult<&str, &str> {
    preceded(alt((tag("prio:"), tag("PRIO:"))), word)(input)
}

fn due_date(input: &str) -> IResult<&str, &str> {
    preceded(
        alt((tag("due:"), tag("DUE:"), tag("duedate:"), tag("DUEDATE:"))),
        word,
    )(input)
}

fn directive(input: &str) -> IResult<&str, ExpressionPrototype> {
    alt((
        map(hashtag, ExpressionPrototype::Tag),
        map(hashtag2, ExpressionPrototype::Tag),
        map(project, ExpressionPrototype::Project),
        map(project2, ExpressionPrototype::Project),
        map(metadata, |(key, value)| ExpressionPrototype::Metadata {
            key,
            value,
        }),
        map(metadata2, |(key, value)| ExpressionPrototype::Metadata {
            key,
            value,
        }),
        map(priority, ExpressionPrototype::Priority),
        map(due_date, ExpressionPrototype::Duedate),
    ))(input)
}

fn parse_inline(input: &str) -> IResult<&str, Vec<ExpressionPrototype>> {
    let mut output = Vec::with_capacity(4);
    let mut current_input = input;

    while !current_input.is_empty() {
        let mut found_directive = false;
        for (current_index, _) in current_input.char_indices() {
            // println!("{} {}", current_index, current_input);
            match directive(&current_input[current_index..]) {
                Ok((remaining, parsed)) => {
                    // println!("Matched {:?} remaining {}", parsed, remaining);
                    let leading_text = &current_input[0..current_index].trim();
                    if !leading_text.is_empty() {
                        output.push(ExpressionPrototype::Description(leading_text));
                    }
                    output.push(parsed);

                    current_input = remaining;
                    found_directive = true;
                    break;
                }
                Err(nom::Err::Error(_)) => {
                    // None of the parsers matched at the current position, so this character is just part of the text.
                    // The iterator will go to the next character so there's nothing to do here.
                }
                Err(e) => {
                    // On any other error, just return the error.
                    return Err(e);
                }
            }
        }

        if !found_directive {
            // no directives matched so just add the text as is into the Description
            output.push(ExpressionPrototype::Description(current_input.trim()));
            break;
        }
    }

    Ok(("", output))
}

/// Errors that can happend during parsing of the task descriptor string
#[derive(Error, Debug, PartialEq, Eq)]
pub enum LexiconError {
    /// There was a problem while parsing the descriptor string
    #[error("i got confused by the language")]
    ParserError(String),
}

/// Parses descriptor string based on known lexicon and returns result of elements
pub fn parse_task(input: String) -> Result<Vec<Expression>> {
    let parsed = alt((all_consuming(parse_inline),))(&input).map(|(_, results)| results);

    match parsed {
        Ok(prototype_expressions) => {
            let mut ready_expressions: Vec<Expression> = vec![];
            for expression_prototype in prototype_expressions {
                ready_expressions.push(
                    Expression::from_prototype(&expression_prototype)
                        .with_context(|| "malformed expression in task descriptor")?,
                );
            }
            Ok(ready_expressions)
        }
        Err(error) => bail!(LexiconError::ParserError(error.to_string())),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn nonws_char_allowed() {
        assert!(nonws_char('a'))
    }

    #[test]
    fn nonws_char_whitespace() {
        assert!(!nonws_char(' '))
    }

    #[test]
    fn allowed_meta_character_hyphen() {
        assert!(allowed_meta_character('-'))
    }

    #[test]
    fn allowed_meta_character_whitespace() {
        assert!(!allowed_meta_character(' '))
    }

    #[test]
    fn word_from_trimmed() {
        assert_eq!(word("word").unwrap(), ("", "word"));
    }

    #[test]
    fn word_from_untrimmed_r() {
        assert_eq!(word("word ").unwrap(), (" ", "word"));
    }

    #[test]
    fn word_from_untrimmed_l() {
        assert!(word(" word").is_err());
    }

    #[test]
    fn metaword_from_allowed() {
        assert_eq!(meta_word("x-meta-word").unwrap(), ("", "x-meta-word"));
    }

    #[test]
    fn metaword_from_whitespace() {
        assert_eq!(meta_word("x-meta -word").unwrap(), (" -word", "x-meta"));
    }

    #[test]
    fn tag_valid() {
        assert_eq!(hashtag("#fubar").unwrap(), ("", "fubar"));
    }

    #[test]
    fn tag2_valid() {
        assert_eq!(hashtag2("TAG:fubar").unwrap(), ("", "fubar"));
    }

    #[test]
    fn tag_broken() {
        assert_eq!(hashtag("#fu bar").unwrap(), (" bar", "fu"));
    }

    #[test]
    fn tag_broken_noprefix() {
        assert!(hashtag("asfd").is_err());
    }

    #[test]
    fn metadata_pair_valid() {
        assert_eq!(
            metadata_pair("x-meta=value").unwrap(),
            ("", ("x-meta", "value"))
        );
    }

    #[test]
    fn project_valid() {
        assert_eq!(project("@fubar").unwrap(), ("", "fubar"));
    }

    #[test]
    fn project2_valid() {
        assert_eq!(project2("PRJ:fubar").unwrap(), ("", "fubar"));
    }

    #[test]
    fn metadata_pair_broken() {
        assert!(metadata_pair("x-meta = value").is_err());
    }

    #[test]
    fn parse_full_testcase() {
        let input = "some task description here @project-here #taghere #a-second-tag %x-meta=data %fuu=bar additional text at the end";

        let (leftover, mut meta) = parse_inline(input).unwrap();

        assert_eq!(leftover, "");
        // assert the expressions from Vec
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Description("additional text at the end")
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Metadata {
                key: "fuu",
                value: "bar"
            }
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Metadata {
                key: "x-meta",
                value: "data"
            }
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Tag("a-second-tag")
        );
        assert_eq!(meta.pop().unwrap(), ExpressionPrototype::Tag("taghere"));
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Project("project-here")
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Description("some task description here")
        );
    }

    #[test]
    fn parse_full_testcase2() {
        let input = "some task description here PRJ:project-here #taghere TAG:a-second-tag META:x-meta=data %fuu=bar DUE:2022-08-16T16:56:00 PRIO:medium and some text at the end";

        let (leftover, mut meta) = parse_inline(input).unwrap();

        assert_eq!(leftover, "");
        // assert the expressions from Vec
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Description("and some text at the end")
        );
        assert_eq!(meta.pop().unwrap(), ExpressionPrototype::Priority("medium"));
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Duedate("2022-08-16T16:56:00")
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Metadata {
                key: "fuu",
                value: "bar"
            }
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Metadata {
                key: "x-meta",
                value: "data"
            }
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Tag("a-second-tag")
        );
        assert_eq!(meta.pop().unwrap(), ExpressionPrototype::Tag("taghere"));
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Project("project-here")
        );
        assert_eq!(
            meta.pop().unwrap(),
            ExpressionPrototype::Description("some task description here")
        );
    }

    #[test]
    fn parse_full_testcase_no_expressions() {
        let input = "some task description here without expressions";

        let (leftover, mut meta) = parse_inline(input).unwrap();

        assert_eq!(leftover, "");
        // after pulling single description expresssion out of the vec
        assert_eq!(meta.pop().unwrap(), ExpressionPrototype::Description(input));
        // ... check that the vec is actually now empty
        assert!(meta.is_empty());
    }
}