Skip to main content

caelix_core/
controller.rs

1use std::{any::Any, sync::Arc};
2
3use crate::{Container, ProviderDependency, Result};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6/// Public Caelix type `RouteDef`.
7pub struct RouteDef {
8    /// The `method` value.
9    pub method: &'static str,
10    /// The `path` value.
11    pub path: &'static str,
12    /// The `handler` value.
13    pub handler: &'static str,
14}
15
16/// Public Caelix extension trait `Controller`.
17pub trait Controller {
18    /// Public Caelix API.
19    fn base_path() -> &'static str;
20
21    /// Providers resolved by generated route wrappers, such as guards and
22    /// interceptors. These participate in module visibility validation and
23    /// lifecycle ordering just like constructor-injected dependencies.
24    fn route_dependencies() -> Vec<ProviderDependency> {
25        vec![]
26    }
27
28    /// Validates generated route configuration during module discovery.
29    #[doc(hidden)]
30    fn validate_routes() -> Result<()> {
31        Ok(())
32    }
33
34    /// Public Caelix API.
35    fn routes() -> &'static [RouteDef] {
36        &[]
37    }
38    /// Public Caelix API.
39    fn register_routes(cfg: &mut dyn Any);
40
41    /// Registers routes with the initialized application container.
42    #[doc(hidden)]
43    fn register_routes_with_container(cfg: &mut dyn Any, _container: Arc<Container>) {
44        Self::register_routes(cfg);
45    }
46
47    #[cfg(feature = "openapi")]
48    #[doc(hidden)]
49    fn openapi_routes() -> &'static [crate::openapi::OpenApiRouteDef] {
50        &[]
51    }
52}