summaryrefslogtreecommitdiff
path: root/src/repo/profile/parsers.rs
blob: 44e767e42d009cbbedcd79667341724e2b887049 (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
use mon::{Parser, ParserIter, any, ascii_whitespace1, tag};

use crate::{
    Parseable,
    repo::profile::{FlagOperation, LineBasedFileExpr},
    useflag::UseFlag,
};

impl<'a, T> Parseable<'a, &'a str> for LineBasedFileExpr<T>
where
    T: Parseable<'a, &'a str>,
{
    type Parser = impl Parser<&'a str, Output = Self>;

    fn parser() -> Self::Parser {
        let comment = tag("#")
            .preceded_by(ascii_whitespace1().opt())
            .followed_by(any().and_not(tag("\n")).repeated().many())
            .map(|_| LineBasedFileExpr::Comment);
        let expr = T::parser().map(|expr| LineBasedFileExpr::Expr(expr));

        comment.or(expr)
    }
}

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

    fn parser() -> Self::Parser {
        UseFlag::parser()
            .preceded_by(tag("-"))
            .map(FlagOperation::Remove)
            .or(UseFlag::parser().map(FlagOperation::Add))
    }
}