Skip to main content

harmos_sdk/
setup.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Lifecycle Preparation And Role-Specific Configuration
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19use std::sync::Arc;
20
21// Third Party Imports
22
23// Local Imports
24use crate::Lifecycle;
25use crate::lifecycle::Context;
26use crate::operations::{OperationCatalog, StartupError};
27
28/* === Constants === */
29
30/* === Macros === */
31
32/* === Type Aliases === */
33
34/* === Structs === */
35
36pub struct Setup<L: Lifecycle> {
37    configuration: <L::Context as Context>::Configuration,
38    replacing: bool,
39}
40
41/* === Enums === */
42
43/* === Traits === */
44
45#[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
60/* === Implementations === */
61
62impl<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/* === Public Functions === */
116
117/* === Private Functions === */
118
119/* === Tests === */