The Prelude
- Introduction
- What the prelude includes
- Importing everything else
- The testing prelude
- Common import problems
Introduction
Most files in your application use the same handful of dust-framework types: requests, responses, redirects, the router, and views. Instead of importing each one, you may import dust-framework's prelude at the top of the file:
use dust::prelude::*;#[dust::input]pub struct Rename { name: Option<String>,}pub async fn store(request: Request) -> dust::Result<Redirect> { let name = request.parse::<Rename>()?.name.or_abort(422)?; // ... Ok(Redirect::back())}Most examples in these docs assume this import.
What the prelude includes
The prelude includes the types most handlers and route files use:
Request,Response,Redirect,Json,Query, andStatusCodeRouterandParamsfor your routesSession,Inject, andApplicationValidfor validated input, andAuthenticatedfor the logged-in user- The
route!andredirect!macros for route URLs and redirects - The
viewfunction anddata!macro for views - The
EnumandNewtypederives, for your own enums and single-field structs in model columns and form fields
It also adds methods to types you already use:
contextandwith_contexton results, for adding error contextor_aborton optionscanandauthorizefor authorizationfeaturesfor feature flagsRule::uniqueandRule::existsfor database validation rules- String helpers such as
snake, and date helpers such asis_past - Spans such as
5.minutes() with_cookieandwithout_cookieon responsesloadon a list of models andwithon a relation, for eager loading
These methods come from traits, so they only work in files that import the prelude. If the compiler says a method such as or_abort doesn't exist, check the file's imports.
Traits behind the helpers
The prelude re-exports the traits, not only the types. That is what makes methods such as .slug(), .minutes(), and .or_abort() appear on values you already have:
| Trait | Adds | Guide |
|---|---|---|
Str |
slug, headline, plural, limit, and the other string helpers |
Strings |
Dates |
is_past, plus, minus, format, for_humans |
Dates and time |
ToSpan |
5.minutes(), 2.hours(), 1.week() |
Lengths of time |
ErrorContext |
context and with_context on results |
Errors |
OrAbort |
or_abort on options |
Errors |
Authorizable |
can and authorize on the logged-in user |
Authorization |
HasFeatures |
features for feature flags |
Feature flags |
ModelList, Relation, MorphRelation |
load on a list of models and with on a relation |
Eager loading |
A trait in scope is enough for the method to work on a type that implements it, even when the type comes from another module. If two imports define the same method name, Rust asks you to disambiguate; import the trait you mean instead of both.
Importing everything else
Import everything else from its own module, such as dust::foundation::ServiceProvider or dust::view::View. The first example in each section of these docs shows its imports, and the examples after it assume them.
The prelude leaves out dust-framework's Result and Error types so they don't replace Rust's own. Write them as dust::Result and dust::Error, as the handler above does.
The testing prelude
In your tests, import the testing prelude instead. It includes everything in the prelude, plus what your tests need to exercise your application:
get,post, and the other request methods, to send requests to your applicationcommand, to run your console commandsassert_database_hasand the other database assertions- The
json!macro, for JSON request bodies and assertions
With that one import, a test can send a request and check what your application stored:
use dust::testing::prelude::*;#[dust::test]async fn messages_can_be_posted(app: Application) { app.post("/messages") .json(json!({ "body": "Hello!" })) .await .assert_redirect(); app.assert_database_has("messages", json!({ "body": "Hello!" })).await;}When you're ready to write tests of your own, the testing guide walks you through everything the test client can do, from acting as a logged-in user and checking your database to faking HTTP requests and traveling through time.
Common import problems
Most "method not found" errors in a dust application come from a missing import, not a missing method. These are the usual ones:
| Message | Cause | Fix |
|---|---|---|
no method named slug found for &str |
Str isn't in scope. |
use dust::prelude::*; or use dust::str::Str; |
no method named minutes found for i32 |
ToSpan isn't in scope. |
Import the prelude, or dust::time::ToSpan. |
no method named or_abort found for Option<..> |
OrAbort isn't in scope. |
Import the prelude. |
no method named context found for Result<..> |
ErrorContext isn't in scope. |
Import the prelude. |
cannot find type Request |
The file uses a prelude type without the prelude. | use dust::prelude::*; |
dust::Result resolves to std::io::Result |
A use std::io; shadows the framework. |
Refer to the framework as ::dust::…. |
The prelude is deliberately small. If you need ServiceProvider, View, or Engine, import the module's type, so the file says where it comes from. The first example in each guide section shows its imports for that reason.