use crate::prelude::*;
#[derive(Debug, Clone)]
pub struct DomainSegment<'a>(Cow<'a, str>);
impl<'a> DomainSegment<'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_segment_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_segment_to_unicode(self.as_str())
}
pub fn is_a_number(&self) -> bool {
is_a_number(&self.0)
}
pub fn into_inner(self) -> Cow<'a, str> {
self.0
}
pub fn borrowed(&self) -> DomainSegment<'_> {
DomainSegment(Cow::Borrowed(&self.0))
}
pub fn into_owned(self) -> DomainSegment<'static> {
DomainSegment(self.0.into_owned().into())
}
}
impl<'a> TryFrom<Cow<'a, [u8]>> for DomainSegment<'a> {
type Error = InvalidDomainSegment;
fn try_from(value: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
let (_, value) = make_domain_segment_bytes(value)?;
unsafe {
Ok(Self::new_unchecked(value))
}
}
}
impl<'a> TryFrom<Cow<'a, str>> for DomainSegment<'a> {
type Error = InvalidDomainSegment;
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
let (_, value) = make_domain_segment(value)?;
unsafe {
Ok(Self::new_unchecked(value))
}
}
}