actix_web_flash_messages/storage/
interface.rs

1use crate::FlashMessage;
2use actix_web::dev::ResponseHead;
3use actix_web::HttpRequest;
4
5/// The interface to retrieve and dispatch flash messages.
6///
7/// `actix-web-flash-messages` provides two implementation of flash messages:
8///
9/// - a cookie-based one, [`CookieMessageStore`], using a signed cookie to store and
10///   retrieve messages;
11/// - a session-based one, [`SessionMessageStore`], which attaches flash messages
12///   to the current session.
13///
14/// You can provide your own custom message store backend by implementing this trait.
15///
16/// [`CookieMessageStore`]: crate::storage::CookieMessageStore
17/// [`SessionMessageStore`]: crate::storage::SessionMessageStore
18pub trait FlashMessageStore: Send + Sync {
19    /// Extract flash messages from an incoming request.
20    fn load(&self, request: &HttpRequest) -> Result<Vec<FlashMessage>, LoadError>;
21
22    /// Attach flash messages to an outgoing response.
23    fn store(
24        &self,
25        messages: &[FlashMessage],
26        request: HttpRequest,
27        response: &mut ResponseHead,
28    ) -> Result<(), StoreError>;
29}
30
31#[derive(thiserror::Error, Debug)]
32/// Possible failures modes for [`FlashMessageStore::load`].
33pub enum LoadError {
34    #[error("Failed to deserialize incoming flash messages")]
35    DeserializationError(#[source] anyhow::Error),
36    #[error("The content of incoming flash messages failed a cryptographic integrity check (e.g. signature verification)")]
37    IntegrityCheckFailed(#[source] anyhow::Error),
38    #[error("Something went wrong when extracting incoming flash messages")]
39    GenericError(#[source] anyhow::Error),
40}
41
42/// Possible failures modes for [`FlashMessageStore::store`].
43#[derive(thiserror::Error, Debug)]
44pub enum StoreError {
45    #[error("Failed to serialize outgoing flash messages")]
46    SerializationError(#[source] anyhow::Error),
47    #[error("Outgoing flash messages, when serialised, exceeded the store size limit")]
48    SizeLimitExceeded(#[source] anyhow::Error),
49    #[error("Something went wrong when flushing outgoing flash messages")]
50    GenericError(#[source] anyhow::Error),
51}