roopes_core/aggregates/handling_publisher/
mod.rs

1//!
2#![cfg_attr(feature = "doc-images",
3  cfg_attr(
4    all(),
5    doc = ::embed_doc_image::embed_image!(
6        "handling-publisher-diagram",
7        "src/aggregates/handling_publisher/handling_publisher.svg"
8)))]
9#![cfg_attr(
10    not(feature = "doc-images"),
11    doc = "**Doc images not enabled**. Compile with feature `doc-images` and \
12           Rust version >= 1.54 to enable."
13)]
14//! The [`handling_publisher`] module creates
15//! [`Handler`]s from arbitrary [`Publisher`]s.
16//!
17//! ![publishing handler diagram][publishing-handler-diagram]
18
19use crate::prelude::{
20    handler::lambda::Delegate,
21    *,
22};
23use std::{
24    fmt::Debug,
25    hash::Hash,
26    marker::PhantomData,
27};
28
29#[cfg(test)]
30mod tests;
31
32/// Exposes the [`HandlingPublisher`] type at the
33/// library level.
34pub mod prelude
35{
36    pub use super::HandlingPublisher;
37}
38
39/// Provides the [`Subscriber`] and [`Handler`]
40/// traits for a wrapped [`Handler`] delegate.
41pub struct HandlingPublisher<P, M>
42where
43    P: Publisher<M>,
44{
45    delegate: P,
46    _retain_types: PhantomData<M>,
47}
48
49impl<P, M> HandlingPublisher<P, M>
50where
51    P: Publisher<M>,
52{
53    /// Creates a [`HandlingPublisher`] from a
54    /// given [`Handler`].
55    pub fn new(delegate: P) -> HandlingPublisher<P, M>
56    {
57        HandlingPublisher {
58            delegate,
59            _retain_types: PhantomData,
60        }
61    }
62}
63
64impl<P, M> Publisher<M> for HandlingPublisher<P, M>
65where
66    P: Publisher<M>,
67{
68    fn publish(
69        &self,
70        message: &M,
71    )
72    {
73        self.delegate.publish(message);
74    }
75}
76
77impl<P, M> Handler<M> for HandlingPublisher<P, M>
78where
79    P: Publisher<M>,
80{
81    fn handle(
82        &self,
83        message: &M,
84    )
85    {
86        self.delegate.publish(message);
87    }
88}
89
90impl<P, M> From<P> for HandlingPublisher<P, M>
91where
92    P: Publisher<M>,
93{
94    fn from(delegate: P) -> Self
95    {
96        HandlingPublisher::new(delegate)
97    }
98}
99
100impl<P, M> PartialEq for HandlingPublisher<P, M>
101where
102    P: Publisher<M> + PartialEq,
103{
104    fn eq(
105        &self,
106        other: &Self,
107    ) -> bool
108    {
109        self.delegate.eq(&other.delegate)
110    }
111}
112
113impl<P, M> Eq for HandlingPublisher<P, M> where P: Publisher<M> + Eq {}
114
115impl<P, M> Debug for HandlingPublisher<P, M>
116where
117    P: Publisher<M> + Debug,
118{
119    fn fmt(
120        &self,
121        f: &mut std::fmt::Formatter<'_>,
122    ) -> std::fmt::Result
123    {
124        f.debug_struct("HandlingPublisher")
125            .field("delegate", &self.delegate)
126            .finish()
127    }
128}
129
130/// Provides the ability to to convert a [`Publisher`] into a
131/// [`HandlingPublisher`], for use as a [`Handler`].
132pub trait IntoHandler<P, M>
133where
134    P: Publisher<M>,
135{
136    /// Wraps  a [`Publisher`] into a [`HandlingPublisher`], for use as a
137    /// [`Handler`].
138    fn into_handler(self) -> HandlingPublisher<P, M>;
139}
140impl<P, M> IntoHandler<P, M> for P
141where
142    P: Publisher<M>,
143{
144    fn into_handler(self) -> HandlingPublisher<P, M>
145    {
146        HandlingPublisher::new(self)
147    }
148}