Skip to content

Commit 494cb47

Browse files
committed
build-manifest: Add a macro that makes it impossible to typo -preview, or have a mismatch between parsing and stringifying
1 parent 79e86e3 commit 494cb47

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

src/tools/build-manifest/src/versions.rs

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,48 @@ use tar::Archive;
88

99
const DEFAULT_TARGET: &str = "x86_64-unknown-linux-gnu";
1010

11-
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
12-
pub(crate) enum PkgType {
13-
Rust,
14-
RustSrc,
15-
Rustc,
16-
Cargo,
17-
Rls,
18-
RustAnalyzer,
19-
Clippy,
20-
Rustfmt,
21-
LlvmTools,
22-
Miri,
23-
JsonDocs,
24-
Other(String),
25-
}
26-
27-
impl PkgType {
28-
pub(crate) fn from_component(component: &str) -> Self {
29-
match component {
30-
"rust" => PkgType::Rust,
31-
"rust-src" => PkgType::RustSrc,
32-
"rustc" => PkgType::Rustc,
33-
"cargo" => PkgType::Cargo,
34-
"rls" | "rls-preview" => PkgType::Rls,
35-
"rust-analyzer" | "rust-analyzer-preview" => PkgType::RustAnalyzer,
36-
"clippy" | "clippy-preview" => PkgType::Clippy,
37-
"rustfmt" | "rustfmt-preview" => PkgType::Rustfmt,
38-
"llvm-tools" | "llvm-tools-preview" => PkgType::LlvmTools,
39-
"miri" | "miri-preview" => PkgType::Miri,
40-
"rust-docs-json" | "rust-docs-json-preview" => PkgType::JsonDocs,
41-
other => PkgType::Other(other.into()),
11+
macro_rules! pkg_type {
12+
( $($variant:ident = $component:literal $(; preview = true $(@$is_preview:tt)? )? ),+ $(,)? ) => {
13+
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
14+
pub(crate) enum PkgType {
15+
$($variant,)+
16+
Other(String),
4217
}
43-
}
4418

45-
/// First part of the tarball name.
46-
fn tarball_component_name(&self) -> &str {
47-
match self {
48-
PkgType::Rust => "rust",
49-
PkgType::RustSrc => "rust-src",
50-
PkgType::Rustc => "rustc",
51-
PkgType::Cargo => "cargo",
52-
PkgType::Rls => "rls",
53-
PkgType::RustAnalyzer => "rust-analyzer",
54-
PkgType::Clippy => "clippy",
55-
PkgType::Rustfmt => "rustfmt",
56-
PkgType::LlvmTools => "llvm-tools",
57-
PkgType::Miri => "miri",
58-
PkgType::JsonDocs => "rust-docs-json",
59-
PkgType::Other(component) => component,
19+
impl PkgType {
20+
pub(crate) fn from_component(component: &str) -> Self {
21+
match component {
22+
$( $component $( | concat!($($is_preview)? $component, "-preview") )? => PkgType::$variant,)+
23+
_ => PkgType::Other(component.into()),
24+
}
25+
}
26+
27+
/// First part of the tarball name.
28+
fn tarball_component_name(&self) -> &str {
29+
match self {
30+
$( PkgType::$variant => $component,)+
31+
PkgType::Other(component) => component,
32+
}
33+
}
6034
}
6135
}
36+
}
6237

38+
pkg_type! {
39+
Rust = "rust",
40+
RustSrc = "rust-src",
41+
Rustc = "rustc",
42+
Cargo = "cargo",
43+
Rls = "rls"; preview = true,
44+
RustAnalyzer = "rust-analyzer"; preview = true,
45+
Clippy = "clippy"; preview = true,
46+
Rustfmt = "rustfmt"; preview = true,
47+
LlvmTools = "llvm-tools"; preview = true,
48+
Miri = "miri"; preview = true,
49+
JsonDocs = "rust-docs-json"; preview = true,
50+
}
51+
52+
impl PkgType {
6353
/// Whether this package has the same version as Rust itself, or has its own `version` and
6454
/// `git-commit-hash` files inside the tarball.
6555
fn should_use_rust_version(&self) -> bool {

0 commit comments

Comments
 (0)