general API consolidation

This commit is contained in:
Jörn-Michael Miehe 2025-06-04 21:23:21 +00:00
parent c246de0c99
commit fd79455de4
2 changed files with 21 additions and 24 deletions

View file

@ -29,20 +29,20 @@ enum FileState {
}
impl AppState {
fn cache_dir() -> Option<PathBuf> {
let dir_name = dirs_next::cache_dir()?.join("shrupl");
fn cache_dir() -> PathBuf {
let dir_name = dirs_next::cache_dir()
.expect("could not determine cache directory")
.join("shrupl");
trace!("cachedir: {}", dir_name.display());
Some(dir_name)
dir_name
}
fn cache_file(args: &Cli) -> Option<PathBuf> {
let file_name = Self::cache_dir()?.join(format!("{}.json", args.get_hash()));
fn cache_file(args: &Cli) -> PathBuf {
let file_name = Self::cache_dir().join(format!("{}.json", args.get_hash()));
trace!("cachefile: {}", file_name.display());
Some(file_name)
file_name
}
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> {
@ -51,7 +51,7 @@ impl AppState {
}
pub fn try_resume(args: &Cli) -> Option<Self> {
let file_name = Self::cache_file(args)?;
let file_name = Self::cache_file(args);
Self::load(&file_name)
.inspect_err(|e| debug!("could not resume from {}: {e}", file_name.display()))
@ -68,16 +68,15 @@ impl AppState {
.ok()
}
pub fn from_args(args: Cli, agent: &ureq::Agent) -> Option<Self> {
let file_name = Self::cache_file(&args)?;
pub fn from_args(args: &Cli, agent: &ureq::Agent) -> Option<Self> {
let file_name = Self::cache_file(args);
let alias = args.get_alias();
let share = Share::create(&agent, &alias, args.get_share_request())
let share = Share::create(agent, &alias, args.get_share_request())
.inspect_err(|e| error!("could not create Share: {e}"))
.ok()?;
let files: Vec<_> = args.files.into_iter().map(|f| FileState::C(f)).collect();
let files: Vec<_> = args.files.clone().into_iter().map(FileState::C).collect();
Some(Self {
file_name,
@ -88,24 +87,20 @@ impl AppState {
}
pub fn save(&self) -> io::Result<()> {
let cache_dir = Self::cache_dir()
.ok_or_else(|| io::Error::other("could not determine cache directory"))?;
fs::create_dir_all(cache_dir)?;
fs::create_dir_all(Self::cache_dir())?;
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
let mut file = fs::File::create(&self.file_name)?;
file.write_all(json.as_bytes())?;
trace!("successfully saved AppState");
Ok(())
}
pub fn delete(self) -> io::Result<()> {
pub fn clear(self) -> io::Result<()> {
fs::remove_file(self.file_name)?;
trace!("successfully cleared AppState");
Ok(())
}
}

View file

@ -23,19 +23,21 @@ fn main() {
.into();
let state = AppState::try_resume(&args)
.and_then(|state| {
.map(|state| {
info!("loaded state: {state:?}");
Some(state)
state
})
.or_else(|| AppState::from_args(args, &agent))
.or_else(|| AppState::from_args(&args, &agent))
.unwrap_or_else(|| {
error!("could not create new state from cli arguments: {args:?}");
std::process::exit(1);
});
info!("continuing with state: {state:?}");
state.save().unwrap();
// state.delete().unwrap();
// state.clear().unwrap();
return;