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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Unlike `impl Into<Option<T>>` or `Option<impl Into<T>>`, this isn't ambiguous for the `None`
// case.

use crate::builder::ArgAction;
use crate::builder::OsStr;
use crate::builder::Str;
use crate::builder::StyledStr;
use crate::builder::ValueHint;
use crate::builder::ValueParser;
use crate::builder::ValueRange;

/// Clearable builder value
///
/// This allows a builder function to both accept any value that can [`Into::into`] `T` (like
/// `&str` into `OsStr`) as well as `None` to reset it to the default.  This is needed to
/// workaround a limitation where you can't have a function argument that is `impl Into<Option<T>>`
/// where `T` is `impl Into<S>` accept `None` as its type is ambiguous.
///
/// # Example
///
/// ```rust
/// # use clap_builder as clap;
/// # use clap::Command;
/// # use clap::Arg;
/// fn common() -> Command {
///     Command::new("cli")
///         .arg(Arg::new("input").short('i').long("input"))
/// }
/// let mut command = common();
/// command.mut_arg("input", |arg| arg.short(None));
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Resettable<T> {
    /// Overwrite builder value
    Value(T),
    /// Reset builder value
    Reset,
}

impl<T> Resettable<T> {
    pub(crate) fn into_option(self) -> Option<T> {
        match self {
            Self::Value(t) => Some(t),
            Self::Reset => None,
        }
    }
}

impl<T> From<T> for Resettable<T> {
    fn from(other: T) -> Self {
        Self::Value(other)
    }
}

impl<T> From<Option<T>> for Resettable<T> {
    fn from(other: Option<T>) -> Self {
        match other {
            Some(inner) => Self::Value(inner),
            None => Self::Reset,
        }
    }
}

/// Convert to the intended resettable type
pub trait IntoResettable<T> {
    /// Convert to the intended resettable type
    fn into_resettable(self) -> Resettable<T>;
}

impl IntoResettable<char> for Option<char> {
    fn into_resettable(self) -> Resettable<char> {
        match self {
            Some(s) => Resettable::Value(s),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<usize> for Option<usize> {
    fn into_resettable(self) -> Resettable<usize> {
        match self {
            Some(s) => Resettable::Value(s),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<ArgAction> for Option<ArgAction> {
    fn into_resettable(self) -> Resettable<ArgAction> {
        match self {
            Some(s) => Resettable::Value(s),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<ValueHint> for Option<ValueHint> {
    fn into_resettable(self) -> Resettable<ValueHint> {
        match self {
            Some(s) => Resettable::Value(s),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<ValueParser> for Option<ValueParser> {
    fn into_resettable(self) -> Resettable<ValueParser> {
        match self {
            Some(s) => Resettable::Value(s),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<StyledStr> for Option<&'static str> {
    fn into_resettable(self) -> Resettable<StyledStr> {
        match self {
            Some(s) => Resettable::Value(s.into()),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<OsStr> for Option<&'static str> {
    fn into_resettable(self) -> Resettable<OsStr> {
        match self {
            Some(s) => Resettable::Value(s.into()),
            None => Resettable::Reset,
        }
    }
}

impl IntoResettable<Str> for Option<&'static str> {
    fn into_resettable(self) -> Resettable<Str> {
        match self {
            Some(s) => Resettable::Value(s.into()),
            None => Resettable::Reset,
        }
    }
}

impl<T> IntoResettable<T> for Resettable<T> {
    fn into_resettable(self) -> Resettable<T> {
        self
    }
}

impl IntoResettable<char> for char {
    fn into_resettable(self) -> Resettable<char> {
        Resettable::Value(self)
    }
}

impl IntoResettable<usize> for usize {
    fn into_resettable(self) -> Resettable<usize> {
        Resettable::Value(self)
    }
}

impl IntoResettable<ArgAction> for ArgAction {
    fn into_resettable(self) -> Resettable<ArgAction> {
        Resettable::Value(self)
    }
}

impl IntoResettable<ValueHint> for ValueHint {
    fn into_resettable(self) -> Resettable<ValueHint> {
        Resettable::Value(self)
    }
}

impl<I: Into<ValueRange>> IntoResettable<ValueRange> for I {
    fn into_resettable(self) -> Resettable<ValueRange> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<ValueParser>> IntoResettable<ValueParser> for I {
    fn into_resettable(self) -> Resettable<ValueParser> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<String>> IntoResettable<String> for I {
    fn into_resettable(self) -> Resettable<String> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<StyledStr>> IntoResettable<StyledStr> for I {
    fn into_resettable(self) -> Resettable<StyledStr> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<OsStr>> IntoResettable<OsStr> for I {
    fn into_resettable(self) -> Resettable<OsStr> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<Str>> IntoResettable<Str> for I {
    fn into_resettable(self) -> Resettable<Str> {
        Resettable::Value(self.into())
    }
}

impl<I: Into<crate::Id>> IntoResettable<crate::Id> for I {
    fn into_resettable(self) -> Resettable<crate::Id> {
        Resettable::Value(self.into())
    }
}