Skip to main content

Crate salvo_flash

Crate salvo_flash 

Source
Expand description

Flash messages middleware for the Salvo web framework.

Flash messages are temporary notifications that persist across a single redirect, commonly used to display success, error, or info messages after form submissions.

§How It Works

  1. A handler sets flash messages before redirecting
  2. The middleware stores them (in cookies or session)
  3. On the next request, the messages are available and then cleared

§Storage Options

StoreFeatureDescription
CookieStorecookie-storeStores messages in a cookie
SessionStoresession-storeStores messages in the session

§Message Levels

Messages have severity levels for appropriate UI styling:

  • Debug - Debugging information
  • Info - General information
  • Success - Operation completed successfully
  • Warning - Warning that doesn’t prevent operation
  • Error - Error that needs attention

§Example

use salvo_flash::{FlashDepotExt, CookieStore};
use salvo_core::prelude::*;

#[handler]
async fn submit_form(depot: &mut Depot, res: &mut Response) {
    // Set flash message before redirect
    depot.outgoing_flash_mut().success("Form submitted successfully!");
    res.render(Redirect::other("/result"));
}

#[handler]
async fn show_result(depot: &mut Depot, res: &mut Response) {
    if let Some(flash) = depot.incoming_flash() {
        for msg in flash.iter() {
            println!("{}: {}", msg.level, msg.value);
        }
    }
    res.render("Result page");
}

// Setup router with flash middleware
let router = Router::new()
    .hoop(CookieStore::new().into_handler())
    .push(Router::with_path("submit").post(submit_form))
    .push(Router::with_path("result").get(show_result));

§Filtering by Level

Use minimum_level() to filter out lower-priority messages:

let mut handler = FlashHandler::new(CookieStore::new());
handler.minimum_level(FlashLevel::Warning);  // Only show Warning and Error

Read more: https://salvo.rs

Structs§

CookieStorecookie-store
CookieStore is a FlashStore implementation that stores the flash messages in a cookie.
Flash
A flash is a list of messages.
FlashHandler
FlashHandler is a middleware for flash messages.
FlashMessage
A flash message.
SessionStoresession-store
SessionStore is a FlashStore implementation that stores the flash messages in a session.

Enums§

FlashLevel
Verbosity level of a flash message.

Constants§

INCOMING_FLASH_KEY
Key for incoming flash messages in depot.
OUTGOING_FLASH_KEY
Key for outgoing flash messages in depot.

Traits§

FlashDepotExt
A trait for Depot to get flash messages.
FlashStore
FlashStore is for stores flash messages.

Functions§

cookie_storecookie-store
Helper function to create a CookieStore.
session_storesession-store
Helper function to create a SessionStore.