de-nesting and LazyLock usage
This commit is contained in:
parent
f03bcb46b6
commit
b77ab83ae0
3 changed files with 42 additions and 44 deletions
|
|
@ -7,7 +7,7 @@ use log::{info, warn};
|
|||
|
||||
type StaticStyled<'t> = LazyLock<StyledObject<&'t str>>;
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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<fid>[^/]+)` - capture FID (one or more non-slash chars)
|
||||
/// - `$` - end of string
|
||||
const UPLOAD_URL_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
trace!("compiling UPLOAD_URL_RE");
|
||||
|
||||
Regex::new(r"^([^:/?#]+)://([^/?#]+)/api/v2/alias/upload/[^/]+/files/tus/(?P<fid>[^/]+)$")
|
||||
.expect("Regex compilation failed")
|
||||
});
|
||||
|
||||
impl TryFrom<String> for FileID {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: String) -> crate::Result<Self> {
|
||||
/// 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<fid>[^/]+)` - capture FID (one or more non-slash chars)
|
||||
/// - `$` - end of string
|
||||
static UPLOAD_URL_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
trace!("compiling UPLOAD_URL_RE");
|
||||
|
||||
Regex::new(
|
||||
r"^([^:/?#]+)://([^/?#]+)/api/v2/alias/upload/[^/]+/files/tus/(?P<fid>[^/]+)$",
|
||||
)
|
||||
.expect("Regex compilation failed")
|
||||
});
|
||||
|
||||
trace!("TryFrom {value:?}");
|
||||
|
||||
if let Some(fid) = UPLOAD_URL_RE
|
||||
|
|
|
|||
|
|
@ -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<scheme>[^:/?#]+)://` - capture scheme (anything but `:/?#`) + `"://"`
|
||||
/// - `(?P<host>[^/?#]+)` - capture authority/host (anything but `/?#`)
|
||||
/// - `(/.*)?` - maybe trailing slash and some path
|
||||
/// - `$` - end of string
|
||||
const SHARRY_URI_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
trace!("compiling SHARRY_URI_RE");
|
||||
|
||||
Regex::new(r"^(?P<scheme>[^:/?#]+)://(?P<host>[^/?#]+)(/.*)?$")
|
||||
.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<String> for Uri {
|
||||
fn from(value: String) -> Self {
|
||||
fn parse_url(value: &str) -> Option<(String, String)> {
|
||||
/// Pattern breakdown:
|
||||
/// - `^(?P<scheme>[^:/?#]+)://` - capture scheme (anything but `:/?#`) + `"://"`
|
||||
/// - `(?P<host>[^/?#]+)` - capture authority/host (anything but `/?#`)
|
||||
/// - `(/.*)?` - maybe trailing slash and some path
|
||||
/// - `$` - end of string
|
||||
static SHARRY_URI_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
trace!("compiling SHARRY_URI_RE");
|
||||
|
||||
Regex::new(r"^(?P<scheme>[^:/?#]+)://(?P<host>[^/?#]+)(/.*)?$")
|
||||
.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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue