summaryrefslogtreecommitdiff
path: root/crates/useflag/src/parsers.rs
blob: 3007bdea1d9affa5b707091fc1dbf6cf131bd424 (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
use parseable::Parseable;

use mon::{Parser, ParserIter, ascii_alphanumeric, one_of, tag};

use crate::{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,
            }))
    }
}