Skip to content

Commit 2d37670

Browse files
authored
Merge pull request #2784 from hi-rustin/rustin-patch-enum
Use profile enum in settings
2 parents ce5817a + c751e36 commit 2d37670

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

src/config.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use thiserror::Error as ThisError;
1414
use crate::cli::self_update::SelfUpdateMode;
1515
use crate::dist::download::DownloadCfg;
1616
use crate::dist::{
17-
dist::{self, valid_profile_names},
17+
dist::{self, Profile},
1818
temp,
1919
};
2020
use crate::errors::RustupError;
@@ -390,20 +390,18 @@ impl Cfg {
390390
}
391391

392392
pub fn set_profile(&mut self, profile: &str) -> Result<()> {
393-
if !dist::Profile::names().contains(&profile) {
394-
return Err(anyhow!(
395-
"unknown profile name: '{}'; valid profile names are {}",
396-
profile.to_owned(),
397-
valid_profile_names(),
398-
));
393+
match Profile::from_str(profile) {
394+
Ok(p) => {
395+
self.profile_override = None;
396+
self.settings_file.with_mut(|s| {
397+
s.profile = Some(p);
398+
Ok(())
399+
})?;
400+
(self.notify_handler)(Notification::SetProfile(profile));
401+
Ok(())
402+
}
403+
Err(err) => Err(err),
399404
}
400-
self.profile_override = None;
401-
self.settings_file.with_mut(|s| {
402-
s.profile = Some(profile.to_owned());
403-
Ok(())
404-
})?;
405-
(self.notify_handler)(Notification::SetProfile(profile));
406-
Ok(())
407405
}
408406

409407
pub fn set_auto_self_update(&mut self, mode: &str) -> Result<()> {
@@ -427,7 +425,7 @@ impl Cfg {
427425
// Returns a profile, if one exists in the settings file.
428426
//
429427
// Returns `Err` if the settings file could not be read or the profile is
430-
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::default())`
428+
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::Default)`
431429
// if there is no profile in the settings file. The last variant happens when
432430
// a user upgrades from a version of Rustup without profiles to a version of
433431
// Rustup with profiles.
@@ -436,11 +434,10 @@ impl Cfg {
436434
return Ok(p);
437435
}
438436
self.settings_file.with(|s| {
439-
let p = match &s.profile {
437+
let p = match s.profile {
440438
Some(p) => p,
441-
None => dist::Profile::default_name(),
439+
None => Profile::Default,
442440
};
443-
let p = dist::Profile::from_str(p)?;
444441
Ok(p)
445442
})
446443
}

src/dist/dist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl FromStr for Profile {
518518
"default" | "d" | "" => Ok(Self::Default),
519519
"complete" | "c" => Ok(Self::Complete),
520520
_ => Err(anyhow!(format!(
521-
"invalid profile name: '{}'; valid names are: {}",
521+
"unknown profile name: '{}'; valid profile names are: {}",
522522
name,
523523
valid_profile_names()
524524
))),

src/settings.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::str::FromStr;
66
use anyhow::{Context, Result};
77

88
use crate::cli::self_update::SelfUpdateMode;
9+
use crate::dist::dist::Profile;
910
use crate::errors::*;
1011
use crate::notifications::*;
1112
use crate::toml_utils::*;
@@ -76,7 +77,7 @@ pub struct Settings {
7677
pub version: String,
7778
pub default_host_triple: Option<String>,
7879
pub default_toolchain: Option<String>,
79-
pub profile: Option<String>,
80+
pub profile: Option<Profile>,
8081
pub overrides: BTreeMap<String, String>,
8182
pub pgp_keys: Option<String>,
8283
pub auto_self_update: Option<SelfUpdateMode>,
@@ -88,7 +89,7 @@ impl Default for Settings {
8889
version: DEFAULT_METADATA_VERSION.to_owned(),
8990
default_host_triple: None,
9091
default_toolchain: None,
91-
profile: Some("default".to_owned()),
92+
profile: Some(Profile::Default),
9293
overrides: BTreeMap::new(),
9394
pgp_keys: None,
9495
auto_self_update: None,
@@ -150,18 +151,15 @@ impl Settings {
150151
if !SUPPORTED_METADATA_VERSIONS.contains(&&*version) {
151152
return Err(RustupError::UnknownMetadataVersion(version).into());
152153
}
153-
let auto_self_update = match get_opt_string(&mut table, "auto_self_update", path)? {
154-
Some(auto_self_update) => match SelfUpdateMode::from_str(auto_self_update.as_str()) {
155-
Ok(mode) => Some(mode),
156-
Err(_) => None,
157-
},
158-
None => None,
159-
};
154+
let auto_self_update = get_opt_string(&mut table, "auto_self_update", path)?
155+
.and_then(|mode| SelfUpdateMode::from_str(mode.as_str()).ok());
156+
let profile = get_opt_string(&mut table, "profile", path)?
157+
.and_then(|p| Profile::from_str(p.as_str()).ok());
160158
Ok(Self {
161159
version,
162160
default_host_triple: get_opt_string(&mut table, "default_host_triple", path)?,
163161
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
164-
profile: get_opt_string(&mut table, "profile", path)?,
162+
profile,
165163
overrides: Self::table_to_overrides(&mut table, path)?,
166164
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
167165
auto_self_update,
@@ -181,7 +179,7 @@ impl Settings {
181179
}
182180

183181
if let Some(v) = self.profile {
184-
result.insert("profile".to_owned(), toml::Value::String(v));
182+
result.insert("profile".to_owned(), toml::Value::String(v.to_string()));
185183
}
186184

187185
if let Some(v) = self.pgp_keys {

0 commit comments

Comments
 (0)