1
0
mirror of https://github.com/actix/examples synced 2024-11-24 14:53:00 +01:00
examples/actix_todo/src/session.rs
2019-03-10 18:23:44 -07:00

39 lines
834 B
Rust

use actix_session::Session;
use actix_web::error::Result;
const FLASH_KEY: &str = "flash";
pub fn set_flash(session: &Session, flash: FlashMessage) -> Result<()> {
session.set(FLASH_KEY, flash)
}
pub fn get_flash(session: &Session) -> Result<Option<FlashMessage>> {
session.get::<FlashMessage>(FLASH_KEY)
}
pub fn clear_flash(session: &Session) {
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(),
}
}
}