Skip to main content

ModuleDefinition

Trait ModuleDefinition 

Source
pub trait ModuleDefinition:
    Send
    + Sync
    + 'static {
    // Required method
    fn register(container: &Container) -> Result<()>;

    // Provided methods
    fn module_name() -> &'static str { ... }
    fn imports() -> Vec<ModuleRef> { ... }
    fn is_global() -> bool { ... }
    fn exports() -> Vec<&'static str> { ... }
    fn controllers() -> Vec<Router<Container>> { ... }
    fn route_docs() -> Vec<RouteDocumentation> { ... }
    fn on_module_init() -> Vec<LifecycleHook>  { ... }
    fn on_module_destroy() -> Vec<LifecycleHook>  { ... }
    fn on_application_bootstrap() -> Vec<LifecycleHook>  { ... }
    fn on_application_shutdown() -> Vec<LifecycleHook>  { ... }
}
Expand description

ModuleDefinition is the contract for defining a NestForge module.

Modules are the building blocks of a NestForge application. They group related controllers and providers together, making the codebase easier to manage and reason about.

The #[module] macro automatically generates implementations of this trait. Manual implementation is possible for more control over the module setup.

§Manual Implementation Example

use nestforge::{ModuleDefinition, Container, ModuleRef};
use anyhow::Result;

struct MyModule;

impl ModuleDefinition for MyModule {
    fn register(container: &Container) -> Result<()> {
        // Register providers here
        Ok(())
    }

    fn imports() -> Vec<ModuleRef> {
        // List dependent modules
        vec![]
    }
}

Required Methods§

Source

fn register(container: &Container) -> Result<()>

Where the magic happens: register your module’s providers into the container.

Provided Methods§

Source

fn module_name() -> &'static str

The name of the module, used for debugging and error messages. By default, it uses the type name of the struct.

Source

fn imports() -> Vec<ModuleRef>

List any other modules that this module depends on. NestForge will make sure they’re initialized first.

Source

fn is_global() -> bool

If true, the providers in this module will be available globally.

Source

fn exports() -> Vec<&'static str>

List the providers that this module makes available to other modules that import it.

Source

fn controllers() -> Vec<Router<Container>>

Returns the Axum routers for the controllers defined in this module.

Source

fn route_docs() -> Vec<RouteDocumentation>

Returns the documentation for the routes defined in this module.

Source

fn on_module_init() -> Vec<LifecycleHook>

A hook that runs after the module has been initialized.

Source

fn on_module_destroy() -> Vec<LifecycleHook>

A hook that runs when the module is being destroyed.

Source

fn on_application_bootstrap() -> Vec<LifecycleHook>

A hook that runs after the entire application has been bootstrapped.

Source

fn on_application_shutdown() -> Vec<LifecycleHook>

A hook that runs when the application is shutting down.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§