dust

Modules

Introduction

A module is a self-contained area of the application with its own routes, handlers, migrations, views, and services. Modules live in modules/ at the root of the application, outside src/, so the application's own code stays separate from the parts you can add, disable, or remove.

Modules are compiled into the application. They are not separate crates, so adding one doesn't add a Cargo.toml, change the workspace, or slow down the build with another package.

A module is a good fit for a feature that has all of these: its own URLs, its own tables, and a boundary you may want to turn off. A blog, a shop, or an admin panel is a module. A single handler, a policy, or a helper is not; those belong in src/.

The feature is on in a new application. On an existing one, add modulis to the dust-framework features, declare the registry in src/lib.rs with #[path = "../modules/mod.rs"] pub mod modules;, and pass crate::modules::register to .with_services(...) in src/app.rs.

Creating a module

shell
dust module:make blog

The command writes modules/blog, adds pub mod blog; to modules/mod.rs, and registers the module:

rust
// modules/mod.rspub mod blog;use dust::container::Registrar;pub fn register(app: &mut Registrar) {    dust::modulis::register::<blog::Blog>(app);}

The application wires the registry once, in src/app.rs:

rust
Application::configure()    .with_migrations(dust::db::migrations!("database/migrations"))    .with_services(crate::modules::register)

dust new already writes both files, so the first module needs no setup. The generated module is a starting point: it has a welcome route, a handler, and a view, and you replace them with the feature you are building.

A module name takes lowercase letters, digits, and _, and starts with a letter. shop_admin becomes the type ShopAdmin and the folder modules/shop_admin. The name is also the module's view namespace, so pick one you can type in a template.

The module's files

code
modules/
├── mod.rs                  the registry
└── blog/
    ├── mod.rs              pub struct Blog; impl Module for Blog
    ├── routes.rs
    ├── handlers/
    │   └── mod.rs
    ├── migrations/
    └── views/

A module may add any folders it needs, such as models/, services/, or config/. The generated files are a starting point, not a limit. Keep the module's mod.rs thin: it declares the submodules and implements Module, while the routes, handlers, and services live in their own files.

The Module trait

The module's mod.rs implements Module:

