use std::borrow::Cow;
use std::fmt::{self, Display};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[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),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantNamespace(Cow<'static, str>);
impl VariantNamespace {
pub fn as_str(&self) -> &str {
&self.0
}
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))
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantFeature(Cow<'static, str>);
impl VariantFeature {
pub fn as_str(&self) -> &str {
&self.0
}
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))
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct VariantValue(Cow<'static, str>);
impl VariantValue {
pub fn as_str(&self) -> &str {
&self.0
}
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
}