arcly_stream/engine/
builder.rs1use super::{AppSpec, Engine, EngineConfig};
4use crate::auth::{AllowAll, StreamAuthenticator};
5use crate::observe::{NoopObserver, Observer};
6use std::sync::Arc;
7
8pub struct EngineBuilder {
21 config: EngineConfig,
22 apps: Vec<AppSpec>,
23 observer: Option<Arc<dyn Observer>>,
24 authenticator: Option<Arc<dyn StreamAuthenticator>>,
25}
26
27impl EngineBuilder {
28 pub fn new() -> Self {
30 Self {
31 config: EngineConfig::default(),
32 apps: Vec::new(),
33 observer: None,
34 authenticator: None,
35 }
36 }
37
38 pub fn max_publishers(mut self, n: usize) -> Self {
40 self.config.max_publishers = n;
41 self
42 }
43
44 pub fn config(mut self, config: EngineConfig) -> Self {
46 self.config = config;
47 self
48 }
49
50 pub fn application(mut self, app: AppSpec) -> Self {
52 self.apps.push(app);
53 self
54 }
55
56 pub fn applications(mut self, apps: impl IntoIterator<Item = AppSpec>) -> Self {
58 self.apps.extend(apps);
59 self
60 }
61
62 pub fn observer<O: Observer>(mut self, observer: O) -> Self {
64 self.observer = Some(Arc::new(observer));
65 self
66 }
67
68 pub fn observer_arc(mut self, observer: Arc<dyn Observer>) -> Self {
70 self.observer = Some(observer);
71 self
72 }
73
74 pub fn authenticator<A: StreamAuthenticator>(mut self, auth: A) -> Self {
76 self.authenticator = Some(Arc::new(auth));
77 self
78 }
79
80 pub fn authenticator_arc(mut self, auth: Arc<dyn StreamAuthenticator>) -> Self {
82 self.authenticator = Some(auth);
83 self
84 }
85
86 pub fn idle_timeout(mut self, timeout: std::time::Duration) -> Self {
88 self.config.idle_timeout = Some(timeout);
89 self
90 }
91
92 pub fn build(self) -> Arc<Engine> {
94 let observer = self.observer.unwrap_or_else(|| Arc::new(NoopObserver));
95 let authenticator = self.authenticator.unwrap_or_else(|| Arc::new(AllowAll));
96 Engine::from_parts(self.config, self.apps, observer, authenticator)
97 }
98}
99
100impl Default for EngineBuilder {
101 fn default() -> Self {
102 Self::new()
103 }
104}