Skip to main content

capy_core/
lib.rs

1//! Typed configuration, shared application state, and safe application errors.
2
3mod config;
4mod error;
5
6pub use config::{AppConfig, AppSettings, DatabaseSettings, HttpSettings, ServerSettings};
7pub use error::{AppError, ErrorKind, Result, ValidationErrors};
8use std::sync::Arc;
9
10/// Cheaply cloned state shared by every request.
11#[derive(Clone, Debug)]
12pub struct AppContext {
13    /// Validated, immutable application configuration.
14    pub config: Arc<AppConfig>,
15    /// SeaORM's native connection pool.
16    pub db: sea_orm::DatabaseConnection,
17}
18
19/// Extract typed application state using `Context(ctx): Context`.
20#[derive(Clone, Debug)]
21pub struct Context(pub AppContext);
22
23impl axum::extract::FromRequestParts<AppContext> for Context {
24    type Rejection = std::convert::Infallible;
25
26    async fn from_request_parts(
27        _parts: &mut axum::http::request::Parts,
28        state: &AppContext,
29    ) -> std::result::Result<Self, Self::Rejection> {
30        Ok(Self(state.clone()))
31    }
32}