Skip to content

Commit 04ea1d4

Browse files
committed
Let argument parser handle SelfUpdateMode conversion
1 parent 36fcd37 commit 04ea1d4

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

src/cli/rustup_mode.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::process::ExitStatus;
66

77
use anyhow::{anyhow, Error, Result};
8-
use clap::{
9-
builder::{PossibleValue, PossibleValuesParser},
10-
Args, CommandFactory, Parser, Subcommand, ValueEnum,
11-
};
8+
use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, ValueEnum};
129
use clap_complete::Shell;
1310
use itertools::Itertools;
1411

@@ -523,11 +520,8 @@ enum SetSubcmd {
523520

524521
/// The rustup auto self update mode
525522
AutoSelfUpdate {
526-
#[arg(
527-
default_value = SelfUpdateMode::default_mode(),
528-
value_parser = PossibleValuesParser::new(SelfUpdateMode::modes()),
529-
)]
530-
auto_self_update_mode: String,
523+
#[arg(value_enum, default_value_t)]
524+
auto_self_update_mode: SelfUpdateMode,
531525
},
532526
}
533527

@@ -710,7 +704,7 @@ pub async fn main(current_dir: PathBuf) -> Result<utils::ExitCode> {
710704
}
711705
SetSubcmd::AutoSelfUpdate {
712706
auto_self_update_mode,
713-
} => set_auto_self_update(cfg, &auto_self_update_mode),
707+
} => set_auto_self_update(cfg, auto_self_update_mode),
714708
},
715709
RustupSubcmd::Completions { shell, command } => output_completion_script(shell, command),
716710
}
@@ -1510,7 +1504,10 @@ async fn man(
15101504
Ok(utils::ExitCode(0))
15111505
}
15121506

1513-
fn set_auto_self_update(cfg: &mut Cfg, auto_self_update_mode: &str) -> Result<utils::ExitCode> {
1507+
fn set_auto_self_update(
1508+
cfg: &mut Cfg,
1509+
auto_self_update_mode: SelfUpdateMode,
1510+
) -> Result<utils::ExitCode> {
15141511
if self_update::NEVER_SELF_UPDATE {
15151512
let process = process();
15161513
let mut args = process.args_os();

src/cli/self_update.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use std::{env, fmt};
5656

5757
use anyhow::{anyhow, Context, Result};
5858
use cfg_if::cfg_if;
59+
use clap::builder::PossibleValue;
5960
use clap::ValueEnum;
6061
use itertools::Itertools;
6162
use same_file::Handle;
@@ -243,21 +244,36 @@ pub(crate) const NEVER_SELF_UPDATE: bool = true;
243244
#[cfg(not(feature = "no-self-update"))]
244245
pub(crate) const NEVER_SELF_UPDATE: bool = false;
245246

246-
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
247+
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
247248
#[serde(rename_all = "kebab-case")]
248249
pub enum SelfUpdateMode {
250+
#[default]
249251
Enable,
250252
Disable,
251253
CheckOnly,
252254
}
253255

254256
impl SelfUpdateMode {
255-
pub(crate) fn modes() -> &'static [&'static str] {
256-
&["enable", "disable", "check-only"]
257+
pub(crate) fn as_str(&self) -> &'static str {
258+
match self {
259+
Self::Enable => "enable",
260+
Self::Disable => "disable",
261+
Self::CheckOnly => "check-only",
262+
}
263+
}
264+
}
265+
266+
impl ValueEnum for SelfUpdateMode {
267+
fn value_variants<'a>() -> &'a [Self] {
268+
&[Self::Enable, Self::Disable, Self::CheckOnly]
257269
}
258270

259-
pub(crate) fn default_mode() -> &'static str {
260-
"enable"
271+
fn to_possible_value(&self) -> Option<PossibleValue> {
272+
Some(PossibleValue::new(self.as_str()))
273+
}
274+
275+
fn from_str(input: &str, _: bool) -> Result<Self, String> {
276+
<Self as FromStr>::from_str(input).map_err(|e| e.to_string())
261277
}
262278
}
263279

@@ -272,19 +288,15 @@ impl FromStr for SelfUpdateMode {
272288
_ => Err(anyhow!(format!(
273289
"unknown self update mode: '{}'; valid modes are {}",
274290
mode,
275-
Self::modes().join(", "),
291+
Self::value_variants().iter().join(", ")
276292
))),
277293
}
278294
}
279295
}
280296

281297
impl std::fmt::Display for SelfUpdateMode {
282298
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
283-
f.write_str(match self {
284-
SelfUpdateMode::Enable => "enable",
285-
SelfUpdateMode::Disable => "disable",
286-
SelfUpdateMode::CheckOnly => "check-only",
287-
})
299+
f.write_str(self.as_str())
288300
}
289301
}
290302

src/config.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,13 @@ impl Cfg {
380380
Ok(())
381381
}
382382

383-
pub(crate) fn set_auto_self_update(&mut self, mode: &str) -> Result<()> {
384-
match SelfUpdateMode::from_str(mode) {
385-
Ok(update_mode) => {
386-
self.settings_file.with_mut(|s| {
387-
s.auto_self_update = Some(update_mode);
388-
Ok(())
389-
})?;
390-
(self.notify_handler)(Notification::SetSelfUpdate(mode));
391-
Ok(())
392-
}
393-
Err(err) => Err(err),
394-
}
383+
pub(crate) fn set_auto_self_update(&mut self, mode: SelfUpdateMode) -> Result<()> {
384+
self.settings_file.with_mut(|s| {
385+
s.auto_self_update = Some(mode);
386+
Ok(())
387+
})?;
388+
(self.notify_handler)(Notification::SetSelfUpdate(mode.as_str()));
389+
Ok(())
395390
}
396391

397392
pub(crate) fn set_toolchain_override(&mut self, toolchain_override: &ResolvableToolchainName) {
@@ -415,11 +410,10 @@ impl Cfg {
415410

416411
pub(crate) fn get_self_update_mode(&self) -> Result<SelfUpdateMode> {
417412
self.settings_file.with(|s| {
418-
let mode = match &s.auto_self_update {
419-
Some(mode) => mode.clone(),
413+
Ok(match s.auto_self_update {
414+
Some(mode) => mode,
420415
None => SelfUpdateMode::Enable,
421-
};
422-
Ok(mode)
416+
})
423417
})
424418
}
425419

0 commit comments

Comments
 (0)