Macros
context_update!, context!, impl_from_error!
● ● ●
pub async fn my_view(mut request: Request) -> AppResult<Response> {
let articles = vec!["Article 1", "Article 2"];
// Inject into Tera context
context_update!(request => {
"title" => "My page",
"articles" => &articles,
"count" => articles.len(),
});
request.render("my_page.html")
}
// In a handler
success!(request.notices => "Saved successfully!");
error!(request.notices => "An error occurred.");
info!(request.notices => "Checking...");
warning!(request.notices => "Warning: session expiring soon.");
// flash_now! — message injected directly into context
context_update!(request => {
"messages" => flash_now!(success => "Created!"),
});
// Automatically converts DbErr and others to AppError
impl_from_error!(
sea_orm::DbErr => database_error,
std::io::Error => internal_error,
serde_json::Error => serialization_error,
);
// Allows using ? in handlers
pub async fn handler(mut request: Request) -> AppResult<Response> {
let article = Article::find_by_id(1).one(&*db).await?; // DbErr converted
Ok(request.render("page.html")?)
}