From 784d1f0bf6ce11a989fbf9f6321ca01a4a46082c Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Thu, 9 Jun 2022 01:32:38 +1000
Subject: [PATCH] Impl new fn `helpers::download_and_extract`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 src/helpers.rs | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/helpers.rs b/src/helpers.rs
index 8473cfe3..3eac6bc6 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -53,6 +53,15 @@ pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallErr
 /// Download a file from the provided URL to the provided path
 pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), BinstallError> {
     let url = Url::parse(url)?;
+    download_and_extract(url, PkgFmt::Bin, path.as_ref()).await
+}
+
+/// Download a file from the provided URL and extract it to the provided path
+pub async fn download_and_extract<P: AsRef<Path>>(
+    url: Url,
+    fmt: PkgFmt,
+    path: P,
+) -> Result<(), BinstallError> {
     debug!("Downloading from: '{url}'");
 
     let resp = reqwest::get(url.clone())
@@ -68,7 +77,7 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
     debug!("Downloading to file: '{}'", path.display());
 
     let mut bytes_stream = resp.bytes_stream();
-    let mut writer = AsyncFileWriter::new(path, PkgFmt::Bin);
+    let mut writer = AsyncFileWriter::new(path, fmt);
 
     while let Some(res) = bytes_stream.next().await {
         writer.write(res?).await?;