use crate::blueprint::ErrorHandler;
use crate::blueprint::Lint;
use crate::blueprint::conversions::{
cloning2cloning, coordinates2coordinates, lifecycle2lifecycle, lint2lint,
};
use pavex_bp_schema::Component;
use pavex_bp_schema::{Blueprint as BlueprintSchema, LintSetting, Location};
use super::CloningPolicy;
use super::Lifecycle;
use super::reflection::AnnotationCoordinates;
pub struct Constructor {
#[doc(hidden)]
pub coordinates: AnnotationCoordinates,
}
pub struct RegisteredConstructor<'a> {
pub(crate) blueprint: &'a mut BlueprintSchema,
pub(crate) component_id: usize,
}
impl RegisteredConstructor<'_> {
#[track_caller]
pub fn error_handler(mut self, error_handler: ErrorHandler) -> Self {
let error_handler = pavex_bp_schema::ErrorHandler {
coordinates: coordinates2coordinates(error_handler.coordinates),
registered_at: Location::caller(),
};
self.constructor().error_handler = Some(error_handler);
self
}
pub fn lifecycle(mut self, lifecycle: Lifecycle) -> Self {
self.constructor().lifecycle = Some(lifecycle2lifecycle(lifecycle));
self
}
pub fn cloning(mut self, strategy: CloningPolicy) -> Self {
self.constructor().cloning_policy = Some(cloning2cloning(strategy));
self
}
pub fn clone_if_necessary(self) -> Self {
self.cloning(CloningPolicy::CloneIfNecessary)
}
pub fn never_clone(self) -> Self {
self.cloning(CloningPolicy::NeverClone)
}
pub fn allow(mut self, lint: Lint) -> Self {
self.constructor()
.lints
.insert(lint2lint(lint), LintSetting::Allow);
self
}
pub fn warn(mut self, lint: Lint) -> Self {
self.constructor()
.lints
.insert(lint2lint(lint), LintSetting::Warn);
self
}
pub fn deny(mut self, lint: Lint) -> Self {
self.constructor()
.lints
.insert(lint2lint(lint), LintSetting::Deny);
self
}
fn constructor(&mut self) -> &mut pavex_bp_schema::Constructor {
let component = &mut self.blueprint.components[self.component_id];
let Component::Constructor(c) = component else {
unreachable!("The component should be a constructor")
};
c
}
}