aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/parser/picker/bools.rs
blob: 4015cd9710d2ad677f45010869bcfc1988b902f7 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::parser::Pickable;

#[derive(Debug, Default)]
pub enum Yes {
    Yes,
    #[default]
    No,
}

impl From<bool> for Yes {
    fn from(b: bool) -> Self {
        if b { Yes::Yes } else { Yes::No }
    }
}

impl From<Yes> for bool {
    fn from(val: Yes) -> Self {
        match val {
            Yes::Yes => true,
            Yes::No => false,
        }
    }
}

impl std::ops::Deref for Yes {
    type Target = bool;

    fn deref(&self) -> &Self::Target {
        static TRUE: bool = true;
        static FALSE: bool = false;
        match self {
            Yes::Yes => &TRUE,
            Yes::No => &FALSE,
        }
    }
}

impl Yes {
    pub fn is_yes(&self) -> bool {
        matches!(self, Yes::Yes)
    }

    pub fn is_no(&self) -> bool {
        matches!(self, Yes::No)
    }
}

impl Pickable for Yes {
    type Output = Yes;

    fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> {
        let value = pick_bool(args, flag, &["y", "yes"]);
        Some(value.into())
    }
}

#[derive(Debug, Default)]
pub enum True {
    True,
    #[default]
    False,
}

impl From<bool> for True {
    fn from(b: bool) -> Self {
        if b { True::True } else { True::False }
    }
}

impl From<True> for bool {
    fn from(val: True) -> Self {
        match val {
            True::True => true,
            True::False => false,
        }
    }
}

impl std::ops::Deref for True {
    type Target = bool;

    fn deref(&self) -> &Self::Target {
        static TRUE: bool = true;
        static FALSE: bool = false;
        match self {
            True::True => &TRUE,
            True::False => &FALSE,
        }
    }
}

impl True {
    pub fn is_true(&self) -> bool {
        matches!(self, True::True)
    }

    pub fn is_false(&self) -> bool {
        matches!(self, True::False)
    }
}

impl Pickable for True {
    type Output = True;

    fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> {
        let value = pick_bool(args, flag, &["true", "t"]);
        Some(value.into())
    }
}

fn pick_bool(
    args: &mut crate::parser::Argument,
    flag: mingling_core::Flag,
    positive: &[&str],
) -> bool {
    let has_flag = args.pick_flag(flag.clone());
    if !has_flag {
        let content = args.pick_argument(flag);
        match content {
            Some(content) => {
                let s = content.as_str();
                positive.contains(&s)
            }
            None => false,
        }
    } else {
        true
    }
}