id: implement rocket FromParam

This commit is contained in:
Philippe Loctaux 2023-03-08 00:15:34 +01:00
parent e04de752ef
commit 7ab34825ad
5 changed files with 28 additions and 2 deletions

16
crates/ezidam/src/id.rs Normal file
View 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)
}
}

View file

@ -5,6 +5,7 @@ mod database;
mod error; mod error;
mod file_from_bytes; mod file_from_bytes;
mod guards; mod guards;
mod id;
mod page; mod page;
mod response_timer; mod response_timer;
mod routes; mod routes;

View file

@ -8,9 +8,10 @@ pub(self) mod prelude {
pub use crate::error::Error; pub use crate::error::Error;
pub use crate::file_from_bytes::FileFromBytes; pub use crate::file_from_bytes::FileFromBytes;
pub use crate::guards::*; pub use crate::guards::*;
pub use crate::id::*;
pub use crate::page::{FlashKind, Page}; pub use crate::page::{FlashKind, Page};
pub use hash::Password; pub use hash::Password;
pub use id::UserID; pub use id::*;
pub use rocket::form::Form; pub use rocket::form::Form;
pub use rocket::request::FlashMessage; pub use rocket::request::FlashMessage;
pub use rocket::response::Flash; pub use rocket::response::Flash;

View file

@ -7,3 +7,4 @@ edition = "2021"
thiserror = { workspace = true } thiserror = { workspace = true }
nanoid = "0.4.0" nanoid = "0.4.0"
nanoid-dictionary = "0.4.3" nanoid-dictionary = "0.4.3"
serde = { version = "1", features = ["derive"] }

View file

@ -1,12 +1,13 @@
use super::Error; use super::Error;
use nanoid::nanoid; use nanoid::nanoid;
use nanoid_dictionary::NOLOOKALIKES; use nanoid_dictionary::NOLOOKALIKES;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
const LENGTH: usize = 15; const LENGTH: usize = 15;
#[derive(Debug, Clone, PartialEq)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct UserID(pub String); pub struct UserID(pub String);
impl Display for UserID { 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 { impl FromStr for UserID {
type Err = Error; type Err = Error;