use rocket::http::RawStr; use rocket::outcome::IntoOutcome; use rocket::request::{self, FromParam, FromRequest, Request}; use serde_derive::Serialize; pub mod characters; #[derive(Eq, PartialEq, Serialize, Debug)] pub struct User { pub id: i32, pub username: String, } impl<'a, 'r> FromRequest<'a, 'r> for User { type Error = !; fn from_request(request: &'a Request<'r>) -> request::Outcome { request .cookies() .get_private("user_id") .and_then(|cookie| cookie.value().parse().ok()) .map(|id| //TODO load from db User { id: id, username: "somebody".to_string(), }) .or_forward(()) } } impl<'r> FromParam<'r> for User { type Error = &'r str; fn from_param(param: &'r RawStr) -> Result { let username: String = param.url_decode().or(Err("Invalid character ID"))?; //TODO load from DB Ok(User { id: 1, username: username, }) } }