username: type to parse username

This commit is contained in:
Philippe Loctaux 2023-05-04 22:47:11 +02:00
parent 0e77f7be5e
commit bdd5eca9f1
7 changed files with 119 additions and 24 deletions

View file

@ -1,6 +1,7 @@
mod app;
mod key;
mod user;
mod username;
// error
#[derive(thiserror::Error)]
@ -14,3 +15,5 @@ pub enum Error {
pub use app::AppID;
pub use key::KeyID;
pub use user::UserID;
pub use username::Username;
pub use username::INVALID_USERNAME_ERROR;

63
crates/id/src/username.rs Normal file
View file

@ -0,0 +1,63 @@
use super::Error;
use nanoid_dictionary::ALPHANUMERIC;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
pub const INVALID_USERNAME_ERROR: &str = "Invalid username. Pattern is [a-zA-Z0-9]";
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Username(pub String);
impl Display for Username {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for Username {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl From<&Username> for String {
fn from(value: &Username) -> Self {
value.to_string()
}
}
impl FromStr for Username {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.chars().all(|c| ALPHANUMERIC.contains(&c)) {
Ok(Self(s.to_string()))
} else {
Err(Error::Invalid("Username"))
}
}
}
#[cfg(test)]
mod tests {
use super::Username;
use std::str::FromStr;
#[test]
fn invalid_characters() {
let value = "#!👎 INVALID 🚫!#";
assert!(Username::from_str(value).is_err());
}
#[test]
fn valid_username() {
let value = "philt3r";
let id = Username::from_str(value);
assert!(id.is_ok());
let id = id.unwrap();
assert_eq!(id.0, value);
}
}