rust
use dust::db::Migrations;use dust::modulis::Module;use dust::routing::Router;pub mod handlers;pub mod routes;pub struct Blog;impl Module for Blog {    fn name() -> &'static str {        "blog"    }    fn routes(route: &mut Router) {        routes::routes(route);    }    fn migrations() -> Migrations {        dust::db::migrations!("modules/blog/migrations")    }}

name is the module's folder and its view namespace. routes adds the module's routes; they are not prefixed automatically, so the module decides its URLs. migrations returns the module's migrations, which dust migrate runs with the application's own. views may replace the default modules/<name>/views namespace, and register adds services, middleware, or commands.

Every method except name has a default, so a module with only routes implements name and routes. A module that adds no routes, migrations, or views can exist as a library of services, but consider whether it belongs in src/ instead.

Routes and handlers

A module's routes function receives the router and registers its URLs in the web group, with sessions and CSRF protection. Write the routes and handlers the same way you write the application's own, under the module's folders:

rust
// modules/blog/routes.rsuse dust::prelude::*;use crate::modules::blog::handlers;pub fn routes(route: &mut Router) {    route.get("/blog", handlers::posts::index);    route.get("/blog/{post:slug}", handlers::posts::show);}
rust
// modules/blog/handlers/posts.rsuse dust::prelude::*;use crate::modules::blog::models::Post;pub async fn show(post: Post) -> View {    view("blog::posts.show", data! { post })}

The view name carries the module namespace, so blog::posts.show resolves to modules/blog/views/posts/show.html. Handlers in a module use the same dust::prelude as the rest of the application, and they can inject the application's services with Inject<T>.

Because the module's routes are ordinary routes, they appear in dust route:list and in route!(...) calls like any other. A handler path includes the module, such as app::modules::blog::handlers::posts::show, so a link from the application names the full path.

Services, middleware, and commands

register runs after the module's routes and migrations are added, and receives the same Registrar a package's service provider receives. Use it for the parts that are not routes:

rust
use dust::container::Registrar;use crate::modules::blog::services::Feed;impl Module for Blog {    // ...    fn register(app: &mut Registrar) {        app.singleton(async |_app| Ok(Feed::new()));        app.middleware().web(|web| {            web.append(RecordBlogVisit);        });    }}

The module's services are registered in the application's container, so any handler may inject them by type. Give them names that include the module when the type could clash with another module's, such as BlogFeed rather than Feed. A module's middleware applies to the whole web group, not only the module's routes; if it should run only on the module's URLs, attach it to the module's route group instead.

Attribute-discovered items such as #[command] functions, listeners, and #[singleton] services work in a declared module too. Rust compiles the module because modules/mod.rs declares it, and dust-framework finds the attributes in the compiled crate.

Registration

dust::modulis::register adds the module's routes, migrations, and blog:: view namespace, then calls the module's own register. The generated registry calls it once per module.

rust
pub fn register(app: &mut Registrar) {    dust::modulis::register::<blog::Blog>(app);    dust::modulis::register::<shop::Shop>(app);}

The order in modules/mod.rs is the order the modules register, so a module that adds a middleware sees the modules before it. Put foundational modules first if a later module depends on their services. Nothing stops two modules from registering the same route; the application refuses to start and names both, so move one route or change its path.

Registration is skipped for a disabled module, but the module's code is still compiled. That keeps a disabled module type-checked, at the cost of compiling code that doesn't run.

Migrations and views

Name a module's migration files with the module as a prefix, such as blog_0001_create_posts_table.rs. Migration names are global, so the prefix keeps two modules from declaring the same one. dust migrate runs the application's migrations and every enabled module's migrations in filename order.

Views are available under the module's name:

rust
view::render("blog::index", data)

The namespace points at modules/blog/views unless the module's views method replaces it. To override a module's view without editing the module, place your file at resources/views/vendor/blog/index.html; the finder checks the application's vendor/<namespace> directory before the module's own.

Enabling and disabling

config/modules.toml lists the modules that stay compiled but don't register:

toml
disabled = ["shop"]
shell
dust module:disable shopdust module:enable shop

The change applies the next time the application starts; it needs no recompilation. A disabled module keeps its files and its code, so enabling it again is immediate.

Disabling a module removes its routes, migrations, views, and services from the running application. Its tables stay in the database, because the migrations already ran; disabling is not a rollback. If a later module or the application's own code injects a service the disabled module registered, the application fails to start, which is the correct outcome: the dependency is real and must be resolved, not hidden.

config/modules.toml must be valid TOML. An invalid file stops the application at startup rather than silently enabling everything.

Testing a module

A module is compiled into the app crate, so its handlers and services are testable through the same test client as the rest of the application. Keep the module's integration tests in the application's tests/, and point them at the module's URLs:

rust
use dust::testing::prelude::*;#[dust::test]async fn the_blog_lists_published_posts(app: Application) {    app.get("/blog").await.assert_ok().assert_see("First post");}

When a module ships its own fixtures or migrations, the test database needs them, so enable refresh_database in src/testing.rs. To test a module in isolation, build a test application that registers only that module, as the package guide does for a package.

Commands

Command What it does
dust module:make <name> Creates the module and adds it to the registry.
dust module:list Lists the modules with their status. --enabled and --disabled filter the list.
dust module:enable <name> Removes the module from disabled.
dust module:disable <name> Adds the module to disabled.
dust module:delete <name> Deletes the module's folder and registry lines. It asks first; --force skips the question.

module:delete removes the registry lines and the folder, but not the module's migrations or the tables they created. Remove those with a new migration if you mean to drop the data.