use crate::prelude::*;
#[derive(Debug, Clone)]
pub enum SpecialNotFileHost<'a> {
Domain(DomainHost<'a>),
Ipv4 (Ipv4Host <'a>),
Ipv6 (Ipv6Host <'a>),
}
impl<'a> SpecialNotFileHost<'a> {
pub unsafe fn from_parts<T: Into<Cow<'a, str>>>(value: T, details: SpecialNotFileHostDetails) -> Self {
unsafe {
match details {
SpecialNotFileHostDetails::Domain(x) => DomainHost::from_parts(value, x).into(),
SpecialNotFileHostDetails::Ipv4 (x) => Ipv4Host ::from_parts(value, x).into(),
SpecialNotFileHostDetails::Ipv6 (x) => Ipv6Host ::from_parts(value, x).into(),
}
}
}
pub fn new_domain<T: TryInto<DomainHost<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_into()?.into())
}
pub fn new_ipv4<T: TryInto<Ipv4Host<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_into()?.into())
}
pub fn new_ipv6<T: TryInto<Ipv6Host<'a>>>(value: T) -> Result<Self, T::Error> {
Ok(value.try_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(),
}
}
pub fn details(&self) -> SpecialNotFileHostDetails {
match self {
Self::Domain(x) => x.details().into(),
Self::Ipv4 (x) => x.details().into(),
Self::Ipv6 (x) => x.details().into(),
}
}
pub fn borrowed(&self) -> SpecialNotFileHost<'_> {
match self {
Self::Domain(x) => x.borrowed().into(),
Self::Ipv4 (x) => x.borrowed().into(),
Self::Ipv6 (x) => x.borrowed().into(),
}
}
pub fn into_owned(self) -> SpecialNotFileHost<'static> {
match self {
Self::Domain(x) => x.into_owned().into(),
Self::Ipv4 (x) => x.into_owned().into(),
Self::Ipv6 (x) => x.into_owned().into(),
}
}
pub fn into_parts(self) -> (Cow<'a, str>, SpecialNotFileHostDetails) {
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())}
}
}
}
impl<'a> TryFrom<Cow<'a, str>> for SpecialNotFileHost<'a> {
type Error = InvalidSpecialNotFileHost;
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
let (_, value, details) = make_special_not_file_host(value)?;
Ok(unsafe {
Self::from_parts(value, details)
})
}
}
impl<'a> TryFrom<Cow<'a, [u8]>> for SpecialNotFileHost<'a> {
type Error = InvalidSpecialNotFileHost;
fn try_from(value: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
let (_, value, details) = make_special_not_file_host_bytes(value)?;
Ok(unsafe {
Self::from_parts(value, details)
})
}
}
impl<'a> TryFrom<Host<'a>> for SpecialNotFileHost<'a> {
type Error = Host<'a>;
fn try_from(value: Host<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Host::Domain(x) => x.into(),
Host::Ipv6 (x) => x.into(),
Host::Ipv4 (x) => x.into(),
Host::Opaque(x) => x.try_into()?,
Host::Empty (x) => x.try_into()?,
})
}
}
impl<'a> TryFrom<FileHost<'a>> for SpecialNotFileHost<'a> {
type Error = FileHost<'a>;
fn try_from(value: FileHost<'a>) -> Result<Self, Self::Error> {
Ok(match value {
FileHost::Domain(x) => x.into(),
FileHost::Ipv6 (x) => x.into(),
FileHost::Ipv4 (x) => x.into(),
FileHost::Empty (x) => x.try_into()?,
})
}
}
impl<'a> TryFrom<NonSpecialHost<'a>> for SpecialNotFileHost<'a> {
type Error = NonSpecialHost<'a>;
fn try_from(value: NonSpecialHost<'a>) -> Result<Self, Self::Error> {
Ok(match value {
NonSpecialHost::Ipv6 (x) => x.into(),
NonSpecialHost::Opaque(x) => x.try_into()?,
NonSpecialHost::Empty (x) => x.try_into()?,
})
}
}
impl<'a> From<DomainHost<'a>> for SpecialNotFileHost<'a> {fn from(value: DomainHost<'a>) -> Self {Self::Domain(value)}}
impl<'a> From<Ipv4Host <'a>> for SpecialNotFileHost<'a> {fn from(value: Ipv4Host <'a>) -> Self {Self::Ipv4 (value)}}
impl<'a> From<Ipv6Host <'a>> for SpecialNotFileHost<'a> {fn from(value: Ipv6Host <'a>) -> Self {Self::Ipv6 (value)}}
impl<'a> TryFrom<OpaqueHost<'a>> for SpecialNotFileHost<'a> {
type Error = OpaqueHost<'a>;
fn try_from(value: OpaqueHost<'a>) -> Result<Self, Self::Error> {
Err(value)
}
}
impl<'a> TryFrom<EmptyHost<'a>> for SpecialNotFileHost<'a> {
type Error = EmptyHost<'a>;
fn try_from(value: EmptyHost<'a>) -> Result<Self, Self::Error> {
Err(value)
}
}