pub mod error;
pub use error::Error;
pub const USERNAME_PATTERN: &str = r"^[\w]{2,64}$";
use regex::Regex;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Data {
username: String,
}
impl Data {
pub fn build(username: String) -> Result<Self, Error> {
match Regex::new(USERNAME_PATTERN) {
Ok(regex) => {
if regex.is_match(&username) {
Ok(Self { username })
} else {
Err(Error::Username(USERNAME_PATTERN.to_string()))
}
}
Err(e) => Err(Error::Regex(e)),
}
}
pub fn to_json(&self) -> Result<String, Error> {
match serde_json::to_string(&self) {
Ok(json) => Ok(json),
Err(e) => Err(Error::Json(e)),
}
}
pub fn username(&self) -> &str {
&self.username
}
}