uri::Uri creation
This commit is contained in:
parent
2cc13f24e7
commit
470ebc4305
2 changed files with 45 additions and 20 deletions
16
src/cli.rs
16
src/cli.rs
|
|
@ -2,11 +2,7 @@ use std::{convert::Infallible, fmt, io, time::Duration};
|
||||||
|
|
||||||
use base64ct::{Base64UrlUnpadded, Encoding};
|
use base64ct::{Base64UrlUnpadded, Encoding};
|
||||||
use blake2b_simd::Params as Blake2b;
|
use blake2b_simd::Params as Blake2b;
|
||||||
use clap::{
|
use clap::{Parser, builder::TypedValueParser, value_parser};
|
||||||
Parser,
|
|
||||||
builder::{PossibleValuesParser, TypedValueParser},
|
|
||||||
value_parser,
|
|
||||||
};
|
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -25,14 +21,6 @@ pub struct Cli {
|
||||||
)]
|
)]
|
||||||
timeout: Duration,
|
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
|
/// Number of times actions are retried
|
||||||
#[arg(short, long, default_value_t = 5, value_name = "N")]
|
#[arg(short, long, default_value_t = 5, value_name = "N")]
|
||||||
retry_limit: u32,
|
retry_limit: u32,
|
||||||
|
|
@ -118,7 +106,7 @@ impl Cli {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_uri(&self) -> Uri {
|
pub fn get_uri(&self) -> Uri {
|
||||||
Uri::new(&self.protocol, &self.url)
|
Uri::from(self.url.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
|
||||||
|
|
@ -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};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
|
@ -18,11 +19,47 @@ impl AsRef<[u8]> for Uri {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Uri {
|
impl From<String> for Uri {
|
||||||
pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self {
|
fn from(value: String) -> Self {
|
||||||
Self(format!("{protocol}://{base_url}"))
|
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 {
|
fn endpoint(&self, path: fmt::Arguments) -> String {
|
||||||
let uri = format!("{}/api/v2/{path}", self.0);
|
let uri = format!("{}/api/v2/{path}", self.0);
|
||||||
trace!("endpoint: {uri:?}");
|
trace!("endpoint: {uri:?}");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue