diff --git a/src/config.rs b/src/config.rs index 3356ac28da..e91bbef497 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ use serde::Deserialize; use thiserror::Error as ThisError; use tokio_stream::StreamExt; +use crate::settings::MetadataVersion; use crate::{ cli::self_update::SelfUpdateMode, currentprocess::process, @@ -22,7 +23,7 @@ use crate::{ fallback_settings::FallbackSettings, install::UpdateStatus, notifications::*, - settings::{Settings, SettingsFile, DEFAULT_METADATA_VERSION}, + settings::{Settings, SettingsFile}, toolchain::{ distributable::DistributableToolchain, names::{ @@ -459,20 +460,19 @@ impl Cfg { #[cfg_attr(feature = "otel", tracing::instrument(skip_all))] pub(crate) fn upgrade_data(&self) -> Result<()> { - let current_version = self.settings_file.with(|s| Ok(s.version.clone()))?; - - if current_version == DEFAULT_METADATA_VERSION { - (self.notify_handler)(Notification::MetadataUpgradeNotNeeded(¤t_version)); + let current_version = self.settings_file.with(|s| Ok(s.version))?; + if current_version == MetadataVersion::default() { + (self.notify_handler)(Notification::MetadataUpgradeNotNeeded(current_version)); return Ok(()); } (self.notify_handler)(Notification::UpgradingMetadata( - ¤t_version, - DEFAULT_METADATA_VERSION, + current_version, + MetadataVersion::default(), )); - match &*current_version { - "2" => { + match current_version { + MetadataVersion::V2 => { // The toolchain installation format changed. Just delete them all. (self.notify_handler)(Notification::UpgradeRemovesToolchains); @@ -490,11 +490,11 @@ impl Cfg { } self.settings_file.with_mut(|s| { - DEFAULT_METADATA_VERSION.clone_into(&mut s.version); + s.version = MetadataVersion::default(); Ok(()) }) } - _ => Err(RustupError::UnknownMetadataVersion(current_version).into()), + MetadataVersion::V12 => unreachable!(), } } @@ -878,8 +878,8 @@ impl Cfg { utils::assert_is_directory(&self.rustup_dir)?; self.settings_file.with(|s| { - (self.notify_handler)(Notification::ReadMetadataVersion(&s.version)); - if s.version == DEFAULT_METADATA_VERSION { + (self.notify_handler)(Notification::ReadMetadataVersion(s.version)); + if s.version == MetadataVersion::default() { Ok(()) } else { Err(anyhow!( diff --git a/src/notifications.rs b/src/notifications.rs index a43561064b..1d3fc56d84 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -1,6 +1,7 @@ use std::fmt::{self, Display}; use std::path::{Path, PathBuf}; +use crate::settings::MetadataVersion; use crate::{ dist::{dist::ToolchainDesc, temp}, toolchain::names::ToolchainName, @@ -26,9 +27,9 @@ pub(crate) enum Notification<'a> { UninstallingToolchain(&'a ToolchainName), UninstalledToolchain(&'a ToolchainName), UpdateHashMatches, - UpgradingMetadata(&'a str, &'a str), - MetadataUpgradeNotNeeded(&'a str), - ReadMetadataVersion(&'a str), + UpgradingMetadata(MetadataVersion, MetadataVersion), + MetadataUpgradeNotNeeded(MetadataVersion), + ReadMetadataVersion(MetadataVersion), NonFatalError(&'a anyhow::Error), UpgradeRemovesToolchains, /// Both `rust-toolchain` and `rust-toolchain.toml` exist within a directory diff --git a/src/settings.rs b/src/settings.rs index c887cf1442..77d1864e53 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::collections::BTreeMap; +use std::fmt; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -12,9 +13,6 @@ use crate::notifications::*; use crate::utils::toml_utils::*; use crate::utils::utils; -pub(crate) const SUPPORTED_METADATA_VERSIONS: [&str; 2] = ["2", "12"]; -pub(crate) const DEFAULT_METADATA_VERSION: &str = "12"; - #[derive(Clone, Debug, Eq, PartialEq)] pub struct SettingsFile { path: PathBuf, @@ -78,7 +76,7 @@ impl SettingsFile { #[derive(Clone, Debug, Eq, PartialEq)] pub struct Settings { - pub version: String, + pub version: MetadataVersion, pub default_host_triple: Option, pub default_toolchain: Option, pub profile: Option, @@ -90,7 +88,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - version: DEFAULT_METADATA_VERSION.to_owned(), + version: MetadataVersion::default(), default_host_triple: None, default_toolchain: None, profile: Some(Profile::default()), @@ -152,9 +150,8 @@ impl Settings { pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result { let version = get_string(&mut table, "version", path)?; - if !SUPPORTED_METADATA_VERSIONS.contains(&&*version) { - return Err(RustupError::UnknownMetadataVersion(version).into()); - } + let version = MetadataVersion::from_str(&version)?; + 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)? @@ -172,7 +169,10 @@ impl Settings { pub(crate) fn into_toml(self) -> toml::value::Table { let mut result = toml::value::Table::new(); - result.insert("version".to_owned(), toml::Value::String(self.version)); + result.insert( + "version".to_owned(), + toml::Value::String(self.version.as_str().to_owned()), + ); if let Some(v) = self.default_host_triple { result.insert("default_host_triple".to_owned(), toml::Value::String(v)); @@ -227,3 +227,37 @@ impl Settings { result } } + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub(crate) enum MetadataVersion { + V2, + #[default] + V12, +} + +impl MetadataVersion { + fn as_str(&self) -> &'static str { + match self { + Self::V2 => "2", + Self::V12 => "12", + } + } +} + +impl FromStr for MetadataVersion { + type Err = RustupError; + + fn from_str(s: &str) -> Result { + match s { + "2" => Ok(Self::V2), + "12" => Ok(Self::V12), + _ => Err(RustupError::UnknownMetadataVersion(s.to_owned())), + } + } +} + +impl fmt::Display for MetadataVersion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.as_str()) + } +}