Skip to content

Commit 76b2108

Browse files
committed
Use serde to encode/decode mock manifests
1 parent 57ba98f commit 76b2108

File tree

4 files changed

+88
-100
lines changed

4 files changed

+88
-100
lines changed

src/dist/manifest.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) struct ComponentStatus {
3737
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
3838
#[serde(rename_all = "kebab-case")]
3939
pub struct Manifest {
40-
manifest_version: ManifestVersion,
40+
pub(crate) manifest_version: ManifestVersion,
4141
pub date: String,
4242
#[serde(default, rename = "pkg")]
4343
pub packages: HashMap<String, Package>,
@@ -46,12 +46,12 @@ pub struct Manifest {
4646
#[serde(default, skip_serializing)]
4747
pub reverse_renames: HashMap<String, String>,
4848
#[serde(default)]
49-
profiles: HashMap<Profile, Vec<String>>,
49+
pub profiles: HashMap<Profile, Vec<String>>,
5050
}
5151

5252
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
5353
pub struct Renamed {
54-
to: String,
54+
pub to: String,
5555
}
5656

5757
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
@@ -228,7 +228,7 @@ pub struct Component {
228228
// Older Rustup distinguished between components (which are essential) and
229229
// extensions (which are not).
230230
#[serde(default)]
231-
is_extension: bool,
231+
pub is_extension: bool,
232232
}
233233

234234
impl PartialEq for Component {

src/dist/manifestation/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn mock_dist_server_smoke_test() {
250250
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
251251
let path = tempdir.path();
252252

253-
create_mock_dist_server(path, None).write(&[ManifestVersion::V2], false, false);
253+
create_mock_dist_server(path, None).write(&[MockManifestVersion::V2], false, false);
254254

255255
assert!(utils::path_exists(path.join(
256256
"dist/2016-02-01/rustc-nightly-x86_64-apple-darwin.tar.gz"
@@ -552,7 +552,7 @@ fn setup_from_dist_server(
552552
f: &dyn Fn(&Url, &ToolchainDesc, &InstallPrefix, &DownloadCfg<'_>, &temp::Context),
553553
) {
554554
server.write(
555-
&[ManifestVersion::V2],
555+
&[MockManifestVersion::V2],
556556
comps.enable_xz(),
557557
comps.enable_zst(),
558558
);

src/test/mock/clitools.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::utils::{raw, utils};
2828

2929
use super::{
3030
dist::{
31-
change_channel_date, ManifestVersion, MockChannel, MockComponent, MockDistServer,
31+
change_channel_date, MockChannel, MockComponent, MockDistServer, MockManifestVersion,
3232
MockPackage, MockTargetedPackage,
3333
},
3434
topical_doc_data, MockComponentBuilder, MockFile, MockInstallerBuilder,
@@ -1134,8 +1134,8 @@ fn create_mock_dist_server(path: &Path, s: Scenario) {
11341134
let vs = match s {
11351135
Scenario::None => unreachable!("None exits above"),
11361136
Scenario::Empty => vec![],
1137-
Scenario::Full => vec![ManifestVersion::V1, ManifestVersion::V2],
1138-
Scenario::SimpleV1 | Scenario::ArchivesV1 => vec![ManifestVersion::V1],
1137+
Scenario::Full => vec![MockManifestVersion::V1, MockManifestVersion::V2],
1138+
Scenario::SimpleV1 | Scenario::ArchivesV1 => vec![MockManifestVersion::V1],
11391139
Scenario::SimpleV2
11401140
| Scenario::ArchivesV2
11411141
| Scenario::ArchivesV2_2015_01_01
@@ -1148,7 +1148,7 @@ fn create_mock_dist_server(path: &Path, s: Scenario) {
11481148
| Scenario::HostGoesMissingBefore
11491149
| Scenario::HostGoesMissingAfter
11501150
| Scenario::MissingComponent
1151-
| Scenario::MissingComponentMulti => vec![ManifestVersion::V2],
1151+
| Scenario::MissingComponentMulti => vec![MockManifestVersion::V2],
11521152
};
11531153

11541154
MockDistServer {

src/test/mock/dist.rs

Lines changed: 78 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ use once_cell::sync::Lazy;
1111
use sha2::{Digest, Sha256};
1212
use url::Url;
1313

14+
use crate::dist::dist::{Profile, TargetTriple};
15+
use crate::dist::manifest::{
16+
Component, CompressionKind, HashedBinary, Manifest, ManifestVersion, Package, PackageTargets,
17+
Renamed, TargetedPackage,
18+
};
19+
1420
use super::clitools::hard_link;
1521
use super::MockInstallerBuilder;
1622

@@ -118,14 +124,14 @@ pub struct MockHashes {
118124
pub zst: Option<String>,
119125
}
120126

121-
pub enum ManifestVersion {
127+
pub enum MockManifestVersion {
122128
V1,
123129
V2,
124130
}
125131

126132
impl MockDistServer {
127133
#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
128-
pub fn write(&self, vs: &[ManifestVersion], enable_xz: bool, enable_zst: bool) {
134+
pub fn write(&self, vs: &[MockManifestVersion], enable_xz: bool, enable_zst: bool) {
129135
fs::create_dir_all(&self.path).unwrap();
130136

131137
for channel in self.channels.iter() {
@@ -136,8 +142,8 @@ impl MockDistServer {
136142
}
137143
for v in vs {
138144
match *v {
139-
ManifestVersion::V1 => self.write_manifest_v1(channel),
140-
ManifestVersion::V2 => self.write_manifest_v2(channel, &hashes),
145+
MockManifestVersion::V1 => self.write_manifest_v1(channel),
146+
MockManifestVersion::V2 => self.write_manifest_v2(channel, &hashes),
141147
}
142148
}
143149
}
@@ -306,136 +312,118 @@ impl MockDistServer {
306312
channel: &MockChannel,
307313
hashes: &HashMap<MockComponent, MockHashes>,
308314
) {
309-
let mut toml_manifest = toml::value::Table::new();
310-
311-
toml_manifest.insert(
312-
String::from("manifest-version"),
313-
toml::Value::String(MOCK_MANIFEST_VERSION.to_owned()),
314-
);
315-
toml_manifest.insert(
316-
String::from("date"),
317-
toml::Value::String(channel.date.to_owned()),
318-
);
315+
let mut manifest = Manifest {
316+
manifest_version: ManifestVersion::V2,
317+
date: channel.date.clone(),
318+
renames: HashMap::default(),
319+
packages: HashMap::default(),
320+
reverse_renames: HashMap::default(),
321+
profiles: HashMap::default(),
322+
};
319323

320324
// [pkg.*]
321-
let mut toml_packages = toml::value::Table::new();
322325
for package in &channel.packages {
323-
let mut toml_package = toml::value::Table::new();
324-
toml_package.insert(
325-
String::from("version"),
326-
toml::Value::String(package.version.to_owned()),
327-
);
326+
let mut targets = HashMap::default();
328327

329328
// [pkg.*.target.*]
330-
let mut toml_targets = toml::value::Table::new();
331329
for target in &package.targets {
332-
let mut toml_target = toml::value::Table::new();
333-
toml_target.insert(
334-
String::from("available"),
335-
toml::Value::Boolean(target.available),
336-
);
330+
let mut tpkg = TargetedPackage {
331+
bins: Vec::new(),
332+
components: Vec::new(),
333+
};
337334

338335
let package_file_name = if target.target != "*" {
339336
format!("{}-{}-{}.tar.gz", package.name, channel.name, target.target)
340337
} else {
341338
format!("{}-{}.tar.gz", package.name, channel.name)
342339
};
340+
343341
let path = self
344342
.path
345343
.join("dist")
346344
.join(&channel.date)
347345
.join(package_file_name);
348-
let url = format!("file://{}", path.to_string_lossy());
349-
toml_target.insert(String::from("url"), toml::Value::String(url.clone()));
350346

351347
let component = MockComponent {
352348
name: package.name.to_owned(),
353349
target: target.target.to_owned(),
354350
is_extension: false,
355351
};
356-
let hash = hashes[&component].clone();
357-
toml_target.insert(String::from("hash"), toml::Value::String(hash.gz));
358-
359-
if let Some(xz_hash) = hash.xz {
360-
toml_target.insert(
361-
String::from("xz_url"),
362-
toml::Value::String(url.replace(".tar.gz", ".tar.xz")),
363-
);
364-
toml_target.insert(String::from("xz_hash"), toml::Value::String(xz_hash));
365-
}
366-
if let Some(zst_hash) = hash.zst {
367-
toml_target.insert(
368-
String::from("zst_url"),
369-
toml::Value::String(url.replace(".tar.gz", ".tar.zst")),
370-
);
371-
toml_target.insert(String::from("zst_hash"), toml::Value::String(zst_hash));
352+
353+
if target.available {
354+
let hash = hashes[&component].clone();
355+
let url = format!("file://{}", path.to_string_lossy());
356+
tpkg.bins.push(HashedBinary {
357+
url: url.clone(),
358+
hash: hash.gz,
359+
compression: CompressionKind::GZip,
360+
});
361+
362+
if let Some(xz_hash) = hash.xz {
363+
tpkg.bins.push(HashedBinary {
364+
url: url.replace(".tar.gz", ".tar.xz"),
365+
hash: xz_hash,
366+
compression: CompressionKind::XZ,
367+
});
368+
}
369+
370+
if let Some(zst_hash) = hash.zst {
371+
tpkg.bins.push(HashedBinary {
372+
url: url.replace(".tar.gz", ".tar.zst"),
373+
hash: zst_hash,
374+
compression: CompressionKind::ZStd,
375+
});
376+
}
372377
}
373378

374379
// [pkg.*.target.*.components.*] and [pkg.*.target.*.extensions.*]
375-
let mut toml_components = toml::value::Array::new();
376-
let mut toml_extensions = toml::value::Array::new();
377380
for component in &target.components {
378-
let mut toml_component = toml::value::Table::new();
379-
toml_component.insert(
380-
String::from("pkg"),
381-
toml::Value::String(component.name.to_owned()),
382-
);
383-
toml_component.insert(
384-
String::from("target"),
385-
toml::Value::String(component.target.to_owned()),
386-
);
387-
if component.is_extension {
388-
toml_extensions.push(toml::Value::Table(toml_component));
389-
} else {
390-
toml_components.push(toml::Value::Table(toml_component));
391-
}
381+
tpkg.components.push(Component {
382+
pkg: component.name.to_owned(),
383+
target: Some(TargetTriple::new(&component.target)),
384+
is_extension: component.is_extension,
385+
});
392386
}
393-
toml_target.insert(
394-
String::from("components"),
395-
toml::Value::Array(toml_components),
396-
);
397-
toml_target.insert(
398-
String::from("extensions"),
399-
toml::Value::Array(toml_extensions),
400-
);
401-
402-
toml_targets.insert(target.target.clone(), toml::Value::Table(toml_target));
387+
388+
targets.insert(TargetTriple::new(&target.target), tpkg);
403389
}
404-
toml_package.insert(String::from("target"), toml::Value::Table(toml_targets));
405390

406-
toml_packages.insert(String::from(package.name), toml::Value::Table(toml_package));
391+
manifest.packages.insert(
392+
package.name.to_owned(),
393+
Package {
394+
version: package.version.clone(),
395+
targets: PackageTargets::Targeted(targets),
396+
},
397+
);
407398
}
408-
toml_manifest.insert(String::from("pkg"), toml::Value::Table(toml_packages));
409399

410-
let mut toml_renames = toml::value::Table::new();
411400
for (from, to) in &channel.renames {
412-
let mut toml_rename = toml::value::Table::new();
413-
toml_rename.insert(String::from("to"), toml::Value::String(to.to_owned()));
414-
toml_renames.insert(from.to_owned(), toml::Value::Table(toml_rename));
401+
manifest
402+
.renames
403+
.insert(from.to_owned(), Renamed { to: to.to_owned() });
415404
}
416-
toml_manifest.insert(String::from("renames"), toml::Value::Table(toml_renames));
417405

418-
let mut toml_profiles = toml::value::Table::new();
419406
let profiles = &[
420-
("minimal", vec!["rustc"]),
421-
("default", vec!["rustc", "cargo", "rust-std", "rust-docs"]),
407+
(Profile::Minimal, &["rustc"][..]),
422408
(
423-
"complete",
424-
vec!["rustc", "cargo", "rust-std", "rust-docs", "rls"],
409+
Profile::Default,
410+
&["rustc", "cargo", "rust-std", "rust-docs"],
411+
),
412+
(
413+
Profile::Complete,
414+
&["rustc", "cargo", "rust-std", "rust-docs", "rls"],
425415
),
426416
];
417+
427418
for (profile, values) in profiles {
428-
let array = values
429-
.iter()
430-
.map(|v| toml::Value::String((**v).to_owned()))
431-
.collect();
432-
toml_profiles.insert((*profile).to_string(), toml::Value::Array(array));
419+
manifest
420+
.profiles
421+
.insert(*profile, values.iter().map(|&v| v.to_owned()).collect());
433422
}
434-
toml_manifest.insert(String::from("profiles"), toml::Value::Table(toml_profiles));
435423

436424
let manifest_name = format!("dist/channel-rust-{}", channel.name);
437425
let manifest_path = self.path.join(format!("{manifest_name}.toml"));
438-
let manifest_content = toml::to_string(&toml_manifest).unwrap();
426+
let manifest_content = manifest.stringify().unwrap();
439427
write_file(&manifest_path, &manifest_content);
440428

441429
let hash_path = self.path.join(format!("{manifest_name}.toml.sha256"));

0 commit comments

Comments
 (0)