1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::{cell::RefCell, fmt, rc::Rc};
use crate::{
foundation::patterns::facade::BaseFacade,
prelude::{Command, Facade, Interest, Notification, Notifier, Singleton},
};
pub struct MacroCommand<Body>
where
Body: fmt::Debug + 'static,
{
sub_commands: RefCell<Vec<Box<dyn Command<Body>>>>,
}
impl<Body> MacroCommand<Body>
where
Body: fmt::Debug + 'static,
{
pub fn new() -> Self {
Self {
sub_commands: RefCell::new(Vec::new()),
}
}
pub fn add_sub_command(&mut self, command: Box<dyn Command<Body>>) {
let mut sub_commands = self.sub_commands.borrow_mut();
sub_commands.push(command);
}
}
impl<Body> Command<Body> for MacroCommand<Body>
where
Body: fmt::Debug + 'static,
{
fn execute(&self, notification: Rc<dyn Notification<Body>>) {
let mut sub_commands = self.sub_commands.borrow_mut();
while let Some(command) = sub_commands.pop() {
command.execute(notification.clone());
}
}
}
impl<Body> Notifier<Body> for MacroCommand<Body>
where
Body: fmt::Debug + 'static,
{
fn send(&self, interest: Interest, body: Option<Body>) {
log::error!("You should implement yourself MacroCommand");
BaseFacade::<Body>::global().send(interest, body);
}
}
impl<Body> fmt::Debug for MacroCommand<Body>
where
Body: fmt::Debug + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MacroCommand")
.field("sub_commands", &self.sub_commands)
.finish()
}
}