summaryrefslogtreecommitdiff
path: root/src/useflag/parsers.rs
blob: ca5929d74b02bc455b3daf8f51d29e23d8eb07df (plain)
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
use mon::{Parser, ParserIter, ascii_alphanumeric, one_of, tag};

use crate::{
    Parseable,
    useflag::{IUseFlag, UseFlag},
};

impl<'a> Parseable<'a, &'a str> for UseFlag {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let start = ascii_alphanumeric();
        let rest = ascii_alphanumeric()
            .or(one_of("+_@-".chars()))
            .repeated()
            .many();

        start
            .and(rest)
            .recognize()
            .map(|output: &str| UseFlag(output.to_string()))
    }
}

impl<'a> Parseable<'a, &'a str> for IUseFlag {
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        UseFlag::parser()
            .preceded_by(tag("+"))
            .map(|flag| IUseFlag {
                default: true,
                flag,
            })
            .or(UseFlag::parser().map(|flag| IUseFlag {
                default: false,
                flag,
            }))
    }
}