1use std::sync::Arc;
20
21use crate::Lifecycle;
25use crate::lifecycle::Context;
26use crate::operations::{OperationCatalog, StartupError};
27
28pub struct Setup<L: Lifecycle> {
37 configuration: <L::Context as Context>::Configuration,
38 replacing: bool,
39}
40
41#[doc(hidden)]
46pub trait SetupConfiguration: Send + 'static {
47 fn resource<T: Send + Sync + 'static>(&mut self, value: Arc<T>, replace: bool);
48 fn validate(&mut self) -> Result<(), StartupError>;
49}
50
51#[doc(hidden)]
52pub trait ApplicationConfiguration: SetupConfiguration {
53 type Lane;
54 type LaneSettings;
55
56 fn lane(&mut self, lane: Self::Lane, settings: Self::LaneSettings);
57 fn operations<C: OperationCatalog>(&mut self, lane: Self::Lane);
58}
59
60impl<L: Lifecycle> Setup<L> {
63 #[doc(hidden)]
64 pub fn new(configuration: <L::Context as Context>::Configuration) -> Self {
65 Self { configuration, replacing: false }
66 }
67
68 pub fn resource<T: Send + Sync + 'static>(&mut self, value: impl Into<Arc<T>>) -> &mut Self {
69 self.configuration.resource(value.into(), self.replacing);
70 self
71 }
72
73 pub fn settings(&mut self) -> &mut <L::Context as Context>::Configuration {
74 &mut self.configuration
75 }
76
77 #[doc(hidden)]
78 pub fn overrides(&mut self, update: impl FnOnce(&mut Self)) {
79 self.replacing = true;
80 update(self);
81 self.replacing = false;
82 }
83
84 #[doc(hidden)]
85 pub fn validate(&mut self) -> Result<(), StartupError> {
86 self.configuration.validate()
87 }
88
89 #[doc(hidden)]
90 pub fn into_configuration(self) -> <L::Context as Context>::Configuration {
91 self.configuration
92 }
93}
94
95impl<L: Lifecycle> Setup<L>
96where
97 <L::Context as Context>::Configuration: ApplicationConfiguration,
98{
99 pub fn lane(
100 &mut self, lane: <<L::Context as Context>::Configuration as ApplicationConfiguration>::Lane,
101 settings: <<L::Context as Context>::Configuration as ApplicationConfiguration>::LaneSettings,
102 ) -> &mut Self {
103 self.configuration.lane(lane, settings);
104 self
105 }
106
107 pub fn operations<C: OperationCatalog>(
108 &mut self, lane: <<L::Context as Context>::Configuration as ApplicationConfiguration>::Lane,
109 ) -> &mut Self {
110 self.configuration.operations::<C>(lane);
111 self
112 }
113}
114
115