Skip to main content

renox_core/
module.rs

1use crate::db::Migration;
2use crate::{Contributions, Registry, Routes};
3
4/// A self-contained piece of an application: its routes and migrations, and
5/// later its jobs, policies and views.
6///
7/// ```
8/// # use renox::prelude::*;
9/// # async fn index() -> &'static str { "" }
10/// # async fn show() -> &'static str { "" }
11/// pub struct Products;
12///
13/// impl Module for Products {
14///     fn name(&self) -> &'static str { "products" }
15///
16///     fn routes(&self) -> Routes {
17///         Routes::new()
18///             .get("/products", index).name("products.index")
19///             .get("/products/{id}", show).name("products.show")
20///     }
21/// }
22/// ```
23pub trait Module: Send + Sync + 'static {
24    /// The module's name, shown by `route:list`.
25    fn name(&self) -> &'static str;
26
27    /// The module's routes; none by default.
28    fn routes(&self) -> Routes {
29        Routes::new()
30    }
31
32    /// The module's routes, given everything modules contributed in
33    /// `register` (see `Registry::contribute`). Every `register` has run
34    /// by then, so module order does not matter. Calls [`Module::routes`]
35    /// by default.
36    ///
37    /// ```
38    /// # use renox::prelude::*;
39    /// struct Link(&'static str);
40    /// struct Menu;
41    /// impl Module for Menu {
42    ///     fn name(&self) -> &'static str { "menu" }
43    ///     fn routes_with(&self, contributions: &Contributions) -> Routes {
44    ///         let _links = contributions.all::<Link>();
45    ///         Routes::new()
46    ///     }
47    /// }
48    /// ```
49    fn routes_with(&self, _contributions: &Contributions) -> Routes {
50        self.routes()
51    }
52
53    /// Migrations this module owns, e.g. `renox::migrations!("src/app/products/migrations")`.
54    fn migrations(&self) -> &'static [Migration] {
55        &[]
56    }
57
58    /// Registers the module's jobs, event listeners and scheduled tasks.
59    ///
60    /// ```
61    /// # use renox::prelude::*;
62    /// # use serde::{Deserialize, Serialize};
63    /// # #[derive(Serialize, Deserialize)] struct SendReceipt { order_id: i64 }
64    /// # impl Job for SendReceipt { const NAME: &'static str = "send-receipt"; async fn handle(self, _: JobContext) -> Result { Ok(()) } }
65    /// # #[derive(Clone)] struct OrderPlaced { order_id: i64 }
66    /// # impl Event for OrderPlaced {}
67    /// # async fn close_day(_: AppState) -> Result { Ok(()) }
68    /// # struct Orders;
69    /// # impl Module for Orders {
70    /// # fn name(&self) -> &'static str { "orders" }
71    /// fn register(&self, app: &mut Registry) {
72    ///     app.job::<SendReceipt>()
73    ///         .listen(|e: OrderPlaced, state| async move {
74    ///             state.dispatch(SendReceipt { order_id: e.order_id }).await?;
75    ///             Ok(())
76    ///         });
77    ///     app.schedule().daily_at("02:00", "close-day", close_day);
78    /// }
79    /// # }
80    /// ```
81    fn register(&self, _app: &mut Registry) {}
82}