elif_http/bootstrap/
app_module.rs

1//! AppBootstrap trait and bootstrap implementation for app modules
2
3use crate::{bootstrap::AppBootstrapper, HttpError};
4
5/// Trait for app modules that can bootstrap themselves
6///
7/// This trait is automatically implemented for modules marked with `#[module]`
8/// that are designated as app modules (typically the root module).
9pub trait AppBootstrap {
10    /// Start the bootstrap process for this app module
11    ///
12    /// This method discovers all modules in the dependency tree, configures
13    /// the DI container, registers all controllers, and returns an AppBootstrapper
14    /// ready for server startup.
15    ///
16    /// Returns a `BootstrapResult<AppBootstrapper>` to allow proper error handling
17    /// instead of panicking on configuration errors.
18    fn bootstrap() -> BootstrapResult<AppBootstrapper>;
19}
20
21/// Error type for bootstrap operations
22#[derive(Debug, thiserror::Error)]
23pub enum BootstrapError {
24    #[error("Module discovery failed: {message}")]
25    ModuleDiscoveryFailed { message: String },
26    
27    #[error("Circular dependency detected: {cycle:?}")]
28    CircularDependency { cycle: Vec<String> },
29    
30    #[error("Missing dependency: module '{module}' depends on '{dependency}' which is not registered")]
31    MissingDependency { module: String, dependency: String },
32    
33    #[error("Module registration failed: {message}")]
34    ModuleRegistrationFailed { message: String },
35    
36    #[error("Container configuration failed: {message}")]
37    ContainerConfigurationFailed { message: String },
38    
39    #[error("Route registration failed: {message}")]
40    RouteRegistrationFailed { message: String },
41    
42    #[error("Controller not found: '{controller_name}'. Available controllers: {available_controllers:?}")]
43    ControllerNotFound { controller_name: String, available_controllers: Vec<String> },
44    
45    #[error("Controller registration failed: {message}")]
46    ControllerRegistrationFailed { message: String },
47    
48    #[error("Controller metadata extraction failed: {message}")]
49    ControllerMetadataFailed { message: String },
50    
51    #[error("Server startup failed: {message}")]
52    ServerStartupFailed { message: String },
53    
54    #[error("HTTP error during bootstrap: {0}")]
55    HttpError(#[from] HttpError),
56}
57
58impl From<BootstrapError> for HttpError {
59    fn from(error: BootstrapError) -> Self {
60        match error {
61            BootstrapError::HttpError(http_error) => http_error,
62            _ => HttpError::InternalError { message: format!("Bootstrap failed: {}", error) },
63        }
64    }
65}
66
67/// Result type for bootstrap operations
68pub type BootstrapResult<T> = Result<T, BootstrapError>;