uv-normalize 0.0.102

This is an internal component crate of uv
Documentation
use std::borrow::Cow;
use std::fmt::{self, Display};
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// An invalid component of a PEP 825 wheel variant property.
#[derive(Debug, thiserror::Error)]
pub enum InvalidVariantComponent {
    #[error("Invalid variant namespace: `{0}`")]
    InvalidNamespace(String),
    #[error("Invalid variant feature: `{0}`")]
    InvalidFeature(String),
    #[error("Invalid variant value: `{0}`")]
    InvalidValue(String),
}

/// A validated variant namespace.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantNamespace(Cow<'static, str>);

impl VariantNamespace {
    /// Return the validated component as a string.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Create a variant namespace from a static string.
    ///
    /// An invalid namespace causes a compilation error in a const context and a panic at runtime.
    pub const fn from_static(value: &'static str) -> Self {
        assert!(valid_component(value, false), "invalid variant namespace");
        Self(Cow::Borrowed(value))
    }
}

impl Display for VariantNamespace {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl FromStr for VariantNamespace {
    type Err = InvalidVariantComponent;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::try_from(value.to_owned())
    }
}

impl TryFrom<String> for VariantNamespace {
    type Error = InvalidVariantComponent;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if valid_component(&value, false) {
            Ok(Self(Cow::Owned(value)))
        } else {
            Err(InvalidVariantComponent::InvalidNamespace(value))
        }
    }
}

/// A validated variant feature.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantFeature(Cow<'static, str>);

impl VariantFeature {
    /// Return the validated component as a string.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Create a variant feature from a static string.
    ///
    /// An invalid feature causes a compilation error in a const context and a panic at runtime.
    pub const fn from_static(value: &'static str) -> Self {
        assert!(valid_component(value, false), "invalid variant feature");
        Self(Cow::Borrowed(value))
    }
}

impl Display for VariantFeature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl FromStr for VariantFeature {
    type Err = InvalidVariantComponent;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::try_from(value.to_owned())
    }
}

impl TryFrom<String> for VariantFeature {
    type Error = InvalidVariantComponent;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if valid_component(&value, false) {
            Ok(Self(Cow::Owned(value)))
        } else {
            Err(InvalidVariantComponent::InvalidFeature(value))
        }
    }
}

/// A validated variant value.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantValue(Cow<'static, str>);

impl VariantValue {
    /// Return the validated component as a string.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Create a variant value from a static string.
    ///
    /// An invalid value causes a compilation error in a const context and a panic at runtime.
    pub const fn from_static(value: &'static str) -> Self {
        assert!(valid_component(value, true), "invalid variant value");
        Self(Cow::Borrowed(value))
    }
}

impl Display for VariantValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl FromStr for VariantValue {
    type Err = InvalidVariantComponent;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::try_from(value.to_owned())
    }
}

impl TryFrom<String> for VariantValue {
    type Error = InvalidVariantComponent;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if valid_component(&value, true) {
            Ok(Self(Cow::Owned(value)))
        } else {
            Err(InvalidVariantComponent::InvalidValue(value))
        }
    }
}

const fn valid_component(value: &str, allow_period: bool) -> bool {
    if value.is_empty() {
        return false;
    }
    let bytes = value.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        let character = bytes[index];
        if !(character.is_ascii_lowercase()
            || character.is_ascii_digit()
            || character == b'_'
            || (allow_period && character == b'.'))
        {
            return false;
        }
        index += 1;
    }
    true
}