]>
git.proxmox.com Git - rustc.git/blob - vendor/clap_builder-4.3.10/src/builder/resettable.rs
1 // Unlike `impl Into<Option<T>>` or `Option<impl Into<T>>`, this isn't ambiguous for the `None`
4 use crate::builder
::ArgAction
;
5 use crate::builder
::OsStr
;
6 use crate::builder
::Str
;
7 use crate::builder
::StyledStr
;
8 use crate::builder
::ValueHint
;
9 use crate::builder
::ValueParser
;
10 use crate::builder
::ValueRange
;
12 /// Clearable builder value
14 /// This allows a builder function to both accept any value that can [`Into::into`] `T` (like
15 /// `&str` into `OsStr`) as well as `None` to reset it to the default. This is needed to
16 /// workaround a limitation where you can't have a function argument that is `impl Into<Option<T>>`
17 /// where `T` is `impl Into<S>` accept `None` as its type is ambiguous.
22 /// # use clap_builder as clap;
23 /// # use clap::Command;
25 /// fn common() -> Command {
26 /// Command::new("cli")
27 /// .arg(Arg::new("input").short('i').long("input"))
29 /// let mut command = common();
30 /// command.mut_arg("input", |arg| arg.short(None));
32 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
33 pub enum Resettable
<T
> {
34 /// Overwrite builder value
36 /// Reset builder value
40 impl<T
> Resettable
<T
> {
41 pub(crate) fn into_option(self) -> Option
<T
> {
43 Self::Value(t
) => Some(t
),
49 impl<T
> From
<T
> for Resettable
<T
> {
50 fn from(other
: T
) -> Self {
55 impl<T
> From
<Option
<T
>> for Resettable
<T
> {
56 fn from(other
: Option
<T
>) -> Self {
58 Some(inner
) => Self::Value(inner
),
64 /// Convert to the intended resettable type
65 pub trait IntoResettable
<T
> {
66 /// Convert to the intended resettable type
67 fn into_resettable(self) -> Resettable
<T
>;
70 impl IntoResettable
<char> for Option
<char> {
71 fn into_resettable(self) -> Resettable
<char> {
73 Some(s
) => Resettable
::Value(s
),
74 None
=> Resettable
::Reset
,
79 impl IntoResettable
<usize> for Option
<usize> {
80 fn into_resettable(self) -> Resettable
<usize> {
82 Some(s
) => Resettable
::Value(s
),
83 None
=> Resettable
::Reset
,
88 impl IntoResettable
<ArgAction
> for Option
<ArgAction
> {
89 fn into_resettable(self) -> Resettable
<ArgAction
> {
91 Some(s
) => Resettable
::Value(s
),
92 None
=> Resettable
::Reset
,
97 impl IntoResettable
<ValueHint
> for Option
<ValueHint
> {
98 fn into_resettable(self) -> Resettable
<ValueHint
> {
100 Some(s
) => Resettable
::Value(s
),
101 None
=> Resettable
::Reset
,
106 impl IntoResettable
<ValueParser
> for Option
<ValueParser
> {
107 fn into_resettable(self) -> Resettable
<ValueParser
> {
109 Some(s
) => Resettable
::Value(s
),
110 None
=> Resettable
::Reset
,
115 impl IntoResettable
<StyledStr
> for Option
<&'
static str> {
116 fn into_resettable(self) -> Resettable
<StyledStr
> {
118 Some(s
) => Resettable
::Value(s
.into()),
119 None
=> Resettable
::Reset
,
124 impl IntoResettable
<OsStr
> for Option
<&'
static str> {
125 fn into_resettable(self) -> Resettable
<OsStr
> {
127 Some(s
) => Resettable
::Value(s
.into()),
128 None
=> Resettable
::Reset
,
133 impl IntoResettable
<Str
> for Option
<&'
static str> {
134 fn into_resettable(self) -> Resettable
<Str
> {
136 Some(s
) => Resettable
::Value(s
.into()),
137 None
=> Resettable
::Reset
,
142 impl<T
> IntoResettable
<T
> for Resettable
<T
> {
143 fn into_resettable(self) -> Resettable
<T
> {
148 impl IntoResettable
<char> for char {
149 fn into_resettable(self) -> Resettable
<char> {
150 Resettable
::Value(self)
154 impl IntoResettable
<usize> for usize {
155 fn into_resettable(self) -> Resettable
<usize> {
156 Resettable
::Value(self)
160 impl IntoResettable
<ArgAction
> for ArgAction
{
161 fn into_resettable(self) -> Resettable
<ArgAction
> {
162 Resettable
::Value(self)
166 impl IntoResettable
<ValueHint
> for ValueHint
{
167 fn into_resettable(self) -> Resettable
<ValueHint
> {
168 Resettable
::Value(self)
172 impl<I
: Into
<ValueRange
>> IntoResettable
<ValueRange
> for I
{
173 fn into_resettable(self) -> Resettable
<ValueRange
> {
174 Resettable
::Value(self.into())
178 impl<I
: Into
<ValueParser
>> IntoResettable
<ValueParser
> for I
{
179 fn into_resettable(self) -> Resettable
<ValueParser
> {
180 Resettable
::Value(self.into())
184 impl<I
: Into
<String
>> IntoResettable
<String
> for I
{
185 fn into_resettable(self) -> Resettable
<String
> {
186 Resettable
::Value(self.into())
190 impl<I
: Into
<StyledStr
>> IntoResettable
<StyledStr
> for I
{
191 fn into_resettable(self) -> Resettable
<StyledStr
> {
192 Resettable
::Value(self.into())
196 impl<I
: Into
<OsStr
>> IntoResettable
<OsStr
> for I
{
197 fn into_resettable(self) -> Resettable
<OsStr
> {
198 Resettable
::Value(self.into())
202 impl<I
: Into
<Str
>> IntoResettable
<Str
> for I
{
203 fn into_resettable(self) -> Resettable
<Str
> {
204 Resettable
::Value(self.into())
208 impl<I
: Into
<crate::Id
>> IntoResettable
<crate::Id
> for I
{
209 fn into_resettable(self) -> Resettable
<crate::Id
> {
210 Resettable
::Value(self.into())