safe_http 0.1.0-beta.4

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

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

#[derive(Debug, Clone)]
pub struct HeaderName(SharedStr);

impl HeaderName {
    #[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 HeaderName"),
        }
    }

    #[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()
    }
}

use std::hash::Hasher;

impl Hash for HeaderName {
    fn hash<H: Hasher>(&self, state: &mut H) {
        for byte in self.0.bytes() {
            state.write_u8(byte.to_ascii_lowercase())
        }
    }
}

impl PartialEq for HeaderName {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq_ignore_ascii_case(&other.0)
    }
}

impl PartialEq<str> for HeaderName {
    fn eq(&self, other: &str) -> bool {
        self.0.eq_ignore_ascii_case(other)
    }
}

impl PartialEq<String> for HeaderName {
    fn eq(&self, other: &String) -> bool {
        self.0.eq_ignore_ascii_case(other)
    }
}

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

impl Eq for HeaderName {}

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

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

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

impl TryFrom<&'static str> for HeaderName {
    type Error = InvalidHeaderName;

    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 HeaderName {
    type Error = InvalidHeaderName;

    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 HeaderName {
    type Error = InvalidHeaderName;

    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 HeaderName {
    type Error = InvalidHeaderName;

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