Architecture of a Runique Project

Rust Macros

Runique provides a set of macros to simplify development.

Context Macros

MacroDescriptionExample
context!Create a Tera contextcontext!("title" => "Page")
context_update!Add to a Request's contextcontext_update!(request => { "key" => value })

Flash Message Macros

MacroDescriptionExample
success!Success message (session)success!(request.notices => "OK!")
error!Error message (session)error!(request.notices => "Error")
info!Info message (session)info!(request.notices => "Info")
warning!Warning (session)warning!(request.notices => "Warning")
flash_now!Immediate message (no session)flash_now!(error => "Errors")

Routing Macros

MacroDescriptionExample
urlpatterns!Define named routesurlpatterns!("/" => view!{...}, name = "index")
view!Handler for all HTTP methodsview!{ handler }
impl_objects!Django-like manager for SeaORMimpl_objects!(Entity)

Error Macros

MacroDescription
impl_from_error!Generates From<Error> for AppError

In context

The macros combine in a typical handler:

use runique::prelude::*;

pub async fn contact(mut request: Request) -> AppResult<Response> {
    let mut form: ContactForm = request.form();

    if request.is_post() && form.is_valid().await {
        // Session flash + redirect (Post/Redirect/Get pattern)
        success!(request.notices => "Message sent!");
        return Ok(Redirect::to("/contact").into_response());
    }

    // Add variables to the request context
    context_update!(request => {
        "title" => "Contact",
        "contact_form" => &form,
    });
    request.render("contact.html")
}

success! / error! / info! / warning! write to the session (visible after a redirect). flash_now! produces an immediate message without a session β€” useful when re-rendering the same page without redirecting.