1use tokio::fs;
2
3use crate::{AppImage, Error, Result, index_dir};
4
5#[derive(Debug, Default)]
6pub struct Index {}
7
8impl Index {
9 pub fn new() -> Self {
10 Self {}
11 }
12 pub async fn get(&self, appname: &str) -> Result<AppImage> {
13 let index_file_path = index_dir()?.join(format!("{appname}.json"));
14 let index_file_content = fs::read_to_string(&index_file_path).await.map_err(|e| {
15 if e.kind() == std::io::ErrorKind::NotFound {
16 Error::NotFound(appname.to_string())
17 } else {
18 Error::from(e)
19 }
20 })?;
21 let appimage: AppImage = serde_json::from_str(&index_file_content)?;
22
23 Ok(appimage)
24 }
25 pub fn exists(&self, executable: &str) -> Result<bool> {
26 Ok(index_dir()?.join(format!("{}.json", &executable)).exists())
27 }
28 pub async fn add(&self, appimage: &AppImage, appname: &str) -> Result<()> {
29 fs::create_dir_all(&index_dir()?).await?;
30
31 let index_file = &index_dir()?.join(format!("{appname}.json"));
32
33 let json = serde_json::to_string_pretty(appimage)?;
34 fs::write(index_file, json).await?;
35
36 Ok(())
37 }
38 pub async fn remove(&self, appname: &str) -> Result<()> {
39 let index_file_path = index_dir()?.join(format!("{appname}.json"));
40 fs::remove_file(index_file_path).await?;
41
42 Ok(())
43 }
44}