use elicitation::{elicit_newtype, elicit_newtype_traits};
use elicitation_derive::reflect_methods;
use tracing::instrument;
elicit_newtype!(url::Url, as Url, serde);
elicit_newtype_traits!(Url, url::Url, [cmp, display, from_str]);
#[reflect_methods]
impl Url {
#[instrument(skip(self))]
pub fn as_str(&self) -> String {
self.0.as_str().to_string()
}
#[instrument(skip(self))]
pub fn scheme(&self) -> String {
self.0.scheme().to_string()
}
#[instrument(skip(self))]
pub fn host(&self) -> Option<String> {
self.0.host_str().map(str::to_string)
}
#[instrument(skip(self))]
pub fn port(&self) -> Option<u16> {
self.0.port()
}
#[instrument(skip(self))]
pub fn port_or_default(&self) -> Option<u16> {
self.0.port_or_known_default()
}
#[instrument(skip(self))]
pub fn path(&self) -> String {
self.0.path().to_string()
}
#[instrument(skip(self))]
pub fn query(&self) -> Option<String> {
self.0.query().map(str::to_string)
}
#[instrument(skip(self))]
pub fn fragment(&self) -> Option<String> {
self.0.fragment().map(str::to_string)
}
#[instrument(skip(self))]
pub fn username(&self) -> String {
self.0.username().to_string()
}
#[instrument(skip(self))]
pub fn has_authority(&self) -> bool {
self.0.has_authority()
}
#[instrument(skip(self))]
pub fn join(&self, input: String) -> Option<String> {
self.0.join(&input).ok().map(|u| u.to_string())
}
#[instrument(skip(self))]
pub fn origin(&self) -> String {
self.0.origin().ascii_serialization()
}
}
impl Url {
pub fn parse(s: &str) -> Option<Self> {
url::Url::parse(s)
.ok()
.map(|u| std::sync::Arc::new(u).into())
}
}