mediator_sys/mediator/synchronous/basic/builder.rs
1use super::{basic::BasicMediator, interface::BasicMediatorBuilderInterface};
2use crate::mediator::{
3 builder::{BuilderFlow, BuilderInternal},
4 listener::Listener,
5};
6use std::{fmt::Debug, sync::mpsc::channel};
7
8/// The [`BasicBuilder`] helps you to create a [`BasicMediator`].
9///
10/// The [`BasicBuilder`] is part of the builder pattern.
11/// It has only two functionalities. The first one is adding a [`Listener`] via
12/// [`BasicBuilder::add_listener()`].
13/// The second one is the mandatory [`BuilderFlow::build()`], which returns
14/// a [`BasicMediator`].
15///
16pub struct BasicBuilder<Ev>
17where
18 Ev: Debug,
19{
20 mediator: BasicMediator<Ev>,
21}
22
23impl<Ev> BuilderInternal<BasicMediator<Ev>, BasicBuilder<Ev>> for BasicMediator<Ev>
24where
25 Ev: Debug,
26{
27 /// Creates a [`BasicBuilder`] with the goal of producing a [`BasicMediator`].
28 ///
29 fn builder() -> BasicBuilder<Ev> {
30 BasicBuilder::<Ev> {
31 mediator: BasicMediator::<Ev> {
32 channel: channel(),
33 listener: vec![],
34 },
35 }
36 }
37}
38
39impl<M, Ev> BasicMediatorBuilderInterface<M, Ev> for BasicBuilder<Ev>
40where
41 Ev: Debug,
42{
43 /// Adds a user-defined listener to the [`BasicBuilder`].
44 ///
45 /// To be able to supply a closure that implements [`Listener`],
46 /// it must satisfy [`Send`] and `'static` bounds.
47 ///
48 /// Also it must be a `Fn(Ev)` with a return type of `()`
49 /// where `Ev` is the user-defined event type
50 /// that must be [`Clone`] and [`Debug`].
51 ///
52 fn add_listener<F>(mut self, f: F) -> Self
53 where
54 F: Listener<Ev>,
55 {
56 self.mediator.listener.push(Box::new(f));
57 self
58 }
59}
60
61impl<Ev> BasicBuilder<Ev>
62where
63 Ev: Debug,
64{
65 /// Adds a user-defined listener to the [`BasicBuilder`].
66 ///
67 /// The supplied type must be a [`Listener`].
68 /// As such, it must implement [`Send`] and `Fn(Ev)`,
69 /// besides being `'static`.
70 ///
71 /// As a side note, here, `Ev` is the user-defined event type
72 /// that must be [`Clone`] and [`Debug`].
73 ///
74 /// # Examples
75 ///
76 /// Basic usage:
77 ///
78 /// ```
79 /// use mediator_sys::synchronous::basic::*;
80 ///
81 /// #[derive(Debug, Clone)]
82 /// enum MyEvent {
83 /// One,
84 /// Two
85 /// }
86 ///
87 /// let mediator = BasicMediator::<MyEvent>::builder()
88 /// .add_listener(|ev| {
89 /// /* Your listening logic */
90 /// })
91 /// .build();
92 ///
93 pub fn add_listener<F>(self, f: F) -> Self
94 where
95 F: Listener<Ev>,
96 {
97 <Self as BasicMediatorBuilderInterface<BasicMediator<Ev>, Ev>>::add_listener(self, f)
98 }
99}
100
101impl<Ev> BuilderFlow<BasicMediator<Ev>> for BasicBuilder<Ev>
102where
103 Ev: Debug,
104{
105 /// Builds the [`BasicMediator`] and returns it.
106 ///
107 /// Because [`BasicMediator`] implements [`BuilderInternal`],
108 /// which in turn means, that the [`BasicBuilder`] implements [`BuilderFlow`]
109 /// and not [`crate::builder::TryBuilderFlow`], this method will
110 /// always return a [`BasicMediator`] as stated by the return type.
111 ///
112 fn build(self) -> BasicMediator<Ev> {
113 self.mediator
114 }
115}