id: implement rocket FromParam
This commit is contained in:
parent
e04de752ef
commit
7ab34825ad
5 changed files with 28 additions and 2 deletions
16
crates/ezidam/src/id.rs
Normal file
16
crates/ezidam/src/id.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use id::UserID;
|
||||
use rocket::request::FromParam;
|
||||
use rocket::serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct RocketUserID(pub UserID);
|
||||
|
||||
impl<'r> FromParam<'r> for RocketUserID {
|
||||
type Error = id::Error;
|
||||
|
||||
fn from_param(param: &'r str) -> Result<Self, Self::Error> {
|
||||
UserID::from_str(param).map(RocketUserID)
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ mod database;
|
|||
mod error;
|
||||
mod file_from_bytes;
|
||||
mod guards;
|
||||
mod id;
|
||||
mod page;
|
||||
mod response_timer;
|
||||
mod routes;
|
||||
|
|
|
|||
|
|
@ -8,9 +8,10 @@ pub(self) mod prelude {
|
|||
pub use crate::error::Error;
|
||||
pub use crate::file_from_bytes::FileFromBytes;
|
||||
pub use crate::guards::*;
|
||||
pub use crate::id::*;
|
||||
pub use crate::page::{FlashKind, Page};
|
||||
pub use hash::Password;
|
||||
pub use id::UserID;
|
||||
pub use id::*;
|
||||
pub use rocket::form::Form;
|
||||
pub use rocket::request::FlashMessage;
|
||||
pub use rocket::response::Flash;
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ edition = "2021"
|
|||
thiserror = { workspace = true }
|
||||
nanoid = "0.4.0"
|
||||
nanoid-dictionary = "0.4.3"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
use super::Error;
|
||||
use nanoid::nanoid;
|
||||
use nanoid_dictionary::NOLOOKALIKES;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::str::FromStr;
|
||||
|
||||
const LENGTH: usize = 15;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct UserID(pub String);
|
||||
|
||||
impl Display for UserID {
|
||||
|
|
@ -21,6 +22,12 @@ impl Default for UserID {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for UserID {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for UserID {
|
||||
type Err = Error;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue