better-url 0.12.0

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

use crate::prelude::*;

impl DomainHost<'_> {
    /// If it has an origin.
    pub fn has_origin(&self) -> bool {
        self.has_middle()
    }



    /// The [`Range::start`] of the origin.
    fn origin_start(&self) -> Option<usize> {
        self.middle_start()
    }

    /// The [`Range::end`] of the origin.
    fn origin_after(&self) -> Option<usize> {
        self.has_middle().then_some(self.suffix_after())
    }

    /// The [`Range`] of the origin.
    pub(crate) fn origin_thing(&self) -> Option<Range<usize>> {
        Some(self.origin_start()? .. self.origin_after()?)
    }



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

    /// The origin as a [`DomainSegments`].
    pub fn origin(&self) -> Option<DomainSegments<'_>> {
        Some(unsafe {DomainSegments::new_unchecked(self.origin_str()?)})
    }



    /// The origin segments as [`str`]s.
    pub fn origin_segment_strs(&self) -> Option<SplitDots<'_>> {
        Some(SplitDots(Some(self.origin_str()?)))
    }

    /// The origin segments as [`DomainSegment`]s.
    pub fn origin_segments(&self) -> Option<DomainSegmentsIter<'_>> {
        Some(unsafe {DomainSegmentsIter::new_unchecked(self.origin_segment_strs()?)})
    }



    /// The `index`th origin segment as a [`str`].
    pub fn origin_segment_str(&self, index: isize) -> Option<&str> {
        self.origin_segment_strs()?.neg_nth(index)
    }

    /// The `index`th origin segment as a [`DomainSegment`].
    pub fn origin_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
        self.origin_segments()?.neg_nth(index)
    }



    /// The range of origin segments as a [`str`].
    pub fn origin_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
        self.origin_segments()?.range_str(range)
    }

    /// The range of origin segments as a [`DomainSegments`].
    pub fn origin_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
        self.origin_segments()?.range(range)
    }



    /// Set the origin.
    /// # Errors
    /// See [`Self`]'s documentation.
    pub fn set_origin<'b, T: TryInto<DomainSegments<'b>>>(&mut self, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
        let replace = self.origin_str().unwrap_or(self.suffix_str());

        match value.map(TryInto::try_into).transpose()? {
            Some(new) if replace == new => return Ok(false),

            Some(new) => match self.host.split_around_substr(replace) {
                ("", "" ) if new.is_empty        () => Err(CantBeEmpty)?,
                (_ , "" ) if new.last_is_empty   () => Err(NonFqdnCantEndInEmpty)?,
                (_ , "" ) if new.ends_in_a_number() => Err(CantEndInANumber)?,
                (_ , ".") if new.last_is_a_number() => Err(CantEndInANumber)?,

                _ => self.host.replace_substr(replace, new.as_str())
            },

            None => match self.host.split_around_substr(replace) {
                ("", "" | ".")                        => Err(CantBeEmpty)?,
                (x , ""      ) if ends_in_empty   (x) => Err(NonFqdnCantEndInEmpty)?,
                (x , ""      ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
                (x , "."     ) if last_is_a_number(x) => Err(CantEndInANumber)?,

                (x , "" | ".") => self.host.replace_range(x.len() - 1 ..                         , ""),
                (x , _       ) => self.host.replace_range(x.len()     ..= x.len() + replace.len(), ""),
            }
        }

        self.details = DomainHostDetails::parse_unchecked(&self.host);

        Ok(true)
    }

    /// Set, insert, or remove the `index`th origin segment.
    /// # Errors
    /// See [`Self`]'s documentation.
    pub fn set_origin_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
        let temp1 = self.origin_str().unwrap_or(self.suffix_str());

        let insert_start = temp1.addr    () - self.host.addr();
        let insert_after = temp1.end_addr() - self.host.addr();

        match (temp1.split('.').try_neg_nth(index), value.map(TryInto::try_into).transpose()?) {
            (Ok(old), Some(new)) if old == new => return Ok(false),

            (Err(1..), Some(_)) => Err(InsertNotFound )?,
            (Err(_  ), None   ) => return Ok(false),

            (Ok(old), Some(new)) => match self.host.split_around_substr(old) {
                ("", ""      ) if new.is_empty   () => Err(CantBeEmpty)?,
                (_ , ""      ) if new.is_empty   () => Err(NonFqdnCantEndInEmpty)?,
                (_ , "" | ".") if new.is_a_number() => Err(CantEndInANumber)?,

                _ => self.host.replace_substr(old, new.as_str()),
            },

            (Ok(old), None) => match self.host.split_around_substr(old) {
                ("", "" | ".")                        => Err(CantBeEmpty)?,
                (x , ""      ) if ends_in_empty   (x) => Err(NonFqdnCantEndInEmpty)?,
                (x , ""      ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
                (x , "."     ) if last_is_a_number(x) => Err(CantEndInANumber)?,

                (x , "" | ".") => self.host.replace_range(x.len() - 1 ..                     , ""),
                (x , _       ) => self.host.replace_range(x.len()     ..= x.len() + old.len(), ""),
            },

            (Err(0), Some(new)) => match index {
                0.. if new.is_a_number() => Err(CantEndInANumber)?,

                0.. => self.host.insert_with(insert_after, [".", new.as_str()]),
                ..0 => self.host.insert_with(insert_start, [new.as_str(), "."]),
            },
        }

        self.details = DomainHostDetails::parse_unchecked(&self.host);

        Ok(true)
    }

    /// Set the `range` origin segments.
    /// # Errors
    /// See [`Self`]'s documentation.
    pub fn set_origin_range<'b, T: TryInto<DomainSegments<'b>>, B: RangeBounds<isize>>(&mut self, range: B, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
        let old = self.origin_range(range).ok_or(RangeNotFound)?;
        let new = value.map(TryInto::try_into).transpose()?;

        match new {
            Some(new) if old == new => return Ok(false),

            Some(new) => match self.host.split_around_substr(old.as_str()) {
                ("", "" ) if new.is_empty()         => Err(CantBeEmpty)?,
                (_ , "" ) if new.last_is_empty()    => Err(NonFqdnCantEndInEmpty)?,
                (_ , "" ) if new.ends_in_a_number() => Err(CantEndInANumber)?,
                (_ , ".") if new.last_is_a_number() => Err(CantEndInANumber)?,

                _ => self.host.replace_substr(old.as_str(), new.as_str())
            },
            None => match self.host.split_around_substr(old.as_str()) {
                ("", "" | ".")                        => Err(CantBeEmpty)?,
                (x , ""      ) if ends_in_empty   (x) => Err(NonFqdnCantEndInEmpty)?,
                (x , ""      ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
                (x , "."     ) if last_is_a_number(x) => Err(CantEndInANumber)?,

                (x , "" | ".") => self.host.replace_range(x.len() - 1 ..                     , ""),
                (x , _       ) => self.host.replace_range(x.len()     ..= x.len() + old.len(), ""),
            }
        }

        self.details = DomainHostDetails::parse_unchecked(&self.host);

        Ok(true)
    }

    /// Insert a new `index`th origin segment.
    /// # Errors
    /// See [`Self`]'s documentation.
    pub fn insert_origin_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetDomainError> where SetDomainError: From<T::Error> {
        let new = value.try_into()?;

        let temp1 = self.origin_str().unwrap_or(self.suffix_str());

        let insert_start = temp1.addr    () - self.host.addr();
        let insert_after = temp1.end_addr() - self.host.addr();

        let temp2 = temp1.split('.').map(|x| self.host.my_substr_range(x)).try_neg_nth(index);

        match (temp2, index) {
            (Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.is_empty   () => Err(NonFqdnCantEndInEmpty)?,
            (Ok(_), -1) | (Err(0), 0..) if                    new.is_a_number() => Err(CantEndInANumber)?,

            (Err(1..), _) => Err(InsertNotFound)?,

            (Ok(Range {start, ..}), 0..) => self.host.insert_with(start, [new.as_str(), "."]),
            (Ok(Range {end  , ..}), ..0) => self.host.insert_with(end  , [".", new.as_str()]),

            (Err(0), 0..) => self.host.insert_with(insert_after, [".", new.as_str()]),
            (Err(0), ..0) => self.host.insert_with(insert_start, [new.as_str(), "."]),
        }

        self.details = DomainHostDetails::parse_unchecked(&self.host);

        Ok(())
    }

    /// Insert new segments starting at the `index`th origin segment.
    /// # Errors
    /// See [`Self`]'s documentation.
    pub fn insert_origin_segments<'b, T: TryInto<DomainSegments<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetDomainError> where SetDomainError: From<T::Error> {
        let new = value.try_into()?;

        let temp1 = self.origin_str().unwrap_or(self.suffix_str());

        let insert_start = temp1.addr    () - self.host.addr();
        let insert_after = temp1.end_addr() - self.host.addr();

        let temp2 = temp1.split('.').map(|x| self.host.my_substr_range(x)).try_neg_nth(index);

        match (temp2, index) {
            (Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.last_is_empty   () => Err(NonFqdnCantEndInEmpty)?,
            (Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.ends_in_a_number() => Err(CantEndInANumber)?,
            (Ok(_), -1) | (Err(0), 0..) if  self.is_fqdn() && new.last_is_a_number() => Err(CantEndInANumber)?,

            (Err(1..), _) => Err(InsertNotFound)?,

            (Ok(Range {start, ..}), 0..) => self.host.insert_with(start, [new.as_str(), "."]),
            (Ok(Range {end  , ..}), ..0) => self.host.insert_with(end  , [".", new.as_str()]),

            (Err(0), 0..) => self.host.insert_with(insert_after, [".", new.as_str()]),
            (Err(0), ..0) => self.host.insert_with(insert_start, [new.as_str(), "."]),
        }

        self.details = DomainHostDetails::parse_unchecked(&self.host);

        Ok(())
    }
}