create cache module

This commit is contained in:
2024-12-09 04:31:00 +01:00
parent b0c2c8d478
commit 573fc14767
5 changed files with 52 additions and 50 deletions

View File

@@ -2,7 +2,7 @@ use std::{fs, path::Path};
use indicatif::{ProgressBar, ProgressStyle};
use crate::{global, structs};
use crate::global;
pub fn stdin() -> String {
let mut input = String::new();
@@ -140,24 +140,6 @@ pub fn prefix(tag_name: &str) -> String {
.map_or_else(|| tag_name.to_string(), |tag| tag.formatted())
}
pub fn get_cache(dir: &Path) -> structs::Cache {
let cache_path = dir.join("awcache.json");
let cache_content = fs::read_to_string(cache_path).unwrap_or_default();
if cache_content.trim().is_empty() {
structs::Cache::default()
} else {
serde_json::from_str(&cache_content).unwrap_or_default()
}
}
pub fn save_cache(dir: &Path, cache: structs::Cache) {
let cache_path = dir.join("awcache.json");
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
fs::write(cache_path, cache_serialized).unwrap_or_else(|e| {
println_error!("Failed to save cache: {}", e);
});
}
pub fn random_string(length: u32) -> String {
use rand::Rng;
let mut rng = rand::thread_rng();