shrupl/src/appstate.rs

107 lines
2.7 KiB
Rust
Raw Normal View History

2025-06-02 23:57:17 +00:00
use std::{
fs,
io::{self, Write},
path::{Path, PathBuf},
2025-06-02 23:57:17 +00:00
};
2025-06-04 21:02:35 +00:00
use log::{debug, error, trace};
2025-06-02 23:57:17 +00:00
use serde::{Deserialize, Serialize};
use super::{
cli::Cli,
sharry::{Alias, FileChecked, FileUploading, Share},
2025-06-02 23:57:17 +00:00
};
#[derive(Serialize, Deserialize, Debug)]
pub struct AppState {
2025-06-04 21:02:35 +00:00
#[serde(skip)]
file_name: PathBuf,
2025-06-02 23:57:17 +00:00
alias: Alias,
share: Share,
2025-06-04 21:02:35 +00:00
files: Vec<FileState>,
}
2025-06-04 21:02:35 +00:00
#[derive(Serialize, Deserialize, Debug)]
enum FileState {
C(FileChecked),
U(FileUploading),
2025-06-02 23:57:17 +00:00
}
impl AppState {
2025-06-04 21:23:21 +00:00
fn cache_dir() -> PathBuf {
let dir_name = dirs_next::cache_dir()
.expect("could not determine cache directory")
.join("shrupl");
2025-06-04 21:02:35 +00:00
trace!("cachedir: {}", dir_name.display());
2025-06-04 21:23:21 +00:00
dir_name
2025-06-04 21:02:35 +00:00
}
2025-06-04 21:23:21 +00:00
fn cache_file(args: &Cli) -> PathBuf {
let file_name = Self::cache_dir().join(format!("{}.json", args.get_hash()));
2025-06-04 21:02:35 +00:00
trace!("cachefile: {}", file_name.display());
2025-06-04 21:23:21 +00:00
file_name
2025-06-04 21:02:35 +00:00
}
2025-06-02 23:57:17 +00:00
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
let content = fs::read_to_string(file_name)?;
serde_json::from_str(&content).map_err(io::Error::other)
2025-06-02 23:57:17 +00:00
}
2025-06-04 21:02:35 +00:00
pub fn try_resume(args: &Cli) -> Option<Self> {
2025-06-04 21:23:21 +00:00
let file_name = Self::cache_file(args);
2025-06-04 21:02:35 +00:00
Self::load(&file_name)
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
.map(|state| {
debug!("successfully loaded AppState");
Self {
file_name,
alias: state.alias,
share: state.share,
files: state.files,
}
})
.ok()
}
2025-06-04 21:23:21 +00:00
pub fn from_args(args: &Cli, agent: &ureq::Agent) -> Option<Self> {
let file_name = Self::cache_file(args);
2025-06-04 21:02:35 +00:00
let alias = args.get_alias();
2025-06-04 21:23:21 +00:00
let share = Share::create(agent, &alias, args.get_share_request())
2025-06-04 21:02:35 +00:00
.inspect_err(|e| error!("could not create Share: {e}"))
.ok()?;
2025-06-04 21:23:21 +00:00
let files: Vec<_> = args.files.clone().into_iter().map(FileState::C).collect();
2025-06-04 21:02:35 +00:00
Some(Self {
file_name,
alias,
share,
files,
})
}
pub fn save(&self) -> io::Result<()> {
2025-06-04 21:23:21 +00:00
fs::create_dir_all(Self::cache_dir())?;
2025-06-04 21:02:35 +00:00
2025-06-02 23:57:17 +00:00
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
2025-06-04 21:02:35 +00:00
let mut file = fs::File::create(&self.file_name)?;
2025-06-02 23:57:17 +00:00
file.write_all(json.as_bytes())?;
2025-06-04 21:02:35 +00:00
trace!("successfully saved AppState");
2025-06-02 23:57:17 +00:00
Ok(())
}
2025-06-04 21:23:21 +00:00
pub fn clear(self) -> io::Result<()> {
2025-06-04 21:02:35 +00:00
fs::remove_file(self.file_name)?;
2025-06-02 23:57:17 +00:00
2025-06-04 21:02:35 +00:00
trace!("successfully cleared AppState");
Ok(())
2025-06-02 23:57:17 +00:00
}
}