#![allow(dead_code)]
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
use std::{borrow::Cow, error, fmt};
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
use smartstring::alias::String;
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
use strid::braid;
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
#[braid(serde, ref_doc = "A borrowed reference to a string slice wrapper")]
pub struct SmartUsernameBuf;
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
#[braid(serde, no_expose)]
pub struct CompactData(compact_str::CompactString);
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
#[braid(
serde,
no_expose,
normalizer,
ref_doc = "A borrowed reference to a non-empty, lowercase string"
)]
pub struct LowerCompactString(compact_str::CompactString);
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
impl strid::Validator for LowerCompactString {
type Error = InvalidString;
fn validate(raw: &str) -> Result<(), Self::Error> {
if raw.is_empty() {
Err(InvalidString::EmptyString)
} else if raw.chars().any(char::is_uppercase) {
Err(InvalidString::InvalidCharacter)
} else {
Ok(())
}
}
}
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
impl strid::Normalizer for LowerCompactString {
fn normalize(s: &str) -> Result<Cow<'_, str>, Self::Error> {
if s.is_empty() {
Err(InvalidString::EmptyString)
} else if s.contains(char::is_uppercase) {
Ok(Cow::Owned(s.to_lowercase()))
} else {
Ok(Cow::Borrowed(s))
}
}
}
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
#[derive(Debug)]
pub enum InvalidString {
EmptyString,
InvalidCharacter,
}
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
impl fmt::Display for InvalidString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::EmptyString => f.write_str("string cannot be empty"),
Self::InvalidCharacter => f.write_str("string contains invalid uppercase character"),
}
}
}
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
impl error::Error for InvalidString {}
#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
strid::from_infallible!(InvalidString);