From 225cf74cd9601ee5829bf89de24eacf801787f93 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sun, 12 Jun 2022 20:01:43 +1000 Subject: [PATCH] Refactor: Ret `impl Stream` in `create_request` Since both `download*` function takes a `impl Stream` and the `Response::bytes_stream` takes `Response` by value, thus there is no lifetime issue and we can return `impl Stream` instead of `Response` Signed-off-by: Jiahao XU --- src/helpers.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index a088b99e..721716bb 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,9 @@ use std::fmt::Debug; use std::path::{Path, PathBuf}; +use bytes::Bytes; use cargo_toml::Manifest; +use futures_util::stream::Stream; use log::debug; use reqwest::{Method, Response}; use serde::Serialize; @@ -49,7 +51,9 @@ pub async fn remote_exists(url: Url, method: Method) -> Result Result { +async fn create_request( + url: Url, +) -> Result>, BinstallError> { reqwest::get(url.clone()) .await .and_then(|r| r.error_for_status()) @@ -58,6 +62,7 @@ async fn create_request(url: Url) -> Result { url, err, }) + .map(Response::bytes_stream) } /// Download a file from the provided URL and extract it to the provided path. @@ -68,13 +73,11 @@ pub async fn download_and_extract>( ) -> Result<(), BinstallError> { debug!("Downloading from: '{url}'"); - let resp = create_request(url).await?; + let stream = create_request(url).await?; let path = path.as_ref(); debug!("Downloading and extracting to: '{}'", path.display()); - let stream = resp.bytes_stream(); - match fmt.decompose() { PkgFmtDecomposed::Tar(fmt) => extract_tar_based_stream(stream, path, fmt).await?, PkgFmtDecomposed::Bin => extract_bin(stream, path).await?, @@ -98,12 +101,10 @@ pub async fn download_tar_based_and_visit Result { debug!("Downloading from: '{url}'"); - let resp = create_request(url).await?; + let stream = create_request(url).await?; debug!("Downloading and extracting then in-memory processing"); - let stream = resp.bytes_stream(); - let visitor = extract_tar_based_stream_and_visit(stream, fmt, visitor).await?; debug!("Download, extraction and in-memory procession OK");