rok-container 0.3.4

IoC service container and dependency injection for the rok ecosystem
Documentation
use thiserror::Error;

/// Errors returned by [`Container`](crate::Container) operations and Axum extractors.
#[derive(Error, Debug)]
pub enum ContainerError {
    /// The requested type was never registered with `bind()` or `singleton()`.
    #[error("service not registered: {0}")]
    NotRegistered(&'static str),

    /// Internal type-map downcast failed — indicates a container bug, not user error.
    #[error("type mismatch when resolving: {0}")]
    TypeMismatch(&'static str),

    /// A required route path parameter was absent.
    #[error("missing route parameter: {0}")]
    MissingRouteParam(&'static str),

    /// Route model binding found no record matching the route key.
    #[error("model not found")]
    ModelNotFound,
}

#[cfg(feature = "axum")]
impl axum::response::IntoResponse for ContainerError {
    fn into_response(self) -> axum::response::Response {
        use axum::http::StatusCode;

        let status = match &self {
            ContainerError::ModelNotFound => StatusCode::NOT_FOUND,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

        axum::response::IntoResponse::into_response((status, self.to_string()))
    }
}