use crate::prelude::*;
mod domain;
mod and_port;
impl BetterUrl {
pub fn has_host(&self) -> bool {
self.details.host_start.is_some()
}
fn host_start(&self) -> Option<usize> {
Some(self.details.host_start?.get() as usize)
}
pub(crate) fn host_after(&self) -> Option<usize> {
if self.details.host_start.is_some() {
Some(self.details.port_mark.map_or(self.details.path_start, NonZero::get) as usize)
} else {
None
}
}
fn host_range(&self) -> Option<Range<usize>> {
Some(self.host_start()? .. self.host_after()?)
}
pub fn host_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.host_range()?)})
}
pub fn host(&self) -> Option<Host<'_>> {
unsafe {Some(Host::from_parts(self.host_str()?, self.host_details()?))}
}
pub fn host_details (&self) -> Option<HostDetails > {self.details.host_details ()}
pub fn domain_details (&self) -> Option<DomainHostDetails> {self.details.domain_details()}
pub fn ipv4_details (&self) -> Option<Ipv4HostDetails > {self.details.ipv4_details ()}
pub fn ipv6_details (&self) -> Option<Ipv6HostDetails > {self.details.ipv6_details ()}
pub fn opaque_host_details(&self) -> Option<OpaqueHostDetails> {self.details.opaque_details()}
pub fn empty_host_details (&self) -> Option<EmptyHostDetails > {self.details.empty_details ()}
pub fn host_is_domain(&self) -> bool {self.details.host_is_domain()}
pub fn host_is_ipv4 (&self) -> bool {self.details.host_is_ipv4 ()}
pub fn host_is_ipv6 (&self) -> bool {self.details.host_is_ipv6 ()}
pub fn host_is_opaque(&self) -> bool {self.details.host_is_opaque()}
pub fn host_is_empty (&self) -> bool {self.details.host_is_empty ()}
pub fn set_host<'a, T: TryInto<FileHost<'a>> + TryInto<SpecialNotFileHost<'a>> + TryInto<NonSpecialHost<'a>>>(&mut self, value: T) -> Result<(), SetHostError> {
let new = Host::new(value, self.scheme_type())?;
if self.cannot_be_a_base() {
Err(CantHaveHost)?;
}
match self.host_range() {
None => {
if self.details.path_start == self.details.scheme_mark + 3 {
let diff = new.len();
if self.len() + diff > u32::MAX as usize {
Err(TooLong)?;
}
unsafe {
self.serialization.modify(|x| {
x.as_mut_vec()[self.details.scheme_mark as usize + 2] = b'/';
x.insert_str(self.details.scheme_mark as usize + 3, new.as_str());
});
};
self.details.set_host_details(Some(new.details()));
self.details.host_start = NonZero::new(self.details.scheme_mark + 3);
self.details.path_start += diff as u32;
if let Some(x) = self.details.port_mark {self.details.port_mark = NonZero::new(x.get() + diff as u32);}
if let Some(x) = self.details.query_mark {self.details.query_mark = NonZero::new(x.get() + diff as u32);}
if let Some(x) = self.details.fragment_mark {self.details.fragment_mark = NonZero::new(x.get() + diff as u32);}
} else {
let diff = new.len() + 2;
if self.len() + diff > u32::MAX as usize {
Err(TooLong)?;
}
unsafe {
self.serialization.modify(|x| x.insert_with(self.details.scheme_mark as usize + 1, ["//", new.as_str()]));
}
self.details.set_host_details(Some(new.details()));
self.details.host_start = NonZero::new(self.details.scheme_mark + 3);
self.details.path_start += diff as u32;
if let Some(x) = self.details.port_mark {self.details.port_mark = NonZero::new(x.get() + diff as u32);}
if let Some(x) = self.details.query_mark {self.details.query_mark = NonZero::new(x.get() + diff as u32);}
if let Some(x) = self.details.fragment_mark {self.details.fragment_mark = NonZero::new(x.get() + diff as u32);}
}
},
Some(range) => {
if new.is_empty() && (self.details.username_after.is_some() || range.end != self.details.path_start as usize) {
Err(CantBeNone)?;
}
let start_len = self.len();
let after_len = self.len() - range.len() + new.len();
if after_len > u32::MAX as usize {
Err(TooLong)?;
}
let diff = (after_len as u32).wrapping_sub(start_len as u32);
unsafe {
self.serialization.modify(|x| x.as_mut_vec().replace_range_unchecked(range.clone(), new.as_str().as_bytes()));
}
self.details.set_host_details(Some(new.details()));
self.details.path_start = self.details.path_start.wrapping_add(diff);
if let Some(x) = self.details.port_mark {self.details.port_mark = NonZero::new(x.get().wrapping_add(diff));}
if let Some(x) = self.details.query_mark {self.details.query_mark = NonZero::new(x.get().wrapping_add(diff));}
if let Some(x) = self.details.fragment_mark {self.details.fragment_mark = NonZero::new(x.get().wrapping_add(diff));}
},
}
Ok(())
}
}