1
0
mirror of https://github.com/actix/examples synced 2024-11-24 23:02:59 +01:00
examples/actix_todo/src/session.rs

40 lines
930 B
Rust
Raw Normal View History

2018-08-27 11:56:26 +02:00
use actix_web::error::Result;
use actix_web::middleware::session::RequestSession;
use actix_web::HttpRequest;
const FLASH_KEY: &str = "flash";
pub fn set_flash<T>(request: &HttpRequest<T>, flash: FlashMessage) -> Result<()> {
request.session().set(FLASH_KEY, flash)
}
pub fn get_flash<T>(req: &HttpRequest<T>) -> Result<Option<FlashMessage>> {
req.session().get::<FlashMessage>(FLASH_KEY)
}
pub fn clear_flash<T>(req: &HttpRequest<T>) {
req.session().remove(FLASH_KEY);
}
#[derive(Deserialize, Serialize)]
pub struct FlashMessage {
pub kind: String,
pub message: String,
}
impl FlashMessage {
pub fn success(message: &str) -> Self {
Self {
kind: "success".to_owned(),
message: message.to_owned(),
}
}
pub fn error(message: &str) -> Self {
Self {
kind: "error".to_owned(),
message: message.to_owned(),
}
}
}