use crate::prelude::*;
mod segment;
mod segments;
pub use segment::*;
pub use segments::*;
#[cfg(feature = "psl")]
mod prefix;
#[cfg(feature = "psl")]
mod middle;
#[cfg(feature = "psl")]
mod suffix;
mod fqddot;
mod labels;
#[cfg(feature = "psl")]
mod origin;
#[cfg(feature = "psl")]
mod normal;
#[derive(Debug, Clone)]
pub struct DomainHost<'a> {
host: Cow<'a, str>,
details: DomainHostDetails,
}
impl<'a> DomainHost<'a> {
pub unsafe fn new_percent_decoded<T: Into<Cow<'a, str>>>(value: T) -> Result<Self, InvalidDomainHost> {
let (_, value, class) = uts46_classify_map_normalize(value);
Ok(if class & 0b0001_0100 == 0b0001_0000 {
Err(InvalidDomainHost)?
} else if class & 0b0000_0110 == 0b0000_0000 && !value.is_empty() {
unsafe {Self::new_raw(value)}
} else {
unsafe {Self::new_not_eian(value)}?
})
}
pub unsafe fn new_normalized<T: Into<Cow<'a, str>>>(value: T) -> Result<Self, InvalidDomainHost> {
let value = value.into();
if ends_in_a_number(&value) {
Err(InvalidDomainHost)?;
}
unsafe {
Self::new_not_eian(value)
}
}
pub unsafe fn new_not_eian<T: Into<Cow<'a, str>>>(value: T) -> Result<Self, InvalidDomainHost> {
let (raw, ascii) = uts46_domain_host_to_ascii(value, false)?;
let value = ascii.map_or(raw, Into::into);
if value.bytes().any(|b| FORBIDDEN_DOMAIN_HOST_LITERAL.contains(b)) {
Err(InvalidDomainSegment)?;
}
if value.is_empty() {
Err(InvalidDomainHost)?
}
unsafe {
Ok(Self::new_raw(value))
}
}
pub unsafe fn new_raw<T: Into<Cow<'a, str>>>(value: T) -> Self {
let value = value.into();
let details = DomainHostDetails::parse_unchecked(&value);
unsafe {
Self::from_parts(value, details)
}
}
pub unsafe fn from_parts<T: Into<Cow<'a, str>>>(value: T, details: DomainHostDetails) -> Self {
Self {
host: value.into(),
details
}
}
pub fn as_str(&self) -> &str {
&self.host
}
pub fn details(&self) -> DomainHostDetails {
self.details
}
pub fn into_inner(self) -> Cow<'a, str> {
self.host
}
pub fn into_parts(self) -> (Cow<'a, str>, DomainHostDetails) {
(self.host, self.details)
}
#[expect(clippy::type_complexity, reason = "It's fine.")]
pub fn into_unicode(self) -> Result<(Cow<'a, str>, Option<String>, BidiDetail), (Cow<'a, str>, Option<String>)> {
uts46_domain_host_to_unicode(self.into_inner())
}
#[expect(clippy::type_complexity, reason = "It's fine.")]
pub fn to_unicode(&self) -> Result<(Cow<'_, str>, Option<String>, BidiDetail), (Cow<'_, str>, Option<String>)> {
uts46_domain_host_to_unicode(self.as_str())
}
pub fn into_owned(self) -> DomainHost<'static> {
DomainHost {
host : self.host.into_owned().into(),
details: self.details
}
}
pub fn borrowed(&self) -> DomainHost<'_> {
DomainHost {
host : Cow::Borrowed(&self.host),
details: self.details,
}
}
pub fn segments(&self) -> DomainSegmentsIter<'_> {
self.labels_segments()
}
pub fn segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.labels_segment(index)
}
pub fn range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.labels_range(range)
}
pub fn set_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
self.set_labels_segment(index, value)
}
pub fn set_range<'b, T: TryInto<DomainSegments<'b>>, B: RangeBounds<isize>>(&mut self, range: B, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
self.set_labels_range(range, value)
}
pub fn insert_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetDomainError> where SetDomainError: From<T::Error> {
self.insert_labels_segment(index, value)
}
}
impl<'a> TryFrom<Cow<'a, [u8]>> for DomainHost<'a> {
type Error = InvalidDomainHost;
fn try_from(value: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
let (_, value, details) = make_domain_host_bytes(value)?;
unsafe {
Ok(Self::from_parts(value, details))
}
}
}
impl<'a> TryFrom<Cow<'a, str>> for DomainHost<'a> {
type Error = InvalidDomainHost;
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
let (_, value, details) = make_domain_host(value)?;
unsafe {
Ok(Self::from_parts(value, details))
}
}
}
impl<'a> TryFrom<DomainSegment<'a>> for DomainHost<'a> {
type Error = DomainSegment<'a>;
fn try_from(value: DomainSegment<'a>) -> Result<Self, Self::Error> {
match value.is_a_number() {
true => Err(value),
false => Ok(Self {
details: DomainHostDetails {
data1: 0,
data2: 0,
},
host: value.into_inner()
})
}
}
}
impl<'a> TryFrom<Host<'a>> for DomainHost<'a> {
type Error = Host<'a>;
fn try_from(value: Host<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Host::Domain(x) => x,
x => Err(x)?,
})
}
}
impl<'a> TryFrom<FileHost<'a>> for DomainHost<'a> {
type Error = FileHost<'a>;
fn try_from(value: FileHost<'a>) -> Result<Self, Self::Error> {
if let FileHost::Domain(x) = value {
Ok(x)
} else {
Err(value)
}
}
}
impl<'a> TryFrom<SpecialNotFileHost<'a>> for DomainHost<'a> {
type Error = SpecialNotFileHost<'a>;
fn try_from(value: SpecialNotFileHost<'a>) -> Result<Self, Self::Error> {
if let SpecialNotFileHost::Domain(x) = value {
Ok(x)
} else {
Err(value)
}
}
}
impl<'a> TryFrom<NonSpecialHost<'a>> for DomainHost<'a> {
type Error = NonSpecialHost<'a>;
fn try_from(value: NonSpecialHost<'a>) -> Result<Self, Self::Error> {
Err(value)
}
}