better-url 0.12.0

A better URL crate.
Documentation
//! [`OpaqueHost`].

use crate::prelude::*;

/// An opaque host.
///
/// Please note that while the [opaque host parser](https://url.spec.whatwg.org/#concept-opaque-host-parser) implicitly allows the empty string,
/// the [opaque host type itself](https://url.spec.whatwg.org/#opaque-host) is specified to not be empty.
///
/// Therefore, empty opaque hosts are rejected.
///
/// See [servo/rust-url#1112](https://github.com/servo/rust-url/issues/1112) and [whatwg/url#908](https://github.com/whatwg/url/issues/908) for discussion.
#[derive(Debug, Clone)]
pub struct OpaqueHost<'a> {
    /// The host.
    host: Cow<'a, str>,
    /// The [`OpaqueHostDetails`].
    details: OpaqueHostDetails
}

impl<'a> OpaqueHost<'a> {
    /// Make a new [`Self`] with zero validity checks.
    /// # Safety
    /// `value` must be a valid opaque host literal and `details` must be its [`OpaqueHostDetails`].
    pub unsafe fn from_parts<T: Into<Cow<'a, str>>>(value: T, details: OpaqueHostDetails) -> Self {
        Self {
            host: value.into(),
            details,
        }
    }

    /// The host as a [`str`].
    pub fn as_str(&self) -> &str {
        &self.host
    }

    /// The [`OpaqueHostDetails`].
    pub fn details(&self) -> OpaqueHostDetails {
        self.details
    }

    /// Turn into the inner [`Cow`] and [`OpaqueHostDetails`].
    pub fn into_parts(self) -> (Cow<'a, str>, OpaqueHostDetails) {
        (self.host, self.details)
    }



    /// Make a borrowing [`Self`].
    pub fn borrowed(&self) -> OpaqueHost<'_> {
        OpaqueHost {
            host: Cow::Borrowed(&self.host),
            details: self.details
        }
    }

    /// Turn into an owned [`Self`].
    pub fn into_owned(self) -> OpaqueHost<'static> {
        OpaqueHost {
            host: self.host.into_owned().into(),
            details: self.details
        }
    }
}



impl<'a> TryFrom<Cow<'a, [u8]>> for OpaqueHost<'a> {
    type Error = InvalidOpaqueHost;

    fn try_from(value: Cow<'a, [u8]>) -> Result<Self, Self::Error> {
        let (_, host, details) = make_opaque_host_bytes(value)?;

        Ok(unsafe {
            Self::from_parts(host, details)
        })
    }
}



impl<'a> TryFrom<Host<'a>> for OpaqueHost<'a> {
    type Error = Host<'a>;

    fn try_from(value: Host<'a>) -> Result<Self, Self::Error> {
        Ok(match value {
            Host::Opaque(x) => x,
            x               => Err(x)?,
        })
    }
}

impl<'a> TryFrom<FileHost<'a>> for OpaqueHost<'a> {
    type Error = FileHost<'a>;

    fn try_from(value: FileHost<'a>) -> Result<Self, Self::Error> {
        Err(value)
    }
}

impl<'a> TryFrom<SpecialNotFileHost<'a>> for OpaqueHost<'a> {
    type Error = SpecialNotFileHost<'a>;

    fn try_from(value: SpecialNotFileHost<'a>) -> Result<Self, Self::Error> {
        Err(value)
    }
}

impl<'a> TryFrom<NonSpecialHost<'a>> for OpaqueHost<'a> {
    type Error = NonSpecialHost<'a>;

    fn try_from(value: NonSpecialHost<'a>) -> Result<Self, Self::Error> {
        if let NonSpecialHost::Opaque(x) = value {
            Ok(x)
        } else {
            Err(value)
        }
    }
}