Skip to content

Commit 5442ade

Browse files
committed
Represent metadata version as an enum
1 parent 280b662 commit 5442ade

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

src/config.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::Deserialize;
1010
use thiserror::Error as ThisError;
1111
use tokio_stream::StreamExt;
1212

13+
use crate::settings::MetadataVersion;
1314
use crate::{
1415
cli::self_update::SelfUpdateMode,
1516
currentprocess::process,
@@ -22,7 +23,7 @@ use crate::{
2223
fallback_settings::FallbackSettings,
2324
install::UpdateStatus,
2425
notifications::*,
25-
settings::{Settings, SettingsFile, DEFAULT_METADATA_VERSION},
26+
settings::{Settings, SettingsFile},
2627
toolchain::{
2728
distributable::DistributableToolchain,
2829
names::{
@@ -459,20 +460,19 @@ impl Cfg {
459460

460461
#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
461462
pub(crate) fn upgrade_data(&self) -> Result<()> {
462-
let current_version = self.settings_file.with(|s| Ok(s.version.clone()))?;
463-
464-
if current_version == DEFAULT_METADATA_VERSION {
465-
(self.notify_handler)(Notification::MetadataUpgradeNotNeeded(&current_version));
463+
let current_version = self.settings_file.with(|s| Ok(s.version))?;
464+
if current_version == MetadataVersion::default() {
465+
(self.notify_handler)(Notification::MetadataUpgradeNotNeeded(current_version));
466466
return Ok(());
467467
}
468468

469469
(self.notify_handler)(Notification::UpgradingMetadata(
470-
&current_version,
471-
DEFAULT_METADATA_VERSION,
470+
current_version,
471+
MetadataVersion::default(),
472472
));
473473

474-
match &*current_version {
475-
"2" => {
474+
match current_version {
475+
MetadataVersion::V2 => {
476476
// The toolchain installation format changed. Just delete them all.
477477
(self.notify_handler)(Notification::UpgradeRemovesToolchains);
478478

@@ -490,11 +490,11 @@ impl Cfg {
490490
}
491491

492492
self.settings_file.with_mut(|s| {
493-
DEFAULT_METADATA_VERSION.clone_into(&mut s.version);
493+
s.version = MetadataVersion::default();
494494
Ok(())
495495
})
496496
}
497-
_ => Err(RustupError::UnknownMetadataVersion(current_version).into()),
497+
MetadataVersion::V12 => unreachable!(),
498498
}
499499
}
500500

@@ -878,8 +878,8 @@ impl Cfg {
878878
utils::assert_is_directory(&self.rustup_dir)?;
879879

880880
self.settings_file.with(|s| {
881-
(self.notify_handler)(Notification::ReadMetadataVersion(&s.version));
882-
if s.version == DEFAULT_METADATA_VERSION {
881+
(self.notify_handler)(Notification::ReadMetadataVersion(s.version));
882+
if s.version == MetadataVersion::default() {
883883
Ok(())
884884
} else {
885885
Err(anyhow!(

src/notifications.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::{self, Display};
22
use std::path::{Path, PathBuf};
33

4+
use crate::settings::MetadataVersion;
45
use crate::{
56
dist::{dist::ToolchainDesc, temp},
67
toolchain::names::ToolchainName,
@@ -26,9 +27,9 @@ pub(crate) enum Notification<'a> {
2627
UninstallingToolchain(&'a ToolchainName),
2728
UninstalledToolchain(&'a ToolchainName),
2829
UpdateHashMatches,
29-
UpgradingMetadata(&'a str, &'a str),
30-
MetadataUpgradeNotNeeded(&'a str),
31-
ReadMetadataVersion(&'a str),
30+
UpgradingMetadata(MetadataVersion, MetadataVersion),
31+
MetadataUpgradeNotNeeded(MetadataVersion),
32+
ReadMetadataVersion(MetadataVersion),
3233
NonFatalError(&'a anyhow::Error),
3334
UpgradeRemovesToolchains,
3435
/// Both `rust-toolchain` and `rust-toolchain.toml` exist within a directory

src/settings.rs

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3+
use std::fmt;
34
use std::path::{Path, PathBuf};
45
use std::str::FromStr;
56

@@ -12,9 +13,6 @@ use crate::notifications::*;
1213
use crate::utils::toml_utils::*;
1314
use crate::utils::utils;
1415

15-
pub(crate) const SUPPORTED_METADATA_VERSIONS: [&str; 2] = ["2", "12"];
16-
pub(crate) const DEFAULT_METADATA_VERSION: &str = "12";
17-
1816
#[derive(Clone, Debug, Eq, PartialEq)]
1917
pub struct SettingsFile {
2018
path: PathBuf,
@@ -78,7 +76,7 @@ impl SettingsFile {
7876

7977
#[derive(Clone, Debug, Eq, PartialEq)]
8078
pub struct Settings {
81-
pub version: String,
79+
pub version: MetadataVersion,
8280
pub default_host_triple: Option<String>,
8381
pub default_toolchain: Option<String>,
8482
pub profile: Option<Profile>,
@@ -90,7 +88,7 @@ pub struct Settings {
9088
impl Default for Settings {
9189
fn default() -> Self {
9290
Self {
93-
version: DEFAULT_METADATA_VERSION.to_owned(),
91+
version: MetadataVersion::default(),
9492
default_host_triple: None,
9593
default_toolchain: None,
9694
profile: Some(Profile::default()),
@@ -152,9 +150,8 @@ impl Settings {
152150

153151
pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
154152
let version = get_string(&mut table, "version", path)?;
155-
if !SUPPORTED_METADATA_VERSIONS.contains(&&*version) {
156-
return Err(RustupError::UnknownMetadataVersion(version).into());
157-
}
153+
let version = MetadataVersion::from_str(&version)?;
154+
158155
let auto_self_update = get_opt_string(&mut table, "auto_self_update", path)?
159156
.and_then(|mode| SelfUpdateMode::from_str(mode.as_str()).ok());
160157
let profile = get_opt_string(&mut table, "profile", path)?
@@ -172,7 +169,10 @@ impl Settings {
172169
pub(crate) fn into_toml(self) -> toml::value::Table {
173170
let mut result = toml::value::Table::new();
174171

175-
result.insert("version".to_owned(), toml::Value::String(self.version));
172+
result.insert(
173+
"version".to_owned(),
174+
toml::Value::String(self.version.as_str().to_owned()),
175+
);
176176

177177
if let Some(v) = self.default_host_triple {
178178
result.insert("default_host_triple".to_owned(), toml::Value::String(v));
@@ -227,3 +227,37 @@ impl Settings {
227227
result
228228
}
229229
}
230+
231+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
232+
pub(crate) enum MetadataVersion {
233+
V2,
234+
#[default]
235+
V12,
236+
}
237+
238+
impl MetadataVersion {
239+
fn as_str(&self) -> &'static str {
240+
match self {
241+
Self::V2 => "2",
242+
Self::V12 => "12",
243+
}
244+
}
245+
}
246+
247+
impl FromStr for MetadataVersion {
248+
type Err = RustupError;
249+
250+
fn from_str(s: &str) -> Result<Self, Self::Err> {
251+
match s {
252+
"2" => Ok(Self::V2),
253+
"12" => Ok(Self::V12),
254+
_ => Err(RustupError::UnknownMetadataVersion(s.to_owned())),
255+
}
256+
}
257+
}
258+
259+
impl fmt::Display for MetadataVersion {
260+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261+
write!(f, "{}", self.as_str())
262+
}
263+
}

0 commit comments

Comments
 (0)