mellon 0.1.0

Library for adding contemporary authentication to rust-based websites.
Documentation
use serde::{Deserialize, Serialize};

/// A sane name.
/// It must be completely alphanumeric (UTF-8),
/// it may contain underscores and it must be at least 1 character and at most 32 characters long.
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct SaneName(String);

impl SaneName {
    /// Create a new `AccountName`
    /// If the provided `name` is not conforming to the restrictions, return the original String as an `Err`.
    pub fn new(name: String) -> Result<Self, String> {
        if !name.is_empty()
            && name.len() <= 32
            && name.chars().all(|c| c.is_alphanumeric() || c == '_')
        {
            Ok(Self(name))
        } else {
            Err(name)
        }
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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