roles: sql, crate, struct, id, rocket id, list page
This commit is contained in:
parent
2e055e2037
commit
efaf2bda5a
23 changed files with 462 additions and 2 deletions
|
|
@ -1,5 +1,6 @@
|
|||
mod app;
|
||||
mod key;
|
||||
mod role;
|
||||
mod user;
|
||||
mod username;
|
||||
|
||||
|
|
@ -14,6 +15,7 @@ pub enum Error {
|
|||
|
||||
pub use app::AppID;
|
||||
pub use key::KeyID;
|
||||
pub use role::RoleID;
|
||||
pub use user::UserID;
|
||||
pub use username::Username;
|
||||
pub use username::INVALID_USERNAME_ERROR;
|
||||
|
|
|
|||
63
crates/id/src/role.rs
Normal file
63
crates/id/src/role.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use super::Error;
|
||||
use nanoid_dictionary::{LOWERCASE, NUMBERS};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct RoleID(pub String);
|
||||
|
||||
impl Display for RoleID {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for RoleID {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&RoleID> for String {
|
||||
fn from(value: &RoleID) -> Self {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RoleID {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.chars()
|
||||
.all(|c| LOWERCASE.contains(&c) || NUMBERS.contains(&c) || c == '-')
|
||||
{
|
||||
Ok(Self(s.to_string()))
|
||||
} else {
|
||||
Err(Error::Invalid("Role"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::RoleID;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
fn invalid_characters() {
|
||||
let value = "AER Rennes tek5";
|
||||
|
||||
assert!(RoleID::from_str(value).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_role() {
|
||||
let value = "aer-rennes-tek5";
|
||||
let id = RoleID::from_str(value);
|
||||
assert!(id.is_ok());
|
||||
|
||||
let id = id.unwrap();
|
||||
assert_eq!(id.as_ref(), value);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue