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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
use crate::blueprint::constructor::CloningStrategy;
use crate::blueprint::conversions::{cloning2cloning, lint2lint, raw_identifiers2callable};
use crate::blueprint::linter::Lint;
use crate::blueprint::reflection::RawIdentifiers;
use pavex_bp_schema::{Blueprint as BlueprintSchema, LintSetting};
use pavex_bp_schema::{Component, Constructor};
/// The type returned by [`Blueprint::constructor`].
///
/// It allows you to further configure the behaviour of the registered constructor.
///
/// [`Blueprint::constructor`]: crate::blueprint::Blueprint::constructor
pub struct RegisteredConstructor<'a> {
pub(crate) blueprint: &'a mut BlueprintSchema,
/// The index of the registered constructor in the blueprint's `components` vector.
pub(crate) component_id: usize,
}
impl<'a> RegisteredConstructor<'a> {
#[track_caller]
/// Register an error handler.
///
/// If an error handler has already been registered for this constructor, it will be
/// overwritten.
///
/// # Guide
///
/// Check out the ["Error handlers"](https://pavex.dev/docs/guide/errors/error_handlers)
/// section of Pavex's guide for a thorough introduction to error handlers
/// in Pavex applications.
///
/// # Example
///
/// ```rust
/// use pavex::f;
/// use pavex::blueprint::{Blueprint, constructor::Lifecycle};
/// use pavex::response::Response;
/// # struct LogLevel;
/// # struct Logger;
/// # struct ConfigurationError;
///
/// // 👇 a fallible constructor
/// fn logger() -> Result<Logger, ConfigurationError> {
/// // [...]
/// # todo!()
/// }
///
/// fn error_to_response(error: &ConfigurationError, log_level: LogLevel) -> Response {
/// // [...]
/// # todo!()
/// }
///
/// # fn main() {
/// let mut bp = Blueprint::new();
/// bp.constructor(f!(crate::logger), Lifecycle::Transient)
/// .error_handler(f!(crate::error_to_response));
/// # }
/// ```
pub fn error_handler(mut self, error_handler: RawIdentifiers) -> Self {
let callable = raw_identifiers2callable(error_handler);
self.constructor().error_handler = Some(callable);
self
}
/// Set the cloning strategy for the output type returned by this constructor.
///
/// By default,
/// Pavex will **never** try to clone the output type returned by a constructor.
/// If the output type implements [`Clone`], you can change the default by setting the cloning strategy
/// to [`CloningStrategy::CloneIfNecessary`]: Pavex will clone the output type if
/// it's necessary to generate code that satisfies Rust's borrow checker.
pub fn cloning(mut self, strategy: CloningStrategy) -> Self {
self.constructor().cloning_strategy = Some(cloning2cloning(strategy));
self
}
/// Set the cloning strategy to [`CloningStrategy::CloneIfNecessary`].
/// Check out [`RegisteredConstructor::cloning`] for more details.
pub fn clone_if_necessary(self) -> Self {
self.cloning(CloningStrategy::CloneIfNecessary)
}
/// Set the cloning strategy to [`CloningStrategy::NeverClone`].
/// Check out [`RegisteredConstructor::cloning`] for more details.
pub fn never_clone(self) -> Self {
self.cloning(CloningStrategy::NeverClone)
}
/// Tell Pavex to ignore a specific [`Lint`] when analysing
/// this constructor and the way it's used.
pub fn ignore(mut self, lint: Lint) -> Self {
self.constructor()
.lints
.insert(lint2lint(lint), LintSetting::Ignore);
self
}
/// Tell Pavex to enforce a specific [`Lint`] when analysing
/// this constructor and the way it's used.
pub fn enforce(mut self, lint: Lint) -> Self {
self.constructor()
.lints
.insert(lint2lint(lint), LintSetting::Enforce);
self
}
fn constructor(&mut self) -> &mut Constructor {
let component = &mut self.blueprint.components[self.component_id];
let Component::Constructor(c) = component else {
unreachable!("The component should be a constructor")
};
c
}
}