summaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
authorNaz <ndpm13@ch-naseem.com>2025-07-30 07:11:24 +0100
committerNaz <ndpm13@ch-naseem.com>2025-07-30 07:11:24 +0100
commitdf40ab0fd7d9641297833dc7e0691522c25e2bc9 (patch)
treeb185168ceedb4f71b255e7e61a365566da41538d /src/index.rs
parentd3c84b04d7a46735dd19a2dae9448e811a609291 (diff)
parent11a86042e73bb0eecad61ac6e636dd98563167f5 (diff)
Merge pull request '🔧refactor: separate download logic from AppImage struct' (#11) from feat/issue-7 into main
Reviewed-on: https://git.ch-naseem.com/ndpm13/zap-rs/pulls/11
Diffstat (limited to 'src/index.rs')
-rw-r--r--src/index.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/index.rs b/src/index.rs
new file mode 100644
index 0000000..069068a
--- /dev/null
+++ b/src/index.rs
@@ -0,0 +1,42 @@
+use tokio::fs;
+
+use crate::{AppImage, index_dir};
+
+#[derive(Debug, Default)]
+pub struct Index {}
+
+impl Index {
+ pub fn new() -> Self {
+ Self {}
+ }
+ pub async fn get(&self, appname: &str) -> Result<AppImage, Box<dyn std::error::Error>> {
+ 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 async fn add(
+ &self,
+ appimage: &AppImage,
+ appname: &str,
+ ) -> Result<(), Box<dyn std::error::Error>> {
+ fs::create_dir_all(&index_dir()).await?;
+
+ let index_file = &index_dir().join(format!("{appname}.json"));
+
+ let json = serde_json::to_string_pretty(appimage)?;
+ fs::write(index_file, json).await?;
+
+ Ok(())
+ }
+ pub async fn remove(&self, appname: &str) -> Result<(), Box<dyn std::error::Error>> {
+ let index_file_path = index_dir().join(format!("{appname}.json"));
+ fs::remove_file(index_file_path).await?;
+
+ Ok(())
+ }
+}