From 3f5fa86f3a180f7daf2596edc5b13557f12c32f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 5 Jul 2025 01:39:38 +0000 Subject: [PATCH 1/3] rename `impl_ureq` -> `ureq_client` --- src/lib.rs | 2 +- src/{impl_ureq.rs => ureq_client.rs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{impl_ureq.rs => ureq_client.rs} (100%) diff --git a/src/lib.rs b/src/lib.rs index b0754c3..2f6aa7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,9 +6,9 @@ mod cachefile; mod cli; mod error; mod file; -mod impl_ureq; pub mod output; mod sharry; +mod ureq_client; pub use appstate::AppState; pub use cli::Cli; diff --git a/src/impl_ureq.rs b/src/ureq_client.rs similarity index 100% rename from src/impl_ureq.rs rename to src/ureq_client.rs From f03bcb46b6e895da78fdffec91742535f2e6afb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 7 Jul 2025 19:41:28 +0000 Subject: [PATCH 2/3] move TODO to lib --- src/lib.rs | 1 + src/output.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2f6aa7d..1d67508 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ // TODO fix with documentation #![allow(clippy::missing_errors_doc)] +#![allow(clippy::missing_panics_doc)] mod appstate; mod cachefile; diff --git a/src/output.rs b/src/output.rs index aee57ea..ddc9bf2 100644 --- a/src/output.rs +++ b/src/output.rs @@ -61,7 +61,6 @@ where } #[must_use] -#[allow(clippy::missing_panics_doc)] pub fn new_progressbar() -> ProgressBar { ProgressBar::no_length().with_style( ProgressStyle::with_template(&format!( From b77ab83ae0a712b22dd12306396b20f7349b6f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 10 Jul 2025 13:23:28 +0000 Subject: [PATCH 3/3] de-nesting and `LazyLock` usage --- src/output.rs | 2 +- src/sharry/ids.rs | 32 ++++++++++++++--------------- src/sharry/uri.rs | 52 +++++++++++++++++++++++------------------------ 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/output.rs b/src/output.rs index ddc9bf2..f7f3598 100644 --- a/src/output.rs +++ b/src/output.rs @@ -7,7 +7,7 @@ use log::{info, warn}; type StaticStyled<'t> = LazyLock>; -pub static SHRUPL: StaticStyled = LazyLock::new(|| style("ShrUpl").yellow().bold()); +pub const SHRUPL: StaticStyled = LazyLock::new(|| style("ShrUpl").yellow().bold()); #[must_use] pub fn prompt_continue() -> bool { diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 4e0a1c4..fceb30c 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -49,27 +49,25 @@ impl fmt::Display for FileID { } } +/// Pattern breakdown: +/// - `^([^:/?#]+)://` - scheme (anything but `:/?#`) + `"://"` +/// - `([^/?#]+)` - authority/host (anything but `/?#`) +/// - `/api/v2/alias/upload/` - literal path segment +/// - `([^/]+)` - capture SID (one or more non-slash chars) +/// - `/files/tus/` - literal path segment +/// - `(?P[^/]+)` - capture FID (one or more non-slash chars) +/// - `$` - end of string +const UPLOAD_URL_RE: LazyLock = LazyLock::new(|| { + trace!("compiling UPLOAD_URL_RE"); + + Regex::new(r"^([^:/?#]+)://([^/?#]+)/api/v2/alias/upload/[^/]+/files/tus/(?P[^/]+)$") + .expect("Regex compilation failed") +}); + impl TryFrom for FileID { type Error = crate::Error; fn try_from(value: String) -> crate::Result { - /// Pattern breakdown: - /// - `^([^:/?#]+)://` - scheme (anything but `:/?#`) + `"://"` - /// - `([^/?#]+)` - authority/host (anything but `/?#`) - /// - `/api/v2/alias/upload/` - literal path segment - /// - `([^/]+)` - capture SID (one or more non-slash chars) - /// - `/files/tus/` - literal path segment - /// - `(?P[^/]+)` - capture FID (one or more non-slash chars) - /// - `$` - end of string - static UPLOAD_URL_RE: LazyLock = LazyLock::new(|| { - trace!("compiling UPLOAD_URL_RE"); - - Regex::new( - r"^([^:/?#]+)://([^/?#]+)/api/v2/alias/upload/[^/]+/files/tus/(?P[^/]+)$", - ) - .expect("Regex compilation failed") - }); - trace!("TryFrom {value:?}"); if let Some(fid) = UPLOAD_URL_RE diff --git a/src/sharry/uri.rs b/src/sharry/uri.rs index 5134574..5730a21 100644 --- a/src/sharry/uri.rs +++ b/src/sharry/uri.rs @@ -1,7 +1,7 @@ use std::{fmt, sync::LazyLock}; use log::{debug, trace}; -use regex::Regex; +use regex::{Captures, Regex}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -19,33 +19,33 @@ impl AsRef<[u8]> for Uri { } } +fn captured(caps: &Captures, name: &str) -> String { + caps.name(name) + .unwrap_or_else(|| panic!("{name} not captured")) + .as_str() + .to_string() +} + +/// Pattern breakdown: +/// - `^(?P[^:/?#]+)://` - capture scheme (anything but `:/?#`) + `"://"` +/// - `(?P[^/?#]+)` - capture authority/host (anything but `/?#`) +/// - `(/.*)?` - maybe trailing slash and some path +/// - `$` - end of string +const SHARRY_URI_RE: LazyLock = LazyLock::new(|| { + trace!("compiling SHARRY_URI_RE"); + + Regex::new(r"^(?P[^:/?#]+)://(?P[^/?#]+)(/.*)?$") + .expect("Regex compilation failed") +}); + +fn parse_url(value: &str) -> Option<(String, String)> { + SHARRY_URI_RE + .captures(value) + .map(|caps| (captured(&caps, "scheme"), captured(&caps, "host"))) +} + 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) - .unwrap_or_else(|| panic!("{name} not captured")) - .as_str() - .to_string() - }; - - (captured("scheme"), captured("host")) - }) - } - trace!("TryFrom {value:?}"); if let Some((scheme, host)) = parse_url(&value) {