Skip to main content

sword_core/
module.rs

1use crate::{ComponentRegistry, Config, ControllerRegistry, ProviderRegistry};
2
3/// A trait for defining modules in the application.
4///
5/// `Module` represents a cohesive unit of functionality that can be
6/// plugged into the application. Modules can register controllers,
7/// components, and providers to extend the application's capabilities.
8///
9/// # Example
10///
11/// ```rust,ignore
12/// use sword_core::Module;
13/// use sword_core::ControllerRegistry;
14///
15/// pub struct MyModule;
16///
17/// impl Module for MyModule {
18///     fn register_controllers(controllers: &ControllerRegistry) {
19///         controllers.register::<MyController>();  // Register HTTP controller
20///     }
21///
22///     fn register_components(components: &ComponentRegistry) {
23///         components.register::<MyService>();
24///     }
25///
26///     async fn register_providers(_: &Config, providers: &ProviderRegistry) {
27///         providers.register(MyProvider::new().await);
28///     }
29/// }
30/// ```
31#[allow(async_fn_in_trait)]
32#[allow(unused_variables)]
33pub trait Module {
34    /// Register controllers provided by the module.
35    /// A `Controller` is a way to represent entry points into the application,
36    /// such as HTTP controllers, Socket.IO Handlers, or gRPC services.
37    fn register_controllers(controllers: &ControllerRegistry) {}
38
39    /// Register component structs marked with `#[injectable]`
40    fn register_components(components: &ComponentRegistry) {}
41
42    /// Register provider structs marked with `#[injectable(provider)]`
43    async fn register_providers(config: &Config, providers: &ProviderRegistry) {}
44}