1
0
mirror of https://github.com/actix/examples synced 2024-11-28 00:12:57 +01:00
examples/basics/todo/src/session.rs

40 lines
884 B
Rust
Raw Normal View History

2019-03-11 02:23:44 +01:00
use actix_session::Session;
2018-08-27 11:56:26 +02:00
use actix_web::error::Result;
use serde::{Deserialize, Serialize};
2018-08-27 11:56:26 +02:00
const FLASH_KEY: &str = "flash";
2019-03-11 02:23:44 +01:00
pub fn set_flash(session: &Session, flash: FlashMessage) -> Result<()> {
2022-07-21 04:30:12 +02:00
Ok(session.insert(FLASH_KEY, flash)?)
2018-08-27 11:56:26 +02:00
}
2019-03-11 02:23:44 +01:00
pub fn get_flash(session: &Session) -> Result<Option<FlashMessage>> {
2022-07-21 04:30:12 +02:00
Ok(session.get::<FlashMessage>(FLASH_KEY)?)
2018-08-27 11:56:26 +02:00
}
2019-03-11 02:23:44 +01:00
pub fn clear_flash(session: &Session) {
session.remove(FLASH_KEY);
2018-08-27 11:56:26 +02:00
}
#[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(),
}
}
}