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) {