use crate::prelude::*;
#[derive(Debug, Clone)]
pub struct DomainSegments<'a>(Cow<'a, str>);
impl<'a> DomainSegments<'a> {
pub unsafe fn new_unchecked<T: Into<Cow<'a, str>>>(value: T) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
#[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_segments_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_segments_to_unicode(self.as_str())
}
pub fn iter(&self) -> DomainSegmentsIter<'_> {
self.into_iter()
}
pub fn ends_in_a_number(&self) -> bool {
ends_in_a_number(self.as_str())
}
pub fn last_is_empty(&self) -> bool {
last_is_empty(self.as_str())
}
pub fn last_is_a_number(&self) -> bool {
last_is_a_number(self.as_str())
}
pub fn borrowed(&self) -> DomainSegments<'_> {
DomainSegments(Cow::Borrowed(&self.0))
}
pub fn into_owned(self) -> DomainSegments<'static> {
DomainSegments(self.0.into_owned().into())
}
pub fn into_inner(self) -> Cow<'a, str> {
self.0
}
}
impl<'a> TryFrom<Cow<'a, [u8]>> for DomainSegments<'a> {
type Error = InvalidDomainSegments;
fn try_from(value: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
let (_, value) = make_domain_segments_bytes(value)?;
unsafe {
Ok(Self::new_unchecked(value))
}
}
}
impl<'a> TryFrom<Cow<'a, str>> for DomainSegments<'a> {
type Error = InvalidDomainSegments;
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
let (_, value) = make_domain_segments(value)?;
unsafe {
Ok(Self::new_unchecked(value))
}
}
}
impl<'a> From<DomainSegment<'a>> for DomainSegments<'a> {
fn from(value: DomainSegment<'a>) -> Self {
unsafe {
Self::new_unchecked(value.into_inner())
}
}
}