~jonn/todo-rs-public unlisted

todo-rs-public/src/parser.rs -rw-r--r-- 3.2 KiB
86650f2eJonn Problem: no attribution in "parsing stuff in rust" 2 months ago
                                                                                
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
use crate::*;

use nom::{
    IResult,
    bytes::complete::{tag, take_while1},
    branch::alt,
    character::{
        complete::{one_of, space0, digit1},
    },
    multi::{separated_list, many1},
    sequence::{pair, preceded, delimited},
};

pub fn query(input : &str) -> IResult<&str, Query> {
    alt((add, done, search))(input)
}

fn ws(input : &str) -> IResult<&str, char> { one_of(" \t")(input)
}


fn add(input : &str) -> IResult<&str, Query> {
    match preceded(
        pair(tag("add"), ws),
        pair(description, preceded(space0, tags))
    )(input) {
        Err(e) => Err(e),
        Ok( (rest, (d, ts)) ) => Ok((
            rest,
            Query::Add(Description::new(&d), ts)
        )),
    }
}

fn is_lowecase_or_dash_or_whitespace(c : char) -> bool {
    c.is_ascii_lowercase() || c.is_whitespace() || c == '-'
}

fn is_lowecase_or_dash(c : char) -> bool {
    c.is_ascii_lowercase() || c == '-'
}

fn sentence(input : &str) -> IResult<&str, &str> {
    take_while(is_lowecase_or_dash_or_whitespace)(input)
}

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

fn todo_tag(input : &str) -> IResult<&str, &str> {
    preceded(tag("#"), word)(input)
}

fn description(input : &str) -> IResult<&str, String> {
    match delimited(tag("\""), sentence, tag("\""))(input) {
        Err(e) => Err(e),
        Ok((rest, d)) => Ok((rest, d.to_string())),
    }
}

fn tags(input : &str) -> IResult<&str, Vec<Tag>> {
    match separated_list(ws, todo_tag)(input) {
        Err(e) => Err(e),
        Ok((rest, ts)) => Ok((
            rest,
            ts.iter().map(|w| Tag::new(w)).collect()
        )),
    }
}

fn done(input : &str) -> IResult<&str, Query> {
    match preceded(
        pair(tag("done"), ws),
        many1(digit1)
    )(input) {
        Err(e) => Err(e),
        Ok((rest, i)) => Ok((
            rest,
            Query::Done(Index::new(vec_to_u64(i)))
        ))
    }
}

fn vec_to_u64(dss : Vec<&str>) -> u64 {
    let ds = dss.iter().fold("".to_string(), |acc, x| format!("{}{}", acc, x));
    ds.parse::<u64>().unwrap()
}

enum SearchWordOrTag {
    RawWord (String),
    RawTag (String),
}

fn search(input : &str) -> IResult<&str, Query> {
    match preceded(pair(tag("search"), ws),
        separated_list(
            tag(" "),
            search_word_or_tag
        )
    )(input) {
        Err(e) => Err(e),
        Ok((rest, mash)) => Ok((rest, mash_to_query(mash))),
    }
}

fn search_word_or_tag(input : &str) -> IResult<&str, SearchWordOrTag> {
    match alt((todo_tag, word))(input) {
        Err(e) => Err(e),
        Ok((rest, wot)) => {
            if wot.starts_with("#") {
                Ok( (rest, SearchWordOrTag::RawTag(wot[1..].to_string())) )
            } else {
                Ok( (rest, SearchWordOrTag::RawWord(wot.to_string())) )
            }
        }
    }
}

fn mash_to_query(mash : Vec<SearchWordOrTag>) -> Query {
    let mut search_words : Vec<SearchWord> = vec![];
    let mut tags : Vec<Tag> = vec![];
    for i in mash {
        match i {
            SearchWordOrTag::RawWord(w) => search_words.push(SearchWord::new(&w)),
            SearchWordOrTag::RawTag(t)  => tags.push(Tag::new(&t)),
        }
    }
    Query::Search(SearchParams{
        words: search_words,
        tags
    })
}