Skip to content

Commit 438ba95

Browse files
committed
Move Component name helpers to Manifest
1 parent 6839030 commit 438ba95

File tree

6 files changed

+68
-63
lines changed

6 files changed

+68
-63
lines changed

src/dist/manifest.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ pub struct Manifest {
5050
pub profiles: HashMap<Profile, Vec<String>>,
5151
}
5252

53+
impl Manifest {
54+
pub(crate) fn name(&self, component: &Component) -> String {
55+
let pkg = self.short_name(component);
56+
if let Some(t) = &component.target {
57+
format!("{pkg}-{t}")
58+
} else {
59+
pkg
60+
}
61+
}
62+
63+
pub(crate) fn description(&self, component: &Component) -> String {
64+
let pkg = self.short_name(component);
65+
if let Some(t) = &component.target {
66+
format!("'{pkg}' for target '{t}'")
67+
} else {
68+
format!("'{pkg}'")
69+
}
70+
}
71+
72+
pub(crate) fn short_name(&self, component: &Component) -> String {
73+
if let Some(from) = self.reverse_renames.get(&component.pkg) {
74+
from.to_owned()
75+
} else {
76+
component.pkg.clone()
77+
}
78+
}
79+
}
80+
5381
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
5482
pub struct Renamed {
5583
pub to: String,
@@ -354,10 +382,10 @@ impl Manifest {
354382
for c in tpkg.components.iter() {
355383
let cpkg = self
356384
.get_package(&c.pkg)
357-
.with_context(|| RustupError::MissingPackageForComponent(c.short_name(self)))?;
385+
.with_context(|| RustupError::MissingPackageForComponent(self.short_name(c)))?;
358386
let _ctpkg = cpkg
359387
.get_target(c.target.as_ref())
360-
.with_context(|| RustupError::MissingPackageForComponent(c.short_name(self)))?;
388+
.with_context(|| RustupError::MissingPackageForComponent(self.short_name(c)))?;
361389
}
362390
Ok(())
363391
}
@@ -431,7 +459,7 @@ impl Manifest {
431459
.unwrap_or_else(|_| {
432460
panic!(
433461
"manifest should contain component {}",
434-
&component.short_name(self)
462+
&self.short_name(component)
435463
)
436464
});
437465
let component_target_pkg = component_pkg
@@ -441,7 +469,7 @@ impl Manifest {
441469

442470
res.push(ComponentStatus {
443471
component: component.clone(),
444-
name: component.name(self),
472+
name: self.name(component),
445473
installed,
446474
available: component_target_pkg.available(),
447475
});
@@ -509,7 +537,7 @@ impl Component {
509537
let manifest = distributable.get_manifest()?;
510538
for component_status in distributable.components()? {
511539
let component = component_status.component;
512-
if name == component.name_in_manifest() || name == component.name(&manifest) {
540+
if name == component.name_in_manifest() || name == manifest.name(&component) {
513541
return Ok(component);
514542
}
515543
}
@@ -529,29 +557,6 @@ impl Component {
529557
}
530558
}
531559

532-
pub(crate) fn name(&self, manifest: &Manifest) -> String {
533-
let pkg = self.short_name(manifest);
534-
if let Some(t) = &self.target {
535-
format!("{pkg}-{t}")
536-
} else {
537-
pkg
538-
}
539-
}
540-
pub(crate) fn short_name(&self, manifest: &Manifest) -> String {
541-
if let Some(from) = manifest.reverse_renames.get(&self.pkg) {
542-
from.to_owned()
543-
} else {
544-
self.pkg.clone()
545-
}
546-
}
547-
pub(crate) fn description(&self, manifest: &Manifest) -> String {
548-
let pkg = self.short_name(manifest);
549-
if let Some(t) = &self.target {
550-
format!("'{pkg}' for target '{t}'")
551-
} else {
552-
format!("'{pkg}'")
553-
}
554-
}
555560
pub fn short_name_in_manifest(&self) -> &String {
556561
&self.pkg
557562
}

src/dist/manifestation.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ impl Manifestation {
135135
match &component.target {
136136
Some(t) if t != &self.target_triple => warn!(
137137
"skipping unavailable component {} for target {}",
138-
component.short_name(&new_manifest),
138+
new_manifest.short_name(component),
139139
t
140140
),
141141
_ => warn!(
142142
"skipping unavailable component {}",
143-
component.short_name(&new_manifest)
143+
new_manifest.short_name(component)
144144
),
145145
}
146146
}
@@ -182,25 +182,25 @@ impl Manifestation {
182182
(true, Some(t)) if t != &self.target_triple => {
183183
info!(
184184
"removing previous version of component {} for target {}",
185-
component.short_name(&new_manifest),
185+
new_manifest.short_name(&component),
186186
t
187187
);
188188
}
189189
(false, Some(t)) if t != &self.target_triple => {
190190
info!(
191191
"removing component {} for target {}",
192-
component.short_name(&new_manifest),
192+
new_manifest.short_name(&component),
193193
t
194194
);
195195
}
196196
(true, _) => {
197197
info!(
198198
"removing previous version of component {}",
199-
component.short_name(&new_manifest),
199+
new_manifest.short_name(&component),
200200
);
201201
}
202202
(false, _) => {
203-
info!("removing component {}", component.short_name(&new_manifest));
203+
info!("removing component {}", new_manifest.short_name(&component));
204204
}
205205
}
206206

@@ -306,7 +306,7 @@ impl Manifestation {
306306
} else {
307307
warn!(
308308
"component {} not found during uninstall",
309-
component.short_name(manifest),
309+
manifest.short_name(&component),
310310
);
311311
}
312312

@@ -556,12 +556,12 @@ impl Update {
556556
match &component.target {
557557
Some(t) if t != &manifestation.target_triple => info!(
558558
"component {} for target {} is up to date",
559-
component.short_name(new_manifest),
559+
new_manifest.short_name(component),
560560
t,
561561
),
562562
_ => info!(
563563
"component {} is up to date",
564-
component.short_name(new_manifest)
564+
new_manifest.short_name(component)
565565
),
566566
}
567567
}
@@ -634,7 +634,7 @@ impl<'a> ComponentBinary<'a> {
634634
Ok(None) => return None,
635635
Err(e) => return Some(Err(e)),
636636
},
637-
status: download_cfg.status_for(component.short_name(manifest)),
637+
status: download_cfg.status_for(manifest.short_name(&component)),
638638
component,
639639
manifest,
640640
download_cfg,
@@ -665,7 +665,7 @@ impl<'a> ComponentBinary<'a> {
665665
)
666666
.await
667667
.with_context(|| {
668-
RustupError::ComponentDownloadFailed(self.component.name(self.manifest))
668+
RustupError::ComponentDownloadFailed(self.manifest.name(&self.component))
669669
})?;
670670

671671
Ok((self, downloaded_file))
@@ -683,7 +683,7 @@ impl<'a> ComponentBinary<'a> {
683683
// component name plus the target triple.
684684
let pkg_name = self.component.name_in_manifest();
685685
let short_pkg_name = self.component.short_name_in_manifest();
686-
let short_name = self.component.short_name(self.manifest);
686+
let short_name = self.manifest.short_name(&self.component);
687687

688688
self.status.installing();
689689

src/dist/manifestation/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ async fn unavailable_component() {
663663
assert_eq!(toolchain, "nightly");
664664
let descriptions = components
665665
.iter()
666-
.map(|c| c.description(&manifest))
666+
.map(|c| manifest.description(c))
667667
.collect::<Vec<_>>();
668668
assert_eq!(descriptions, ["'bonus' for target 'x86_64-apple-darwin'"])
669669
}
@@ -708,7 +708,7 @@ async fn unavailable_component_from_profile() {
708708
assert_eq!(toolchain, "nightly");
709709
let descriptions = components
710710
.iter()
711-
.map(|c| c.description(&manifest))
711+
.map(|c| manifest.description(c))
712712
.collect::<Vec<_>>();
713713
assert_eq!(descriptions, ["'rustc' for target 'x86_64-apple-darwin'"])
714714
}
@@ -762,7 +762,7 @@ async fn removed_component() {
762762
assert_eq!(toolchain, "nightly");
763763
let descriptions = components
764764
.iter()
765-
.map(|c| c.description(&manifest))
765+
.map(|c| manifest.description(c))
766766
.collect::<Vec<_>>();
767767
assert_eq!(descriptions, ["'bonus' for target 'x86_64-apple-darwin'"])
768768
}
@@ -826,7 +826,7 @@ async fn unavailable_components_is_target() {
826826
assert_eq!(toolchain, "nightly");
827827
let descriptions = components
828828
.iter()
829-
.map(|c| c.description(&manifest))
829+
.map(|c| manifest.description(c))
830830
.collect::<Vec<_>>();
831831
assert_eq!(
832832
descriptions,
@@ -891,7 +891,7 @@ async fn unavailable_components_with_same_target() {
891891
assert_eq!(toolchain, "nightly");
892892
let descriptions = components
893893
.iter()
894-
.map(|c| c.description(&manifest))
894+
.map(|c| manifest.description(c))
895895
.collect::<Vec<_>>();
896896
assert_eq!(
897897
descriptions,

src/dist/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ fn components_missing_msg(cs: &[Component], manifest: &ManifestV2, toolchain: &s
6767
let _ = writeln!(
6868
buf,
6969
"component {} is unavailable for download for channel '{}'",
70-
c.description(manifest),
70+
manifest.description(c),
7171
toolchain,
7272
);
7373
}
7474
cs => {
7575
let cs_str = cs
7676
.iter()
77-
.map(|c| c.description(manifest))
77+
.map(|c| manifest.description(c))
7878
.collect::<Vec<_>>()
7979
.join(", ");
8080
let _ = write!(
@@ -1048,8 +1048,8 @@ impl<'cfg, 'a> DistOptions<'cfg, 'a> {
10481048
.iter()
10491049
.map(|component| {
10501050
match component.target.as_ref() == Some(&toolchain.target) {
1051-
true => component.short_name(manifest),
1052-
false => component.name(manifest),
1051+
true => manifest.short_name(component),
1052+
false => manifest.name(component),
10531053
}
10541054
})
10551055
.join(", ");

src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn component_unavailable_msg(cs: &[Component], manifest: &Manifest, toolchain: &
198198
let _ = writeln!(
199199
buf,
200200
"component {} is unavailable for download for channel '{}'",
201-
c.description(manifest),
201+
manifest.description(c),
202202
toolchain,
203203
);
204204

@@ -217,12 +217,12 @@ fn component_unavailable_msg(cs: &[Component], manifest: &Manifest, toolchain: &
217217

218218
let cs_str = if same_target {
219219
cs.iter()
220-
.map(|c| format!("'{}'", c.short_name(manifest)))
220+
.map(|c| format!("'{}'", manifest.short_name(c)))
221221
.collect::<Vec<_>>()
222222
.join(", ")
223223
} else {
224224
cs.iter()
225-
.map(|c| c.description(manifest))
225+
.map(|c| manifest.description(c))
226226
.collect::<Vec<_>>()
227227
.join(", ")
228228
};

src/toolchain/distributable.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a> DistributableToolchain<'a> {
107107
}
108108
return Err(RustupError::UnknownComponent {
109109
desc,
110-
component: component.description(&manifest),
110+
component: manifest.description(&component),
111111
suggestion,
112112
}
113113
.into());
@@ -169,7 +169,7 @@ impl<'a> DistributableToolchain<'a> {
169169
// check if all the components we want are installed
170170
let wanted_components = components.iter().all(|name| {
171171
installed_components.iter().any(|status| {
172-
let cname = status.component.short_name(&manifest);
172+
let cname = manifest.short_name(&status.component);
173173
let cname = cname.as_str();
174174
let cnameim = status.component.short_name_in_manifest();
175175
let cnameim = cnameim.as_str();
@@ -259,8 +259,8 @@ impl<'a> DistributableToolchain<'a> {
259259
.map(|c| {
260260
(
261261
damerau_levenshtein(
262-
&c.component.name(manifest)[..],
263-
&component.name(manifest)[..],
262+
&manifest.name(&c.component)[..],
263+
&manifest.name(component)[..],
264264
),
265265
c,
266266
)
@@ -275,7 +275,7 @@ impl<'a> DistributableToolchain<'a> {
275275
(
276276
damerau_levenshtein(
277277
&c.component.name_in_manifest()[..],
278-
&component.name(manifest)[..],
278+
&manifest.name(component)[..],
279279
),
280280
c,
281281
)
@@ -284,7 +284,7 @@ impl<'a> DistributableToolchain<'a> {
284284
.expect("There should be always at least one component");
285285

286286
let mut closest_distance = short_name_distance;
287-
let mut closest_match = short_name_distance.1.component.short_name(manifest);
287+
let mut closest_match = manifest.short_name(&short_name_distance.1.component);
288288

289289
// Find closer suggestion
290290
if short_name_distance.0 > long_name_distance.0 {
@@ -304,8 +304,8 @@ impl<'a> DistributableToolchain<'a> {
304304
}
305305
} else {
306306
// Check if only targets differ
307-
if closest_distance.1.component.short_name(manifest)
308-
== component.short_name(manifest)
307+
if manifest.short_name(&closest_distance.1.component)
308+
== manifest.short_name(component)
309309
{
310310
closest_match = short_name_distance.1.component.target();
311311
}
@@ -355,9 +355,9 @@ impl<'a> DistributableToolchain<'a> {
355355
if let Some(component_name) = component_for_bin(&binary_lossy) {
356356
let component_status = component_statuses
357357
.iter()
358-
.find(|cs| cs.component.short_name(&manifest) == component_name)
358+
.find(|cs| manifest.short_name(&cs.component) == component_name)
359359
.ok_or_else(|| anyhow!("component {component_name} should be in the manifest"))?;
360-
let short_name = component_status.component.short_name(&manifest);
360+
let short_name = manifest.short_name(&component_status.component);
361361
if !component_status.available {
362362
Err(anyhow!(
363363
"the '{short_name}' component which provides the command '{binary_lossy}' is not available for the '{desc}' toolchain"
@@ -417,7 +417,7 @@ impl<'a> DistributableToolchain<'a> {
417417
}
418418
return Err(RustupError::UnknownComponent {
419419
desc: self.desc.clone(),
420-
component: component.description(&manifest),
420+
component: manifest.description(&component),
421421
suggestion,
422422
}
423423
.into());

0 commit comments

Comments
 (0)