pavex/blueprint/middleware/pre/
unregistered.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::blueprint::conversions::raw_identifiers2callable;
use crate::blueprint::middleware::pre::RegisteredPreProcessingMiddleware;
use crate::blueprint::reflection::RawIdentifiers;
use crate::blueprint::Blueprint;
use pavex_bp_schema::Callable;

/// A pre-processing middleware that has been configured but has not yet been registered with a [`Blueprint`].
///
/// # Guide
///
/// Check out [`Blueprint::pre_process`] for an introduction to pre_processing middlewares in Pavex.
///
/// # Use cases
///
/// [`PreProcessingMiddleware`] is primarily used by
/// [kits](https://pavex.dev/docs/guide/dependency_injection/kits)
/// to allow users to customize (or disable!)
/// the bundled middlewares **before** registering them with a [`Blueprint`].
#[derive(Clone, Debug)]
pub struct PreProcessingMiddleware {
    pub(in crate::blueprint) callable: Callable,
    pub(in crate::blueprint) error_handler: Option<Callable>,
}

impl PreProcessingMiddleware {
    /// Create a new (unregistered) pre_processing middleware.
    ///
    /// Check out the documentation of [`Blueprint::pre_process`] for more details
    /// on pre-processing middlewares.
    #[track_caller]
    pub fn new(callable: RawIdentifiers) -> Self {
        Self {
            callable: raw_identifiers2callable(callable),
            error_handler: None,
        }
    }

    /// Register an error handler for this middleware.
    ///
    /// Check out the documentation of [`RegisteredPreProcessingMiddleware::error_handler`] for more details.
    #[track_caller]
    pub fn error_handler(mut self, error_handler: RawIdentifiers) -> Self {
        self.error_handler = Some(raw_identifiers2callable(error_handler));
        self
    }

    /// Register this middleware with a [`Blueprint`].
    ///
    /// Check out the documentation of [`Blueprint::pre_process`] for more details.
    pub fn register(self, bp: &mut Blueprint) -> RegisteredPreProcessingMiddleware {
        bp.register_pre_processing_middleware(self)
    }
}