use std::any::type_name;
use std::marker::PhantomData;
use std::panic::Location;
use std::sync::Arc;
use crate::binding::factory::{Factory, erase_factory, erase_try_factory};
use crate::binding::handle::{Contribution, Handle, Open, Set};
use crate::binding::{BindingRecord, Coercion, ErasedCtor, Qualifier, Recipe, coercion, erase_construct, instance_of};
use crate::construct::Construct;
use crate::dependency::Dependencies;
use crate::hooks::erase_trait_hooks;
use crate::key::{BindingKind, Key};
use crate::module::def::{ModuleNode, ValueFailure};
use crate::scope::{Auto, PerExecution, Scope, ScopeKind, Singleton, Transient};
use crate::timer::BoxError;
pub struct Contribute<'m, U: ?Sized, Q = (), M = Plain> {
node: &'m mut ModuleNode,
enhancer: bool,
_u: PhantomData<fn() -> (PhantomData<U>, Q, M)>,
}
pub enum Plain {}
pub enum Enhancer {}
impl<'m, U: ?Sized + Send + Sync + 'static, Q: 'static> Contribute<'m, U, Q, Plain> {
pub(crate) fn new(node: &'m mut ModuleNode) -> Self {
Contribute { node, enhancer: false, _u: PhantomData }
}
pub fn qualified<Q2: 'static>(self) -> Contribute<'m, U, Q2, Plain> {
Contribute { node: self.node, enhancer: self.enhancer, _u: PhantomData }
}
}
impl<'m, U: ?Sized + Send + Sync + 'static> Contribute<'m, U, (), Enhancer> {
pub(crate) fn for_role(node: &'m mut ModuleNode) -> Self {
Contribute { node, enhancer: true, _u: PhantomData }
}
}
impl<'m, U: ?Sized + Send + Sync + 'static, Q: 'static, M> Contribute<'m, U, Q, M> {
fn record(
built: &'static str,
scope: ScopeKind,
recipe: Recipe,
dependencies: Dependencies,
into_primary: Coercion,
location: &'static Location<'static>,
) -> BindingRecord {
let mut record =
BindingRecord::new(Key::of::<U, ()>(), built, BindingKind::Collection, scope, recipe, dependencies, location);
record.qualifier = Qualifier::of::<Q>();
record.into_primary = Some(into_primary);
record
}
fn push<T: ?Sized, K>(self, record: BindingRecord) -> Handle<'m, T, K> {
let node = self.node;
let index = node.bindings.len();
node.bindings.push(record);
if self.enhancer {
node.enhancers.push(index);
}
Handle::new(&mut node.bindings[index])
}
fn push_factory<T: Send + Sync + 'static, S: Scope, Args, F: Factory<Args>, K>(
self,
ctor: ErasedCtor,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
location: &'static Location<'static>,
) -> Handle<'m, T, K> {
let mut dependencies = Dependencies::default();
<F as Factory<Args>>::dependencies(&mut dependencies);
let into_primary = coercion::<T, U, _>(coerce);
let record = Self::record(type_name::<T>(), S::KIND, Recipe::Factory(ctor), dependencies, into_primary, location);
self.push(record)
}
#[track_caller]
pub fn provide<T: Construct>(
self,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, T, Contribution<T::Scope, Set>> {
let location = Location::caller();
let mut dependencies = Dependencies::default();
T::dependencies(&mut dependencies);
let mut record = Self::record(
type_name::<T>(),
<T::Scope as Scope>::KIND,
Recipe::Construct(erase_construct::<T>()),
dependencies,
coercion::<T, U, _>(coerce),
location,
);
record.construct_bound = T::CONSTRUCT_TIMEOUT;
record.hooks = erase_trait_hooks::<T>(location);
record.constructs = true;
self.push(record)
}
#[track_caller]
pub fn value(self, value: Arc<U>) -> Handle<'m, U, Contribution<Singleton, Set>> {
let location = Location::caller();
let record = Self::record(
type_name::<U>(),
ScopeKind::Singleton,
Recipe::Value(instance_of(value)),
Dependencies::default(),
coercion::<U, U, _>(|a| a),
location,
);
self.push(record)
}
#[track_caller]
pub fn try_value<E: Into<BoxError>>(self, value: Result<Arc<U>, E>) -> Handle<'m, U, Contribution<Singleton, Set>> {
let location = Location::caller();
let recipe = match value {
Ok(value) => Recipe::Value(instance_of(value)),
Err(error) => {
self.node.failures.push(ValueFailure { key: Key::of::<U, Q>(), error: error.into(), location });
Recipe::Failed
}
};
let record = Self::record(
type_name::<U>(),
ScopeKind::Singleton,
recipe,
Dependencies::default(),
coercion::<U, U, _>(|a| a),
location,
);
self.push(record)
}
#[track_caller]
pub fn with<Args, F>(
self,
factory: F,
coerce: impl Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, F::Output, Contribution<Auto, Open>>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
let ctor = erase_factory::<Args, F>(factory);
self.push_factory::<F::Output, Auto, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn try_with<Args, F, T, E>(
self,
factory: F,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, T, Contribution<Auto, Open>>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
let ctor = erase_try_factory::<Args, F, T, E>(factory);
self.push_factory::<T, Auto, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn singleton<Args, F>(
self,
factory: F,
coerce: impl Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, F::Output, Contribution<Singleton, Open>>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
let ctor = erase_factory::<Args, F>(factory);
self.push_factory::<F::Output, Singleton, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn try_singleton<Args, F, T, E>(
self,
factory: F,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, T, Contribution<Singleton, Open>>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
let ctor = erase_try_factory::<Args, F, T, E>(factory);
self.push_factory::<T, Singleton, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn execution<Args, F>(
self,
factory: F,
coerce: impl Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, F::Output, Contribution<PerExecution, Open>>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
let ctor = erase_factory::<Args, F>(factory);
self.push_factory::<F::Output, PerExecution, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn try_execution<Args, F, T, E>(
self,
factory: F,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, T, Contribution<PerExecution, Open>>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
let ctor = erase_try_factory::<Args, F, T, E>(factory);
self.push_factory::<T, PerExecution, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn transient<Args, F>(
self,
factory: F,
coerce: impl Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, F::Output, Contribution<Transient, Open>>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
let ctor = erase_factory::<Args, F>(factory);
self.push_factory::<F::Output, Transient, Args, F, _>(ctor, coerce, Location::caller())
}
#[track_caller]
pub fn try_transient<Args, F, T, E>(
self,
factory: F,
coerce: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
) -> Handle<'m, T, Contribution<Transient, Open>>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
let ctor = erase_try_factory::<Args, F, T, E>(factory);
self.push_factory::<T, Transient, Args, F, _>(ctor, coerce, Location::caller())
}
}