better-url 0.12.0

A better URL crate.
Documentation
//! Host stuff.

use crate::prelude::*;

mod domain;
mod and_port;

impl BetterUrl {
    /// If it has a host.
    pub fn has_host(&self) -> bool {
        self.details.host_start.is_some()
    }

    /// The [`Range::start`] of the host.
    fn host_start(&self) -> Option<usize> {
        Some(self.details.host_start?.get() as usize)
    }

    /// The [`Range::end`] of the host.
    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
        }
    }

    /// The [`Range`] of the host.
    fn host_range(&self) -> Option<Range<usize>> {
        Some(self.host_start()? .. self.host_after()?)
    }



    /// The host as a [`str`].
    pub fn host_str(&self) -> Option<&str> {
        Some(unsafe {self.as_str().get_unchecked(self.host_range()?)})
    }

    /// The [`Host`].
    pub fn host(&self) -> Option<Host<'_>> {
        unsafe {Some(Host::from_parts(self.host_str()?, self.host_details()?))}
    }



    /** The [`HostDetails`].       */ pub fn host_details       (&self) -> Option<HostDetails      > {self.details.host_details  ()}
    /** The [`DomainHostDetails`]. */ pub fn domain_details     (&self) -> Option<DomainHostDetails> {self.details.domain_details()}
    /** The [`Ipv4HostDetails`].   */ pub fn ipv4_details       (&self) -> Option<Ipv4HostDetails  > {self.details.ipv4_details  ()}
    /** The [`Ipv6HostDetails`].   */ pub fn ipv6_details       (&self) -> Option<Ipv6HostDetails  > {self.details.ipv6_details  ()}
    /** The [`OpaqueHostDetails`]. */ pub fn opaque_host_details(&self) -> Option<OpaqueHostDetails> {self.details.opaque_details()}
    /** The [`EmptyHostDetails`].  */ pub fn empty_host_details (&self) -> Option<EmptyHostDetails > {self.details.empty_details ()}

    /** If the host is [`DomainHost`]. */ pub fn host_is_domain(&self) -> bool {self.details.host_is_domain()}
    /** If the host is [`Ipv4Host`].   */ pub fn host_is_ipv4  (&self) -> bool {self.details.host_is_ipv4  ()}
    /** If the host is [`Ipv6Host`].   */ pub fn host_is_ipv6  (&self) -> bool {self.details.host_is_ipv6  ()}
    /** If the host is [`OpaqueHost`]. */ pub fn host_is_opaque(&self) -> bool {self.details.host_is_opaque()}
    /** If the host is [`EmptyHost`].  */ pub fn host_is_empty (&self) -> bool {self.details.host_is_empty ()}



    /// Set the host.
    /// # Errors
    /// If [`Host::new`] returns an error, that error is returned.
    ///
    /// If [`Self::cannot_be_a_base`] returns true, returns the error [`CantHaveHost`].
    ///
    /// If the URL would become too long, returns the error [`TooLong`].
    ///
    /// If the URL has a visible userinfo and/or port and the host is empty, returns the error [`CantBeNone`].
    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(())
    }
}