uri::Uri creation

This commit is contained in:
Jörn-Michael Miehe 2025-07-03 12:58:53 +00:00
parent 2cc13f24e7
commit 470ebc4305
2 changed files with 45 additions and 20 deletions

View file

@ -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]

View file

@ -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<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)
.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:?}");