#![allow(clippy::module_name_repetitions)]
use crate::error::{Error as IriError, Result as IriResult};
use crate::{Authority, Fragment, Host, Path, Port, Query, Scheme, UserInfo, IRI};
use std::convert::TryFrom;
use std::str::FromStr;
#[derive(Debug)]
pub struct IriBuilder {
scheme: Option<Scheme>,
host: Option<Host>,
user_name: Option<String>,
password: Option<String>,
port: Option<Port>,
path: Option<Path>,
query: Option<Query>,
fragment: Option<Fragment>,
}
impl Default for IriBuilder {
fn default() -> Self {
Self {
scheme: None,
host: None,
user_name: None,
password: None,
port: None,
path: None,
query: None,
fragment: None,
}
}
}
impl TryFrom<&mut IriBuilder> for IRI {
type Error = IriError;
fn try_from(builder: &mut IriBuilder) -> Result<Self, Self::Error> {
let mut iri = match &builder.path {
None => IRI::default(),
Some(path) => IRI::new(path),
};
if let Some(scheme) = &builder.scheme {
iri.set_scheme(Some(scheme.clone()));
}
if let Some(host) = &builder.host {
let mut authority = Authority::new(host.clone());
if let Some(port) = &builder.port {
authority.set_port(port.clone());
}
if let Some(user_name) = &builder.user_name {
let mut user_info = UserInfo::new(user_name)?;
if let Some(password) = &builder.password {
user_info.set_password(password)?;
}
authority.set_user_info(user_info);
}
iri.set_authority(Some(authority));
}
if let Some(query) = &builder.query {
iri.set_query(Some(query.clone()));
}
if let Some(fragment) = &builder.fragment {
iri.set_fragment(Some(fragment.clone()));
}
Ok(iri)
}
}
impl IriBuilder {
pub fn scheme(&mut self, scheme: &Scheme) -> &mut Self {
self.scheme = Some(scheme.clone());
self
}
pub fn scheme_str(&mut self, scheme: &str) -> IriResult<&mut Self> {
Ok(self.scheme(&Scheme::from_str(scheme)?))
}
pub fn host(&mut self, host: &Host) -> &mut Self {
self.host = Some(host.clone());
self
}
pub fn host_str(&mut self, host: &str) -> IriResult<&mut Self> {
Ok(self.host(&Host::from_str(host)?))
}
pub fn port(&mut self, port: Port) -> &mut Self {
self.port = Some(port);
self
}
pub fn user(&mut self, user_name: &str, password: &str) -> &mut Self {
self.user_name = Some(user_name.to_string());
self.password = Some(password.to_string());
self
}
pub fn user_name(&mut self, user_name: &str) -> &mut Self {
self.user_name = Some(user_name.to_string());
self
}
pub fn password(&mut self, password: &str) -> &mut Self {
self.password = Some(password.to_string());
self
}
pub fn path_root(&mut self) -> &mut Self {
self.path = Some(Path::root());
self
}
pub fn path(&mut self, path: &Path) -> &mut Self {
self.path = Some(path.clone());
self
}
pub fn path_str(&mut self, path: &str) -> IriResult<&mut Self> {
self.path = Some(Path::from_str(path)?);
Ok(self)
}
pub fn append_path_segment(&mut self, segment: &str) -> IriResult<&mut Self> {
match &mut self.path {
None => self.path = Some(Path::from_str(segment)?),
Some(path) => path.push(segment)?,
}
Ok(self)
}
pub fn query(&mut self, query: &Query) -> &mut Self {
self.query = Some(query.clone());
self
}
pub fn query_str(&mut self, query: &str) -> IriResult<&mut Self> {
self.query = Some(Query::from_str(query)?);
Ok(self)
}
pub fn fragment(&mut self, fragment: &Fragment) -> &mut Self {
self.fragment = Some(fragment.clone());
self
}
pub fn fragment_str(&mut self, fragment: &str) -> IriResult<&mut Self> {
self.fragment = Some(Fragment::from_str(fragment)?);
Ok(self)
}
}