//! Registry for sinks and sources. //! //! This module provides the `EndpointRegistry` object which associates each //! event sink, event source and query source to a unique name. mod event_sink_registry; mod event_source_registry; mod query_source_registry; use serde::{de::DeserializeOwned, ser::Serialize}; use crate::ports::{EventSinkStream, EventSource, QuerySource}; pub(crate) use event_sink_registry::EventSinkRegistry; pub(crate) use event_source_registry::EventSourceRegistry; pub(crate) use query_source_registry::QuerySourceRegistry; /// A registry that holds all sources and sinks meant to be accessed through /// bindings or remote procedure calls. #[derive(Default, Debug)] pub struct EndpointRegistry { pub(crate) event_sink_registry: EventSinkRegistry, pub(crate) event_source_registry: EventSourceRegistry, pub(crate) query_source_registry: QuerySourceRegistry, } impl EndpointRegistry { /// Creates a new, empty registry. pub fn new() -> Self { Self::default() } /// Adds an event source to the registry. /// /// If the specified name is already in use for another event source, the source /// provided as argument is returned in the error. pub fn add_event_source( &mut self, source: EventSource, name: impl Into, ) -> Result<(), EventSource> where T: DeserializeOwned + Clone + Send + 'static, { self.event_source_registry.add(source, name) } /// Adds a query source to the registry. /// /// If the specified name is already in use for another query source, the /// source provided as argument is returned in the error. pub fn add_query_source( &mut self, source: QuerySource, name: impl Into, ) -> Result<(), QuerySource> where T: DeserializeOwned + Clone + Send + 'static, R: Serialize + Send + 'static, { self.query_source_registry.add(source, name) } /// Adds an event sink to the registry. /// /// If the specified name is already in use for another event sink, the /// event sink provided as argument is returned in the error. pub fn add_event_sink(&mut self, sink: S, name: impl Into) -> Result<(), S> where S: EventSinkStream + Send + 'static, S::Item: Serialize, { self.event_sink_registry.add(sink, name) } }