capy-core 0.4.0

Typed configuration, application context, and errors for the Capy web framework.
Documentation
//! Typed configuration, shared application state, and safe application errors.

mod config;
mod error;

pub use config::{AppConfig, AppSettings, DatabaseSettings, HttpSettings, ServerSettings};
pub use error::{AppError, ErrorKind, Result, ValidationErrors};
use std::sync::Arc;

/// Cheaply cloned state shared by every request.
#[derive(Clone, Debug)]
pub struct AppContext {
    /// Validated, immutable application configuration.
    pub config: Arc<AppConfig>,
    /// SeaORM's native connection pool.
    pub db: sea_orm::DatabaseConnection,
}

/// Extract typed application state using `Context(ctx): Context`.
#[derive(Clone, Debug)]
pub struct Context(pub AppContext);

impl axum::extract::FromRequestParts<AppContext> for Context {
    type Rejection = std::convert::Infallible;

    async fn from_request_parts(
        _parts: &mut axum::http::request::Parts,
        state: &AppContext,
    ) -> std::result::Result<Self, Self::Rejection> {
        Ok(Self(state.clone()))
    }
}