use super::{AppSpec, Engine, EngineConfig};
use crate::auth::{AllowAll, StreamAuthenticator};
use crate::observe::{NoopObserver, Observer};
use std::sync::Arc;
pub struct EngineBuilder {
config: EngineConfig,
apps: Vec<AppSpec>,
observer: Option<Arc<dyn Observer>>,
authenticator: Option<Arc<dyn StreamAuthenticator>>,
}
impl EngineBuilder {
pub fn new() -> Self {
Self {
config: EngineConfig::default(),
apps: Vec::new(),
observer: None,
authenticator: None,
}
}
pub fn max_publishers(mut self, n: usize) -> Self {
self.config.max_publishers = n;
self
}
pub fn config(mut self, config: EngineConfig) -> Self {
self.config = config;
self
}
pub fn application(mut self, app: AppSpec) -> Self {
self.apps.push(app);
self
}
pub fn applications(mut self, apps: impl IntoIterator<Item = AppSpec>) -> Self {
self.apps.extend(apps);
self
}
pub fn observer<O: Observer>(mut self, observer: O) -> Self {
self.observer = Some(Arc::new(observer));
self
}
pub fn observer_arc(mut self, observer: Arc<dyn Observer>) -> Self {
self.observer = Some(observer);
self
}
pub fn authenticator<A: StreamAuthenticator>(mut self, auth: A) -> Self {
self.authenticator = Some(Arc::new(auth));
self
}
pub fn authenticator_arc(mut self, auth: Arc<dyn StreamAuthenticator>) -> Self {
self.authenticator = Some(auth);
self
}
pub fn idle_timeout(mut self, timeout: std::time::Duration) -> Self {
self.config.idle_timeout = Some(timeout);
self
}
pub fn build(self) -> Arc<Engine> {
let observer = self.observer.unwrap_or_else(|| Arc::new(NoopObserver));
let authenticator = self.authenticator.unwrap_or_else(|| Arc::new(AllowAll));
Engine::from_parts(self.config, self.apps, observer, authenticator)
}
}
impl Default for EngineBuilder {
fn default() -> Self {
Self::new()
}
}