summaryrefslogtreecommitdiff
path: root/src/useflag/parsers.rs
blob: fc371a69ab1f0e4b9f8a9c07689c608f4779da68 (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
use mon::{Parser, ParserIter, 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 = alphanumeric();
        let rest = 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,
            }))
    }
}