Skip to content

Commit

Permalink
Represent metadata version as an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jun 9, 2024
1 parent 280b662 commit 5442ade
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
26 changes: 13 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::{
Expand Down Expand Up @@ -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(&current_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(
&current_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);

Expand All @@ -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!(),
}
}

Expand Down Expand Up @@ -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!(
Expand Down
7 changes: 4 additions & 3 deletions src/notifications.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
52 changes: 43 additions & 9 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;

Expand All @@ -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,
Expand Down Expand Up @@ -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<String>,
pub default_toolchain: Option<String>,
pub profile: Option<Profile>,
Expand All @@ -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()),
Expand Down Expand Up @@ -152,9 +150,8 @@ impl Settings {

pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
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)?
Expand All @@ -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));
Expand Down Expand Up @@ -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<Self, Self::Err> {
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())
}
}

0 comments on commit 5442ade

Please sign in to comment.