[wip] impl Client for ureq::Agent

- `AppState` creation
This commit is contained in:
Jörn-Michael Miehe 2025-06-11 00:28:02 +00:00
parent 0993679641
commit 592e7bf76e

View file

@ -1,6 +1,6 @@
use std::{ use std::{
collections::VecDeque, collections::VecDeque,
fs, fmt, fs,
io::{self, Write}, io::{self, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
time::Duration, time::Duration,
@ -17,7 +17,7 @@ use super::{
sharry::{self, Client, ClientError, Uri}, sharry::{self, Client, ClientError, Uri},
}; };
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize)]
pub struct AppState { pub struct AppState {
#[serde(skip)] #[serde(skip)]
file_name: PathBuf, file_name: PathBuf,
@ -32,6 +32,18 @@ pub struct AppState {
files: VecDeque<FileState>, files: VecDeque<FileState>,
} }
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)] #[derive(Serialize, Deserialize, Debug)]
enum FileState { enum FileState {
C(file::Checked), C(file::Checked),
@ -80,29 +92,48 @@ impl AppState {
file_name file_name
} }
fn load(file_name: impl AsRef<Path>) -> io::Result<Self> { fn new(
let content = fs::read_to_string(file_name)?; file_name: PathBuf,
serde_json::from_str(&content).map_err(io::Error::other) chunk_size: usize,
uri: Uri,
alias_id: String,
share_id: String,
files: VecDeque<FileState>,
) -> 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<Self> {
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<Self> { 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) Self::load(&file_name, args.chunk_size)
.inspect_err(|e| debug!("could not resume from {:?}: {e}", file_name.display())) .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() .ok()
} }
@ -119,15 +150,14 @@ impl AppState {
let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect(); let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect();
Ok(Self { Ok(Self::new(
file_name, file_name,
progress: None, args.chunk_size,
buffer: Vec::with_capacity(args.chunk_size),
uri, uri,
alias_id, alias_id,
share_id, share_id,
files, files,
}) ))
} }
pub fn file_names(&self) -> Vec<&str> { pub fn file_names(&self) -> Vec<&str> {