ruex/foundation/patterns/mediator/
mediator.rs

1use std::{fmt, rc::Rc};
2
3use crate::{
4    foundation::patterns::facade::BaseFacade,
5    prelude::{Facade, Interest, Mediator, Notification, Notifier, NotifyContext, Singleton, View},
6};
7
8/// A base [Mediator] implementation.
9
10pub struct BaseMediator<Body> {
11    // The view component
12    view_component: Option<Rc<dyn View<Body>>>,
13}
14
15impl<Body> BaseMediator<Body> {
16    /// The name of the [Mediator].
17    ///
18    /// Typically, a [Mediator] will be written to serve
19    /// one specific control or group controls and so,
20    /// will not have a need to be dynamically named.
21
22    /// Constructor.
23    pub fn new(view_component: Option<Rc<dyn View<Body>>>) -> Self {
24        Self { view_component }
25    }
26}
27
28impl<Body> Mediator<Body> for BaseMediator<Body>
29where
30    Body: fmt::Debug + 'static,
31{
32    fn view_component(&self) -> Option<Rc<dyn View<Body>>> {
33        self.view_component.as_ref().cloned()
34    }
35
36    fn handle_notification(&self, _notification: Rc<dyn Notification<Body>>) {}
37
38    fn list_notification_interests(&self) -> &[Interest] {
39        &[]
40    }
41
42    fn on_register(&self) {}
43
44    fn on_remove(&self) {}
45
46    fn set_view_component(&mut self, view_component: Option<Rc<dyn View<Body>>>) {
47        self.view_component = view_component;
48    }
49}
50
51impl<Body> NotifyContext for BaseMediator<Body>
52where
53    Body: fmt::Debug + 'static,
54{
55    fn id(&self) -> u64 {
56        0x01
57    }
58}
59
60impl<Body> Notifier<Body> for BaseMediator<Body>
61where
62    Body: fmt::Debug + 'static,
63{
64    fn send(&self, interest: Interest, body: Option<Body>) {
65        log::error!("You should implement yourself Mediator");
66        BaseFacade::<Body>::global().send(interest, body);
67    }
68}
69
70impl<Body> fmt::Debug for BaseMediator<Body>
71where
72    Body: fmt::Debug + 'static,
73{
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.debug_struct("EmployeesMediator")
76            //  .field("x", &self.x)
77            .finish()
78    }
79}