Skip to content

Commit d0a4151

Browse files
committed
dist: simplify ComponentBinary construction
1 parent 0635282 commit d0a4151

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/dist/manifest.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ impl Manifest {
287287
Ok(toml::to_string(&self)?)
288288
}
289289

290+
pub(super) fn binary(&self, component: &Component) -> Result<Option<&HashedBinary>> {
291+
let package = self.get_package(component.short_name_in_manifest())?;
292+
let target_package = package.get_target(component.target.as_ref())?;
293+
// We prefer the first format in the list, since the parsing of the
294+
// manifest leaves us with the files/hash pairs in preference order.
295+
Ok(target_package.bins.first())
296+
}
297+
290298
pub fn get_package(&self, name: &str) -> Result<&Package> {
291299
self.packages
292300
.get(name)

src/dist/manifestation.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,31 +152,7 @@ impl Manifestation {
152152
let components = update
153153
.components_to_install
154154
.into_iter()
155-
.filter_map(|component| {
156-
let package = match new_manifest.get_package(component.short_name_in_manifest()) {
157-
Ok(p) => p,
158-
Err(e) => return Some(Err(e)),
159-
};
160-
161-
let target_package = match package.get_target(component.target.as_ref()) {
162-
Ok(tp) => tp,
163-
Err(e) => return Some(Err(e)),
164-
};
165-
166-
match target_package.bins.is_empty() {
167-
// This package is not available, no files to download.
168-
true => None,
169-
// We prefer the first format in the list, since the parsing of the
170-
// manifest leaves us with the files/hash pairs in preference order.
171-
false => Some(Ok(ComponentBinary {
172-
status: download_cfg.status_for(component.short_name(&new_manifest)),
173-
component,
174-
binary: &target_package.bins[0],
175-
manifest: &new_manifest,
176-
download_cfg,
177-
})),
178-
}
179-
})
155+
.filter_map(|component| ComponentBinary::new(component, &new_manifest, download_cfg))
180156
.collect::<Result<Vec<_>>>()?;
181157

182158
const DEFAULT_CONCURRENT_DOWNLOADS: usize = 2;
@@ -647,6 +623,24 @@ struct ComponentBinary<'a> {
647623
}
648624

649625
impl<'a> ComponentBinary<'a> {
626+
fn new(
627+
component: Component,
628+
manifest: &'a Manifest,
629+
download_cfg: &'a DownloadCfg<'a>,
630+
) -> Option<Result<Self>> {
631+
Some(Ok(ComponentBinary {
632+
binary: match manifest.binary(&component) {
633+
Ok(Some(b)) => b,
634+
Ok(None) => return None,
635+
Err(e) => return Some(Err(e)),
636+
},
637+
status: download_cfg.status_for(component.short_name(manifest)),
638+
component,
639+
manifest,
640+
download_cfg,
641+
}))
642+
}
643+
650644
async fn download(self, max_retries: usize) -> Result<(Self, File)> {
651645
use tokio_retry::{RetryIf, strategy::FixedInterval};
652646

0 commit comments

Comments
 (0)