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
use std::marker::PhantomData;

use nom::branch::alt;
use nom::bytes::complete::tag_no_case;

use super::CommandThen;
use crate::{ArgumentMarkerDefaultImpl, ChildUsage, CommandArgument, CommandError, IntoMultipleUsage, Then};

/// Create a boolean parser
pub fn boolean<S>(name: &'static str) -> BoolArgument<S> {
    BoolArgument {
        name,
        source: PhantomData,
    }
}

/// Boolean argument parser.
///
/// This parser has no fields because it simply parses either `"true"` or
/// `"false`".
pub struct BoolArgument<S> {
    name: &'static str,
    source: PhantomData<S>,
}

impl<S> CommandArgument<S, bool> for BoolArgument<S> {
    fn parse<'a>(&self, _source: S, input: &'a str) -> nom::IResult<&'a str, bool, CommandError<'a>> {
        alt((
            |i| {
                let (i, _) = tag_no_case("true")(i)?;
                Ok((i, true))
            },
            |i| {
                let (i, _) = tag_no_case("false")(i)?;
                Ok((i, false))
            },
        ))(input)
    }
}

impl<S> ArgumentMarkerDefaultImpl for BoolArgument<S> {}

impl<S, E> Then<E> for BoolArgument<S> {
    type Output = CommandThen<Self, E, bool, S>;

    fn then(self, executor: E) -> Self::Output {
        CommandThen {
            argument: self,
            executor,
            output: PhantomData,
            source: PhantomData,
        }
    }
}

impl<S> IntoMultipleUsage for BoolArgument<S> {
    type Item = <[&'static str; 3] as IntoMultipleUsage>::Item;

    fn usage_gen(&self) -> Self::Item { self.usage_child().usage_gen() }
}

impl<S> ChildUsage for BoolArgument<S> {
    type Child = [&'static str; 3];

    fn usage_child(&self) -> Self::Child { ["<", self.name, ">"] }
}