safe_uri 0.1.0-beta.4

Simple and safe URI types.
Documentation
// This file is automatically generated.

use super::{validate_static, validate_with_normalized_percent_encoding, InvalidFragment};
use shared_bytes::SharedStr;
use std::{hash::Hash, sync::Arc};

#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Fragment(SharedStr);

impl Fragment {
    #[track_caller]
    #[inline]
    pub const fn from_static(string: &'static str) -> Self {
        match validate_static(string.as_bytes()) {
            Ok(()) => Self(SharedStr::from_static(string)),
            Err(_e) => panic!("invalid static Fragment"),
        }
    }

    #[inline]
    pub fn as_shared_str(&self) -> &SharedStr {
        &self.0
    }

    #[inline]
    pub fn into_shared_str(self) -> SharedStr {
        self.0
    }

    #[inline]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl PartialEq<str> for Fragment {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<String> for Fragment {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl AsRef<str> for Fragment {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Eq for Fragment {}

impl From<Fragment> for SharedStr {
    fn from(x: Fragment) -> Self {
        x.into_shared_str()
    }
}

impl From<Fragment> for String {
    fn from(x: Fragment) -> Self {
        x.into_shared_str().into_string()
    }
}

impl<T> PartialEq<&'_ T> for Fragment
where
    Self: PartialEq<T>,
    T: ?Sized,
{
    fn eq(&self, other: &&T) -> bool {
        self.eq(*other)
    }
}

impl TryFrom<&'static str> for Fragment {
    type Error = InvalidFragment;

    fn try_from(value: &'static str) -> Result<Self, Self::Error> {
        let inner = validate_with_normalized_percent_encoding(value)?
            .unwrap_or_else(|| SharedStr::from(value));
        Ok(Self(inner))
    }
}

impl TryFrom<String> for Fragment {
    type Error = InvalidFragment;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let inner = validate_with_normalized_percent_encoding(&value)?
            .unwrap_or_else(|| SharedStr::from(value));
        Ok(Self(inner))
    }
}

impl TryFrom<Arc<String>> for Fragment {
    type Error = InvalidFragment;

    fn try_from(value: Arc<String>) -> Result<Self, Self::Error> {
        let inner = validate_with_normalized_percent_encoding(&value)?
            .unwrap_or_else(|| SharedStr::from(value));
        Ok(Self(inner))
    }
}

impl TryFrom<SharedStr> for Fragment {
    type Error = InvalidFragment;

    fn try_from(value: SharedStr) -> Result<Self, Self::Error> {
        let inner = validate_with_normalized_percent_encoding(&value)?.unwrap_or(value);
        Ok(Self(inner))
    }
}