From 180fe06facf1f8b4796df69a42596643990b9d32 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 31 Jul 2025 15:59:56 +0100 Subject: =?UTF-8?q?=E2=9C=A8feat:=20remove=20expect=20from=20path=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/downloader.rs | 6 +++--- src/index.rs | 12 ++++++------ src/manager.rs | 6 +++--- src/paths.rs | 16 +++++++++------- 4 files changed, 21 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/downloader.rs b/src/downloader.rs index be35bd8..cf78a49 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -11,17 +11,17 @@ 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 { // 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<()> { - fs::create_dir_all(&appimages_dir()).await?; + fs::create_dir_all(&appimages_dir()?).await?; let resp = reqwest::get(&url.to_string()).await?; let total_size = resp.content_length().unwrap_or(0); diff --git a/src/index.rs b/src/index.rs index dff2f58..8f2521a 100644 --- a/src/index.rs +++ b/src/index.rs @@ -10,19 +10,19 @@ impl Index { Self {} } pub async fn get(&self, appname: &str) -> Result { - let index_file_path = index_dir().join(format!("{appname}.json")); + let index_file_path = index_dir()?.join(format!("{appname}.json")); let index_file_content = fs::read_to_string(&index_file_path).await?; let appimage: AppImage = serde_json::from_str(&index_file_content)?; Ok(appimage) } - pub fn exists(&self, executable: &str) -> bool { - index_dir().join(format!("{}.json", &executable)).exists() + pub fn exists(&self, executable: &str) -> Result { + Ok(index_dir()?.join(format!("{}.json", &executable)).exists()) } pub async fn add(&self, appimage: &AppImage, appname: &str) -> Result<()> { - fs::create_dir_all(&index_dir()).await?; + fs::create_dir_all(&index_dir()?).await?; - let index_file = &index_dir().join(format!("{appname}.json")); + let index_file = &index_dir()?.join(format!("{appname}.json")); let json = serde_json::to_string_pretty(appimage)?; fs::write(index_file, json).await?; @@ -30,7 +30,7 @@ impl Index { Ok(()) } pub async fn remove(&self, appname: &str) -> Result<()> { - let index_file_path = index_dir().join(format!("{appname}.json")); + let index_file_path = index_dir()?.join(format!("{appname}.json")); fs::remove_file(index_file_path).await?; Ok(()) diff --git a/src/manager.rs b/src/manager.rs index 6616e1a..6f3cfac 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -18,14 +18,14 @@ impl PackageManager { } } pub async fn install(&self, appimage: &mut AppImage, appname: &str) -> Result<()> { - if self.index.exists(&appimage.executable) { + if self.index.exists(&appimage.executable)? { println!("{} is already installed.", appimage.executable); return Ok(()); } appimage.file_path = self .downloader - .prepare_path(&appimage.source.meta.url, &appimage.executable); + .prepare_path(&appimage.source.meta.url, &appimage.executable)?; self.downloader .download_with_progress(&appimage.source.meta.url, &appimage.file_path) .await?; @@ -44,7 +44,7 @@ impl PackageManager { Ok(()) } pub async fn list(&self) -> Result<()> { - let mut appimages = fs::read_dir(index_dir()).await?; + let mut appimages = fs::read_dir(index_dir()?).await?; while let Some(appimage) = appimages.next_entry().await? { if let Some(stem) = appimage.path().file_stem().and_then(|s| s.to_str()) { diff --git a/src/paths.rs b/src/paths.rs index 172cae6..7bbc8f8 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -1,14 +1,16 @@ use std::path::PathBuf; -pub fn zap_rs_home() -> PathBuf { - let home = std::env::var("HOME").expect("HOME not set"); - PathBuf::from(home).join(".local/share/zap-rs") +use crate::Result; + +pub fn zap_rs_home() -> Result { + let home = std::env::var("HOME")?; + Ok(PathBuf::from(home).join(".local/share/zap-rs")) } -pub fn index_dir() -> PathBuf { - zap_rs_home().join("index") +pub fn index_dir() -> Result { + Ok(zap_rs_home()?.join("index")) } -pub fn appimages_dir() -> PathBuf { - zap_rs_home().join("appimages") +pub fn appimages_dir() -> Result { + Ok(zap_rs_home()?.join("appimages")) } -- cgit v1.2.3