use crate::blueprint::constructor::{CloningStrategy, Lifecycle, RegisteredConstructor};
use crate::blueprint::conversions::{lint2lint, raw_callable2registered_callable};
use crate::blueprint::linter::Lint;
use crate::blueprint::reflection::RawCallable;
use crate::blueprint::Blueprint;
use pavex_bp_schema::{Callable, LintSetting};
use std::collections::BTreeMap;
#[derive(Clone, Debug)]
pub struct Constructor {
pub(in crate::blueprint) callable: Callable,
pub(in crate::blueprint) lifecycle: Lifecycle,
pub(in crate::blueprint) cloning_strategy: Option<CloningStrategy>,
pub(in crate::blueprint) error_handler: Option<Callable>,
pub(in crate::blueprint) lints: BTreeMap<pavex_bp_schema::Lint, LintSetting>,
}
impl Constructor {
#[track_caller]
pub fn new(callable: RawCallable, lifecycle: Lifecycle) -> Self {
Self {
callable: raw_callable2registered_callable(callable),
lifecycle,
cloning_strategy: None,
error_handler: None,
lints: Default::default(),
}
}
#[track_caller]
pub fn singleton(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::Singleton)
}
#[track_caller]
pub fn request_scoped(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::RequestScoped)
}
#[track_caller]
pub fn transient(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::Transient)
}
#[track_caller]
pub fn error_handler(mut self, error_handler: RawCallable) -> Self {
self.error_handler = Some(raw_callable2registered_callable(error_handler));
self
}
pub fn cloning(mut self, cloning_strategy: CloningStrategy) -> Self {
self.cloning_strategy = Some(cloning_strategy);
self
}
pub fn ignore(mut self, lint: Lint) -> Self {
self.lints.insert(lint2lint(lint), LintSetting::Ignore);
self
}
pub fn enforce(mut self, lint: Lint) -> Self {
self.lints.insert(lint2lint(lint), LintSetting::Enforce);
self
}
pub fn register(self, bp: &mut Blueprint) -> RegisteredConstructor {
bp.register_constructor(self)
}
}