summaryrefslogtreecommitdiff
path: root/mingling/src/parser/picker/bools.rs
blob: f0a262246f260ba138c714237981162c42819183 (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
130
131
132
133
134
135
136
137
138
139
140
use crate::parser::Pickable;

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

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

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

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

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

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

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

impl Pickable for YesOrNo {
    type Output = YesOrNo;

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

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

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

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

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

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

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

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

impl Pickable for TrueOrFalse {
    type Output = TrueOrFalse;

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

fn pick_bool(
    args: &mut crate::parser::Argument,
    flag: mingling_core::Flag,
    positive: &[&str],
    negative: &[&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();
                if positive.contains(&s) {
                    true
                } else if negative.contains(&s) {
                    false
                } else {
                    false
                }
            }
            None => false,
        }
    } else {
        true
    }
}