use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use crate::prelude::*;
#[derive(Debug, Clone)]
pub enum Host<'a> {
Domain(DomainHost<'a>),
Ipv4 (Ipv4Host <'a>),
Ipv6 (Ipv6Host <'a>),
Opaque(OpaqueHost<'a>),
Empty (EmptyHost <'a>),
}
impl<'a> Host<'a> {
pub unsafe fn from_parts<T: Into<Cow<'a, str>>>(value: T, details: HostDetails) -> Self {
unsafe {
match details {
HostDetails::Domain(x) => DomainHost::from_parts(value, x).into(),
HostDetails::Ipv4 (x) => Ipv4Host ::from_parts(value, x).into(),
HostDetails::Ipv6 (x) => Ipv6Host ::from_parts(value, x).into(),
HostDetails::Opaque(x) => OpaqueHost::from_parts(value, x).into(),
HostDetails::Empty (x) => EmptyHost ::from_parts(value, x).into(),
}
}
}
pub fn new<T: TryInto<FileHost<'a>> + TryInto<SpecialNotFileHost<'a>> + TryInto<NonSpecialHost<'a>>>(value: T, scheme_type: SchemeType) -> Result<Self, InvalidHost> {
Ok(match scheme_type {
SchemeType::File => Self::new_file (value).map_err(|_| InvalidHost)?,
SchemeType::SpecialNotFile => Self::new_special_not_file(value).map_err(|_| InvalidHost)?,
SchemeType::NonSpecial => Self::new_non_special (value).map_err(|_| InvalidHost)?,
})
}
pub fn new_file<T: TryInto<FileHost<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_into()?.into())
}
pub fn new_special_not_file<T: TryInto<SpecialNotFileHost<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_into()?.into())
}
pub fn new_non_special<T: TryInto<NonSpecialHost<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_into()?.into())
}
pub fn new_domain<T: TryInto<DomainHost<'a>>>(value: T) -> Result<Self, T::Error> {
value.try_into().map(Into::into)
}
pub fn new_ipv4<T: TryInto<DomainHost<'a>>>(value: T) -> Result<Self, T::Error> {
value.try_into().map(Into::into)
}
pub fn new_ipv6<T: TryInto<Ipv6Host<'a>>>(value: T) -> Result<Self, T::Error> {
value.try_into().map(Into::into)
}
pub fn new_opaque<T: TryInto<OpaqueHost<'a>>>(value: T) -> Result<Self, T::Error> {
value.try_into().map(Into::into)
}
pub fn new_empty<T: TryInto<EmptyHost<'a>>>(value: T) -> Result<Self, T::Error> {
value.try_into().map(Into::into)
}
pub fn as_str(&self) -> &str {
match self {
Self::Domain(x) => x.as_str(),
Self::Ipv4 (x) => x.as_str(),
Self::Ipv6 (x) => x.as_str(),
Self::Opaque(x) => x.as_str(),
Self::Empty (x) => x.as_str(),
}
}
pub fn details(&self) -> HostDetails {
match self {
Self::Domain(x) => x.details().into(),
Self::Ipv4 (x) => x.details().into(),
Self::Ipv6 (x) => x.details().into(),
Self::Opaque(x) => x.details().into(),
Self::Empty (x) => x.details().into(),
}
}
pub fn into_parts(self) -> (Cow<'a, str>, HostDetails) {
match self {
Self::Domain(x) => {let (host, details) = x.into_parts(); (host, details.into())},
Self::Ipv4 (x) => {let (host, details) = x.into_parts(); (host, details.into())},
Self::Ipv6 (x) => {let (host, details) = x.into_parts(); (host, details.into())},
Self::Opaque(x) => {let (host, details) = x.into_parts(); (host, details.into())},
Self::Empty (x) => {let (host, details) = x.into_parts(); (host, details.into())},
}
}
pub fn into_owned(self) -> Host<'static> {
match self {
Self::Domain(x) => x.into_owned().into(),
Self::Ipv4 (x) => x.into_owned().into(),
Self::Ipv6 (x) => x.into_owned().into(),
Self::Opaque(x) => x.into_owned().into(),
Self::Empty (x) => x.into_owned().into(),
}
}
pub fn borrowed(&self) -> Host<'_> {
match self {
Self::Domain(x) => x.borrowed().into(),
Self::Ipv4 (x) => x.borrowed().into(),
Self::Ipv6 (x) => x.borrowed().into(),
Self::Opaque(x) => x.borrowed().into(),
Self::Empty (x) => x.borrowed().into(),
}
}
pub fn is_domain(&self) -> bool {matches!(self, Self::Domain(_))}
pub fn is_ipv4 (&self) -> bool {matches!(self, Self::Ipv4 (_))}
pub fn is_ipv6 (&self) -> bool {matches!(self, Self::Ipv6 (_))}
pub fn is_opaque(&self) -> bool {matches!(self, Self::Opaque(_))}
pub fn as_domain(&self) -> Option<DomainHost<'_>> {self.borrowed().domain()}
pub fn as_ipv4 (&self) -> Option<Ipv4Host <'_>> {self.borrowed().ipv4 ()}
pub fn as_ipv6 (&self) -> Option<Ipv6Host <'_>> {self.borrowed().ipv6 ()}
pub fn as_opaque(&self) -> Option<OpaqueHost<'_>> {self.borrowed().opaque()}
pub fn as_empty (&self) -> Option<EmptyHost <'_>> {self.borrowed().empty ()}
pub fn domain(self) -> Option<DomainHost<'a>> {self.try_into().ok()}
pub fn ipv4 (self) -> Option<Ipv4Host <'a>> {self.try_into().ok()}
pub fn ipv6 (self) -> Option<Ipv6Host <'a>> {self.try_into().ok()}
pub fn opaque(self) -> Option<OpaqueHost<'a>> {self.try_into().ok()}
pub fn empty (self) -> Option<EmptyHost <'a>> {self.try_into().ok()}
#[cfg(feature = "psl")] pub fn domain_prefix_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.prefix_thing()?])}
#[cfg(feature = "psl")] pub fn domain_middle_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.middle_thing()?])}
#[cfg(feature = "psl")] pub fn domain_suffix_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.suffix_thing() ])}
#[cfg(feature = "psl")] pub fn domain_labels_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.labels_thing() ])}
#[cfg(feature = "psl")] pub fn domain_origin_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.origin_thing()?])}
#[cfg(feature = "psl")] pub fn domain_normal_str(&self) -> Option<&str> {Some(&self.as_str()[self.as_domain()?.normal_thing() ])}
#[cfg(feature = "psl")] pub fn domain_prefix(&self) -> Option<DomainSegments<'_>> {Some(unsafe {DomainSegments::new_unchecked(self.domain_prefix_str()?)})}
#[cfg(feature = "psl")] pub fn domain_middle(&self) -> Option<DomainSegment <'_>> {Some(unsafe {DomainSegment ::new_unchecked(self.domain_middle_str()?)})}
#[cfg(feature = "psl")] pub fn domain_suffix(&self) -> Option<DomainSegments<'_>> {Some(unsafe {DomainSegments::new_unchecked(self.domain_suffix_str()?)})}
#[cfg(feature = "psl")] pub fn domain_labels(&self) -> Option<DomainSegments<'_>> {Some(unsafe {DomainSegments::new_unchecked(self.domain_labels_str()?)})}
#[cfg(feature = "psl")] pub fn domain_origin(&self) -> Option<DomainSegments<'_>> {Some(unsafe {DomainSegments::new_unchecked(self.domain_origin_str()?)})}
#[cfg(feature = "psl")] pub fn domain_normal(&self) -> Option<DomainSegments<'_>> {Some(unsafe {DomainSegments::new_unchecked(self.domain_normal_str()?)})}
}
impl<'a> From<DomainHost<'a>> for Host<'a> {fn from(value: DomainHost<'a>) -> Self {Self::Domain(value)}}
impl<'a> From<Ipv4Host <'a>> for Host<'a> {fn from(value: Ipv4Host <'a>) -> Self {Self::Ipv4 (value)}}
impl<'a> From<Ipv6Host <'a>> for Host<'a> {fn from(value: Ipv6Host <'a>) -> Self {Self::Ipv6 (value)}}
impl<'a> From<OpaqueHost<'a>> for Host<'a> {fn from(value: OpaqueHost<'a>) -> Self {Self::Opaque(value)}}
impl<'a> From<EmptyHost <'a>> for Host<'a> {fn from(value: EmptyHost <'a>) -> Self {Self::Empty (value)}}
impl<'a> From<FileHost<'a>> for Host<'a> {
fn from(value: FileHost<'a>) -> Self {
match value {
FileHost::Domain(x) => x.into(),
FileHost::Ipv4 (x) => x.into(),
FileHost::Ipv6 (x) => x.into(),
FileHost::Empty (x) => x.into(),
}
}
}
impl<'a> From<SpecialNotFileHost<'a>> for Host<'a> {
fn from(value: SpecialNotFileHost<'a>) -> Self {
match value {
SpecialNotFileHost::Domain(x) => x.into(),
SpecialNotFileHost::Ipv4 (x) => x.into(),
SpecialNotFileHost::Ipv6 (x) => x.into(),
}
}
}
impl<'a> From<NonSpecialHost<'a>> for Host<'a> {
fn from(value: NonSpecialHost<'a>) -> Self {
match value {
NonSpecialHost::Ipv6 (x) => x.into(),
NonSpecialHost::Opaque(x) => x.into(),
NonSpecialHost::Empty (x) => x.into(),
}
}
}
impl From<Ipv4Addr> for Host<'static> {fn from(value: Ipv4Addr) -> Self {Self::Ipv4(value.into())}}
impl From<Ipv6Addr> for Host<'static> {fn from(value: Ipv6Addr) -> Self {Self::Ipv6(value.into())}}
impl From<IpAddr> for Host<'static> {
fn from(value: IpAddr) -> Self {
match value {
IpAddr::V4(x) => x.into(),
IpAddr::V6(x) => x.into(),
}
}
}