use crate::prelude::*;
#[expect(clippy::type_complexity, reason = "It's fine.")]
pub fn domain_segment_to_unicode<'a, T: Into<Cow<'a, str>>>(value: T) -> Result<(Cow<'a, str>, Option<String>, BidiDetail), (Cow<'a, str>, Option<String>)> {
let (_, value) = uts46_map_normalize(value);
uts46_domain_segment_to_unicode(value)
}
#[expect(clippy::type_complexity, reason = "It's fine.")]
pub fn uts46_domain_segment_to_unicode<'a, T: Into<Cow<'a, str>>>(value: T) -> Result<(Cow<'a, str>, Option<String>, BidiDetail), (Cow<'a, str>, Option<String>)> {
let value = value.into();
match value.strip_prefix("xn--") {
None => match BidiDetail::parse_ascii(&value) {
Some(bidi_detail) => Ok((value, None, bidi_detail)),
None => match mostly_validate_uts46_domain_segment_unicode(&value) {
true => match BidiDetail::parse_unicode(&value) {
BidiDetail::ForceAscii => Err((value, None )),
bidi_detail => Ok ((value, None, bidi_detail)),
},
false => Err((value, None)),
},
},
Some(punycode) => match decode_punycode(punycode) {
Ok(decoded) => match mostly_validate_domain_segment_unicode(&decoded) {
true => match BidiDetail::parse_unicode(&decoded) {
BidiDetail::ForceAscii => Err((value, Some(decoded) )),
bidi_detail => Ok ((value, Some(decoded), bidi_detail)),
},
false => Err((value, Some(decoded))),
},
Err(InvalidPunycode) => Err((value, None)),
}
}
}
pub fn unchecked_uts46_domain_segment_to_unicode<'a, T: Into<Cow<'a, str>>>(value: T) -> (Cow<'a, str>, Option<String>) {
let value = value.into();
match value.strip_prefix("xn--") {
None => (value, None),
Some(punycode) => match decode_punycode(punycode) {
Ok (decoded ) => (value, Some(decoded)),
Err(InvalidPunycode) => (value, None ),
}
}
}