[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::{
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<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)]
enum FileState {
C(file::Checked),
@ -80,29 +92,48 @@ impl AppState {
file_name
}
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)
fn new(
file_name: PathBuf,
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> {
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> {