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
54
55
use crate::blueprint::conversions::raw_identifiers2callable;
use crate::blueprint::middleware::RegisteredPostProcessingMiddleware;
use crate::blueprint::reflection::RawIdentifiers;
use crate::blueprint::Blueprint;
use pavex_bp_schema::Callable;

/// A post-processing middleware that has been configured
/// but has not yet been registered with a [`Blueprint`].
///
/// # Guide
///
/// Check out [`Blueprint::post_process`] for an introduction to post-processing
/// middlewares in Pavex.
///
/// # Use cases
///
/// [`PostProcessingMiddleware`] 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 PostProcessingMiddleware {
    pub(in crate::blueprint) callable: Callable,
    pub(in crate::blueprint) error_handler: Option<Callable>,
}

impl PostProcessingMiddleware {
    /// Create a new (unregistered) post-processing middleware.
    ///
    /// Check out the documentation of [`Blueprint::post_process`] for more details
    /// on middleware.
    #[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 [`RegisteredPostProcessingMiddleware::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::post_process`] for more details.
    pub fn register(self, bp: &mut Blueprint) -> RegisteredPostProcessingMiddleware {
        bp.register_post_processing_middleware(self)
    }
}