From 2cc13f24e7af906cfa3d43882a9f12d3e6e28f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 2 Jul 2025 12:01:07 +0000 Subject: [PATCH 1/3] minor refactoring - untangle `CacheFile::{rewind_chunk, abort_upload}` - change `file::Uploading::rewind` signature to a more "builder"esque pattern --- src/cachefile.rs | 23 +++++++++++------------ src/file/uploading.rs | 10 ++++------ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/cachefile.rs b/src/cachefile.rs index 15b18e9..36fed9c 100644 --- a/src/cachefile.rs +++ b/src/cachefile.rs @@ -169,23 +169,22 @@ impl CacheFile { } pub fn rewind_chunk(mut self) -> Option { - self.uploading = Some( - self.uploading - .take() - .expect("rewind_chunk called while not uploading") - .rewind()?, - ); + let upl = self + .uploading + .take() + .expect("rewind_chunk called while not uploading"); + self.uploading = Some(upl.rewind()?); Some(self) } pub fn abort_upload(&mut self) { - self.files.push_front( - self.uploading - .take() - .expect("abort_upload called while not uploading") - .abort(), - ); + let upl = self + .uploading + .take() + .expect("abort_upload called while not uploading"); + + self.files.push_front(upl.abort()); } pub fn share_notify(&self, client: &impl Client) -> crate::Result<()> { diff --git a/src/file/uploading.rs b/src/file/uploading.rs index 290f494..8ac6eea 100644 --- a/src/file/uploading.rs +++ b/src/file/uploading.rs @@ -46,13 +46,11 @@ impl Uploading { self.offset } - pub fn rewind(self) -> Option { + pub fn rewind(mut self) -> Option { if let Some(last_offset) = self.last_offset { - Some(Self { - last_offset: None, - offset: last_offset, - ..self - }) + self.last_offset = None; + self.offset = last_offset; + Some(self) } else { warn!("attempted to rewind twice"); None From 470ebc4305f81a49fe983b9b03b1e292a6cdd397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 3 Jul 2025 12:58:53 +0000 Subject: [PATCH 2/3] `uri::Uri` creation --- src/cli.rs | 16 ++-------------- src/sharry/uri.rs | 49 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index b6dc9ca..abc0237 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,11 +2,7 @@ use std::{convert::Infallible, fmt, io, time::Duration}; use base64ct::{Base64UrlUnpadded, Encoding}; use blake2b_simd::Params as Blake2b; -use clap::{ - Parser, - builder::{PossibleValuesParser, TypedValueParser}, - value_parser, -}; +use clap::{Parser, builder::TypedValueParser, value_parser}; use log::LevelFilter; use crate::{ @@ -25,14 +21,6 @@ pub struct Cli { )] timeout: Duration, - /// Protocol for Sharry instance - #[arg( - short, long, - default_value = "https", value_name = "VARIANT", - value_parser = PossibleValuesParser::new(["http", "https"]), - )] - protocol: String, - /// Number of times actions are retried #[arg(short, long, default_value_t = 5, value_name = "N")] retry_limit: u32, @@ -118,7 +106,7 @@ impl Cli { #[must_use] pub fn get_uri(&self) -> Uri { - Uri::new(&self.protocol, &self.url) + Uri::from(self.url.clone()) } #[must_use] diff --git a/src/sharry/uri.rs b/src/sharry/uri.rs index 793a133..2a257a5 100644 --- a/src/sharry/uri.rs +++ b/src/sharry/uri.rs @@ -1,6 +1,7 @@ -use std::fmt; +use std::{fmt, sync::LazyLock}; -use log::trace; +use log::{debug, trace}; +use regex::Regex; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -18,11 +19,47 @@ impl AsRef<[u8]> for Uri { } } -impl Uri { - pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self { - Self(format!("{protocol}://{base_url}")) - } +impl From for Uri { + fn from(value: String) -> Self { + fn parse_url(value: &str) -> Option<(String, String)> { + /// Pattern breakdown: + /// - `^(?P[^:/?#]+)://` - capture scheme (anything but `:/?#`) + `"://"` + /// - `(?P[^/?#]+)` - capture authority/host (anything but `/?#`) + /// - `(/.*)?` - maybe trailing slash and some path + /// - `$` - end of string + static SHARRY_URI_RE: LazyLock = LazyLock::new(|| { + trace!("compiling SHARRY_URI_RE"); + Regex::new(r"^(?P[^:/?#]+)://(?P[^/?#]+)(/.*)?$") + .expect("Regex compilation failed") + }); + + SHARRY_URI_RE.captures(value).map(|caps| { + let captured = |name| { + caps.name(name) + .expect(&format!("{name} not captured")) + .as_str() + .to_string() + }; + + (captured("scheme"), captured("host")) + }) + } + + trace!("TryFrom {value:?}"); + + if let Some((scheme, host)) = parse_url(&value) { + let result = Self(format!("{scheme}://{host}")); + debug!("{result:?}"); + + result + } else { + Self(value) + } + } +} + +impl Uri { fn endpoint(&self, path: fmt::Arguments) -> String { let uri = format!("{}/api/v2/{path}", self.0); trace!("endpoint: {uri:?}"); From 46913e93b9fa1faee54d87c030d874ff41dddaaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 3 Jul 2025 13:02:13 +0000 Subject: [PATCH 3/3] use `to_string` instead of `to_owned` where applicable --- src/appstate.rs | 2 +- src/sharry/ids.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/appstate.rs b/src/appstate.rs index 99ea3d4..5590e8e 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -62,7 +62,7 @@ impl AppState { if let Some(upl) = self.inner.peek_uploading() { if bar.length().is_none() { bar.set_length(upl.get_size()); - bar.set_message(upl.get_name().to_owned()); + bar.set_message(upl.get_name().to_string()); bar.enable_steady_tick(Duration::from_millis(100)); } diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 6f9121d..57a5cd2 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -76,7 +76,7 @@ impl TryFrom for FileID { .captures(&value) .and_then(|caps| caps.name("fid").map(|m| m.as_str())) { - let result = Self(fid.to_owned()); + let result = Self(fid.to_string()); debug!("{result:?}"); Ok(result) @@ -112,8 +112,8 @@ mod tests { ]; for (good, expected_fid) in cases { - let s = good.to_string(); - let file_id = FileID::try_from(s.clone()).expect("URL should parse successfully"); + let file_id = + FileID::try_from(good.to_string()).expect("URL should parse successfully"); assert_eq!( file_id.0, expected_fid, "Expected `{}` → FileID({}), got {:?}",