Skip to content

Commit b46252a

Browse files
committed
Represent manifest version as enum
1 parent e598bd5 commit b46252a

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/dist/manifest.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Docs: <https://forge.rust-lang.org/infra/channel-layout.html>
1414
1515
use std::collections::HashMap;
16+
use std::fmt;
1617
use std::hash::{Hash, Hasher};
1718
use std::str::FromStr;
1819

@@ -25,8 +26,6 @@ use crate::utils::toml_utils::*;
2526

2627
use super::{config::Config, dist::ToolchainDesc};
2728

28-
pub(crate) const SUPPORTED_MANIFEST_VERSIONS: [&str; 1] = ["2"];
29-
3029
/// Used by the `installed_components` function
3130
pub(crate) struct ComponentStatus {
3231
pub component: Component,
@@ -37,7 +36,7 @@ pub(crate) struct ComponentStatus {
3736

3837
#[derive(Clone, Debug, Eq, PartialEq)]
3938
pub struct Manifest {
40-
manifest_version: String,
39+
manifest_version: ManifestVersion,
4140
pub date: String,
4241
pub packages: HashMap<String, Package>,
4342
pub renames: HashMap<String, String>,
@@ -133,12 +132,11 @@ impl Manifest {
133132

134133
pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
135134
let version = get_string(&mut table, "manifest-version", path)?;
136-
if !SUPPORTED_MANIFEST_VERSIONS.contains(&&*version) {
137-
bail!(RustupError::UnsupportedVersion(version));
138-
}
135+
let manifest_version = ManifestVersion::from_str(&version)?;
136+
139137
let (renames, reverse_renames) = Self::table_to_renames(&mut table, path)?;
140138
Ok(Self {
141-
manifest_version: version,
139+
manifest_version,
142140
date: get_string(&mut table, "date", path)?,
143141
packages: Self::table_to_packages(&mut table, path)?,
144142
renames,
@@ -152,7 +150,7 @@ impl Manifest {
152150
result.insert("date".to_owned(), toml::Value::String(self.date));
153151
result.insert(
154152
"manifest-version".to_owned(),
155-
toml::Value::String(self.manifest_version),
153+
toml::Value::String(self.manifest_version.to_string()),
156154
);
157155

158156
let renames = Self::renames_to_table(self.renames);
@@ -719,6 +717,37 @@ impl Component {
719717
}
720718
}
721719

720+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
721+
pub(crate) enum ManifestVersion {
722+
#[default]
723+
V2,
724+
}
725+
726+
impl ManifestVersion {
727+
pub fn as_str(&self) -> &'static str {
728+
match self {
729+
Self::V2 => "2",
730+
}
731+
}
732+
}
733+
734+
impl FromStr for ManifestVersion {
735+
type Err = RustupError;
736+
737+
fn from_str(s: &str) -> Result<Self, Self::Err> {
738+
match s {
739+
"2" => Ok(Self::V2),
740+
_ => Err(RustupError::UnsupportedVersion(s.to_owned())),
741+
}
742+
}
743+
}
744+
745+
impl fmt::Display for ManifestVersion {
746+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
747+
write!(f, "{}", self.as_str())
748+
}
749+
}
750+
722751
#[cfg(test)]
723752
mod tests {
724753
use crate::dist::dist::TargetTriple;

0 commit comments

Comments
 (0)