diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:08:45 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:08:45 +0100 |
| commit | 57ffe6ff51a8f3ccc41607f18dcf3c9039e85d94 (patch) | |
| tree | 7ab8e10bf2fce88ca3cdfb2a6b13c0717c638407 /src/downloader.rs | |
| parent | 6b3f5d37fbce2e880a454e66d8fbd78269f1c867 (diff) | |
| parent | dc0ee6ce99a0480b7a6c228492936b16ceaf60cd (diff) | |
Merge pull request 'Add comprehensive error types' (#12) from feat/issue-5 into main
Reviewed-on: https://git.ch-naseem.com/ndpm13/zap-rs/pulls/12
Diffstat (limited to 'src/downloader.rs')
| -rw-r--r-- | src/downloader.rs | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/downloader.rs b/src/downloader.rs index 2196e25..0141424 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -2,7 +2,7 @@ use futures_util::StreamExt; use std::path::PathBuf; use tokio::{fs, io::AsyncWriteExt}; -use crate::{appimages_dir, make_progress_bar}; +use crate::{Error, Result, appimages_dir, make_progress_bar}; #[derive(Debug, Default)] pub struct Downloader {} @@ -11,32 +11,36 @@ impl Downloader { pub fn new() -> Self { Self {} } - pub fn prepare_path(&self, url: &str, executable: &str) -> PathBuf { + pub fn prepare_path(&self, url: &str, executable: &str) -> Result<PathBuf> { // Try to extract filename from URL or use default let filename = match url.split('/').next_back() { Some(name) => name.to_string(), None => format!("{executable}.AppImage"), }; - appimages_dir().join(filename) + Ok(appimages_dir()?.join(filename)) } - pub async fn download_with_progress( - &self, - url: &str, - path: &PathBuf, - ) -> Result<(), Box<dyn std::error::Error>> { - fs::create_dir_all(&appimages_dir()).await?; - - let resp = reqwest::get(&url.to_string()).await?; + pub async fn download_with_progress(&self, url: &str, path: &PathBuf) -> Result<()> { + fs::create_dir_all(&appimages_dir()?).await?; + + let resp = reqwest::get(&url.to_string()) + .await + .map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let total_size = resp.content_length().unwrap_or(0); - let bar = make_progress_bar(total_size); + let bar = make_progress_bar(total_size)?; let mut out = tokio::fs::File::create(&path).await?; // Stream download with progress updates let mut stream = resp.bytes_stream(); while let Some(chunk) = stream.next().await { - let chunk = chunk?; + let chunk = chunk.map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let len = chunk.len() as u64; out.write_all(&chunk).await?; bar.inc(len); |
