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
use crate::component::Interface;
use crate::module::{ComponentMap, ParameterMap};
use crate::parameters::ComponentParameters;
use crate::provider::ProviderFn;
use crate::{Component, ComponentFn, HasComponent, HasProvider, Module, ModuleBuildContext};
use std::marker::PhantomData;
use std::sync::Arc;
pub struct ModuleBuilder<M: Module> {
parameters: ParameterMap,
submodules: M::Submodules,
component_overrides: ComponentMap,
component_fn_overrides: ComponentMap,
provider_overrides: ComponentMap,
_module: PhantomData<M>,
}
impl<M: Module> ModuleBuilder<M> {
pub fn with_submodules(submodules: M::Submodules) -> Self {
ModuleBuilder {
parameters: ParameterMap::new(),
submodules,
component_overrides: ComponentMap::new(),
component_fn_overrides: ComponentMap::new(),
provider_overrides: ComponentMap::new(),
_module: PhantomData,
}
}
pub fn with_component_parameters<C: Component<M>>(mut self, params: C::Parameters) -> Self
where
M: HasComponent<C::Interface>,
{
self.parameters
.insert(ComponentParameters::<C, C::Parameters>::new(params));
self
}
pub fn with_component_override<I: Interface + ?Sized>(mut self, component: Box<I>) -> Self
where
M: HasComponent<I>,
{
self.component_overrides
.insert::<Arc<I>>(Arc::from(component));
self
}
pub fn with_component_override_fn<I: Interface + ?Sized>(
mut self,
component_fn: ComponentFn<M, I>,
) -> Self
where
M: HasComponent<I>,
{
self.component_fn_overrides.insert(component_fn);
self
}
pub fn with_provider_override<I: 'static + ?Sized>(
mut self,
provider_fn: ProviderFn<M, I>,
) -> Self
where
M: HasProvider<I>,
{
self.provider_overrides.insert(Arc::new(provider_fn));
self
}
pub fn build(self) -> M {
M::build(ModuleBuildContext::new(
self.parameters,
self.component_overrides,
self.component_fn_overrides,
self.provider_overrides,
self.submodules,
))
}
}