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