use crate::errors::InputSpecError;
use reqwest::blocking::{Client, ClientBuilder};
use std::fmt::Display;
use url::Url;
#[derive(Debug, Clone)]
pub struct UrlSpec {
url: Url,
client: Client,
}
impl UrlSpec {
pub fn url(&self) -> &Url {
&self.url
}
pub fn client(&self) -> &Client {
&self.client
}
pub fn parse(str: &str) -> Result<UrlSpec, InputSpecError> {
let url = Url::parse(str).map_err(|e| InputSpecError::UrlParseError {
str: str.to_string(),
error: format!("{e}"),
})?;
let client = ClientBuilder::new()
.build()
.map_err(|e| InputSpecError::ClientBuilderError { error: format!("{e}") })?;
Ok(UrlSpec { url, client })
}
pub fn as_str(&self) -> &str {
self.url.as_str()
}
}
impl Display for UrlSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.url)
}
}