use std::{ fs, io::{self, Write}, path::{Path, PathBuf}, }; use log::{debug, trace}; use serde::{Deserialize, Serialize}; use super::{ cli::Cli, sharry::{Alias, FileChecked, FileUploading, Share}, }; #[derive(Serialize, Deserialize, Debug)] pub struct AppState { alias: Alias, share: Share, checked: Vec, uploading: Vec, } fn get_cachefile(args: &Cli) -> Option { let file_name: PathBuf = dirs_next::cache_dir()? .join("shrupl") .join(format!("{}.json", args.get_hash())); trace!("cachefile: {}", file_name.display()); Some(file_name) } impl AppState { fn load(file_name: impl AsRef) -> io::Result { let content = fs::read_to_string(file_name)?; serde_json::from_str(&content).map_err(io::Error::other) } fn save(&self, file_name: impl AsRef) -> io::Result<()> { let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?; let mut file = fs::File::create(file_name)?; file.write_all(json.as_bytes())?; Ok(()) } pub fn try_resume(args: &Cli) -> Option { let file_name = get_cachefile(args)?; // let content = fs::read_to_string(&file_name).ok()?; // serde_json::from_str(&content) // .inspect_err(|e| debug!("could not resume from {}: {e}", &file_name.display())) // .ok() Self::load(&file_name) .inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display())) .ok() } }