Skip to content

Commit

Permalink
Merge pull request #2784 from hi-rustin/rustin-patch-enum
Browse files Browse the repository at this point in the history
Use profile enum in settings
  • Loading branch information
kinnison authored Jun 8, 2021
2 parents ce5817a + c751e36 commit 2d37670
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
33 changes: 15 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use thiserror::Error as ThisError;
use crate::cli::self_update::SelfUpdateMode;
use crate::dist::download::DownloadCfg;
use crate::dist::{
dist::{self, valid_profile_names},
dist::{self, Profile},
temp,
};
use crate::errors::RustupError;
Expand Down Expand Up @@ -390,20 +390,18 @@ impl Cfg {
}

pub fn set_profile(&mut self, profile: &str) -> Result<()> {
if !dist::Profile::names().contains(&profile) {
return Err(anyhow!(
"unknown profile name: '{}'; valid profile names are {}",
profile.to_owned(),
valid_profile_names(),
));
match Profile::from_str(profile) {
Ok(p) => {
self.profile_override = None;
self.settings_file.with_mut(|s| {
s.profile = Some(p);
Ok(())
})?;
(self.notify_handler)(Notification::SetProfile(profile));
Ok(())
}
Err(err) => Err(err),
}
self.profile_override = None;
self.settings_file.with_mut(|s| {
s.profile = Some(profile.to_owned());
Ok(())
})?;
(self.notify_handler)(Notification::SetProfile(profile));
Ok(())
}

pub fn set_auto_self_update(&mut self, mode: &str) -> Result<()> {
Expand All @@ -427,7 +425,7 @@ impl Cfg {
// Returns a profile, if one exists in the settings file.
//
// Returns `Err` if the settings file could not be read or the profile is
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::default())`
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::Default)`
// if there is no profile in the settings file. The last variant happens when
// a user upgrades from a version of Rustup without profiles to a version of
// Rustup with profiles.
Expand All @@ -436,11 +434,10 @@ impl Cfg {
return Ok(p);
}
self.settings_file.with(|s| {
let p = match &s.profile {
let p = match s.profile {
Some(p) => p,
None => dist::Profile::default_name(),
None => Profile::Default,
};
let p = dist::Profile::from_str(p)?;
Ok(p)
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl FromStr for Profile {
"default" | "d" | "" => Ok(Self::Default),
"complete" | "c" => Ok(Self::Complete),
_ => Err(anyhow!(format!(
"invalid profile name: '{}'; valid names are: {}",
"unknown profile name: '{}'; valid profile names are: {}",
name,
valid_profile_names()
))),
Expand Down
20 changes: 9 additions & 11 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::str::FromStr;
use anyhow::{Context, Result};

use crate::cli::self_update::SelfUpdateMode;
use crate::dist::dist::Profile;
use crate::errors::*;
use crate::notifications::*;
use crate::toml_utils::*;
Expand Down Expand Up @@ -76,7 +77,7 @@ pub struct Settings {
pub version: String,
pub default_host_triple: Option<String>,
pub default_toolchain: Option<String>,
pub profile: Option<String>,
pub profile: Option<Profile>,
pub overrides: BTreeMap<String, String>,
pub pgp_keys: Option<String>,
pub auto_self_update: Option<SelfUpdateMode>,
Expand All @@ -88,7 +89,7 @@ impl Default for Settings {
version: DEFAULT_METADATA_VERSION.to_owned(),
default_host_triple: None,
default_toolchain: None,
profile: Some("default".to_owned()),
profile: Some(Profile::Default),
overrides: BTreeMap::new(),
pgp_keys: None,
auto_self_update: None,
Expand Down Expand Up @@ -150,18 +151,15 @@ impl Settings {
if !SUPPORTED_METADATA_VERSIONS.contains(&&*version) {
return Err(RustupError::UnknownMetadataVersion(version).into());
}
let auto_self_update = match get_opt_string(&mut table, "auto_self_update", path)? {
Some(auto_self_update) => match SelfUpdateMode::from_str(auto_self_update.as_str()) {
Ok(mode) => Some(mode),
Err(_) => None,
},
None => None,
};
let auto_self_update = get_opt_string(&mut table, "auto_self_update", path)?
.and_then(|mode| SelfUpdateMode::from_str(mode.as_str()).ok());
let profile = get_opt_string(&mut table, "profile", path)?
.and_then(|p| Profile::from_str(p.as_str()).ok());
Ok(Self {
version,
default_host_triple: get_opt_string(&mut table, "default_host_triple", path)?,
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
profile: get_opt_string(&mut table, "profile", path)?,
profile,
overrides: Self::table_to_overrides(&mut table, path)?,
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
auto_self_update,
Expand All @@ -181,7 +179,7 @@ impl Settings {
}

if let Some(v) = self.profile {
result.insert("profile".to_owned(), toml::Value::String(v));
result.insert("profile".to_owned(), toml::Value::String(v.to_string()));
}

if let Some(v) = self.pgp_keys {
Expand Down

0 comments on commit 2d37670

Please sign in to comment.