From 592e7bf76e263abb0889131d60474005b17052e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 11 Jun 2025 00:28:02 +0000 Subject: [PATCH] [wip] impl `Client` for `ureq::Agent` - `AppState` creation --- src/appstate.rs | 76 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/src/appstate.rs b/src/appstate.rs index dbec49b..db8c64b 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -1,6 +1,6 @@ use std::{ collections::VecDeque, - fs, + fmt, fs, io::{self, Write}, path::{Path, PathBuf}, time::Duration, @@ -17,7 +17,7 @@ use super::{ sharry::{self, Client, ClientError, Uri}, }; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize)] pub struct AppState { #[serde(skip)] file_name: PathBuf, @@ -32,6 +32,18 @@ pub struct AppState { files: VecDeque, } +impl fmt::Debug for AppState { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AppState") + .field("file_name", &self.file_name) + .field("uri", &self.uri) + .field("alias_id", &self.alias_id) + .field("share_id", &self.share_id) + .field("files", &self.files) + .finish() + } +} + #[derive(Serialize, Deserialize, Debug)] enum FileState { C(file::Checked), @@ -80,29 +92,48 @@ impl AppState { file_name } - 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 new( + file_name: PathBuf, + chunk_size: usize, + uri: Uri, + alias_id: String, + share_id: String, + files: VecDeque, + ) -> Self { + Self { + file_name, + progress: None, + buffer: vec![0; chunk_size * 1024 * 1024], + uri, + alias_id, + share_id, + files, + } + } + + fn load(file_name: &Path, chunk_size: usize) -> io::Result { + let file = fs::File::open(file_name)?; + serde_json::from_reader(io::BufReader::new(file)) + .map_err(io::Error::other) + .map(|state: Self| { + debug!("successfully loaded AppState"); + + Self::new( + file_name.to_owned(), + chunk_size, + state.uri, + state.alias_id, + state.share_id, + state.files, + ) + }) } pub fn try_resume(args: &Cli) -> Option { let file_name = Self::cache_file(args); - Self::load(&file_name) + Self::load(&file_name, args.chunk_size) .inspect_err(|e| debug!("could not resume from {:?}: {e}", file_name.display())) - .map(|state| { - debug!("successfully loaded AppState"); - - Self { - file_name, - progress: None, - buffer: Vec::with_capacity(args.chunk_size), - uri: state.uri, - alias_id: state.alias_id, - share_id: state.share_id, - files: state.files, - } - }) .ok() } @@ -119,15 +150,14 @@ impl AppState { let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect(); - Ok(Self { + Ok(Self::new( file_name, - progress: None, - buffer: Vec::with_capacity(args.chunk_size), + args.chunk_size, uri, alias_id, share_id, files, - }) + )) } pub fn file_names(&self) -> Vec<&str> {