summaryrefslogtreecommitdiff
path: root/mingling_core/src/asset/dispatcher.rs
blob: 86dfe7c32c454c1847bfafb6d2cf004418af299f (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::fmt::Display;

use crate::{ChainProcess, Program, asset::node::Node};

/// Dispatches user input commands to specific [ChainProcess](./enum.ChainProcess.html)
///
/// Note: If you are using [mingling_macros](https://crates.io/crates/mingling_macros),
/// you can use the `dispatcher!("node.subnode", CommandType => Entry)` macro to declare a `Dispatcher`
pub trait Dispatcher<G>
where
    G: Display,
{
    /// Returns a command node for matching user input
    fn node(&self) -> Node;

    /// Returns a [ChainProcess](./enum.ChainProcess.html) based on user input arguments,
    /// to be sent to the specific invocation
    fn begin(&self, args: Vec<String>) -> ChainProcess<G>;

    /// Clones the current dispatcher for implementing the `Clone` trait
    fn clone_dispatcher(&self) -> Box<dyn Dispatcher<G>>;
}

impl<G> Clone for Box<dyn Dispatcher<G>>
where
    G: Display,
{
    fn clone(&self) -> Self {
        self.clone_dispatcher()
    }
}

impl<C: crate::program::ProgramCollect, G: Display> Program<C, G> {
    /// Adds a dispatcher to the program.
    pub fn with_dispatcher<Disp>(&mut self, dispatcher: Disp)
    where
        Disp: Dispatcher<G> + 'static,
    {
        self.dispatcher.push(Box::new(dispatcher));
    }

    /// Add some dispatchers to the program.
    pub fn with_dispatchers<D>(&mut self, dispatchers: D)
    where
        D: Into<Dispatchers<G>>,
    {
        let dispatchers = dispatchers.into();
        self.dispatcher.extend(dispatchers.dispatcher);
    }
}

/// A collection of dispatchers.
///
/// This struct holds a vector of boxed `Dispatcher` trait objects,
/// allowing multiple dispatchers to be grouped together and passed
/// to the program via `Program::with_dispatchers`.
/// A collection of dispatchers.
///
/// This struct holds a vector of boxed `Dispatcher` trait objects,
/// allowing multiple dispatchers to be grouped together and passed
/// to the program via `Program::with_dispatchers`.
pub struct Dispatchers<G> {
    dispatcher: Vec<Box<dyn Dispatcher<G> + 'static>>,
}

impl<G> From<Vec<Box<dyn Dispatcher<G>>>> for Dispatchers<G> {
    fn from(dispatcher: Vec<Box<dyn Dispatcher<G>>>) -> Self {
        Self { dispatcher }
    }
}

impl<G> From<Box<dyn Dispatcher<G>>> for Dispatchers<G> {
    fn from(dispatcher: Box<dyn Dispatcher<G>>) -> Self {
        Self {
            dispatcher: vec![dispatcher],
        }
    }
}

impl<D, G> From<(D,)> for Dispatchers<G>
where
    D: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatcher: (D,)) -> Self {
        Self {
            dispatcher: vec![Box::new(dispatcher.0)],
        }
    }
}

impl<D1, D2, G> From<(D1, D2)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2)) -> Self {
        Self {
            dispatcher: vec![Box::new(dispatchers.0), Box::new(dispatchers.1)],
        }
    }
}

impl<D1, D2, D3, G> From<(D1, D2, D3)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    D3: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2, D3)) -> Self {
        Self {
            dispatcher: vec![
                Box::new(dispatchers.0),
                Box::new(dispatchers.1),
                Box::new(dispatchers.2),
            ],
        }
    }
}

impl<D1, D2, D3, D4, G> From<(D1, D2, D3, D4)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    D3: Dispatcher<G> + 'static,
    D4: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2, D3, D4)) -> Self {
        Self {
            dispatcher: vec![
                Box::new(dispatchers.0),
                Box::new(dispatchers.1),
                Box::new(dispatchers.2),
                Box::new(dispatchers.3),
            ],
        }
    }
}

impl<D1, D2, D3, D4, D5, G> From<(D1, D2, D3, D4, D5)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    D3: Dispatcher<G> + 'static,
    D4: Dispatcher<G> + 'static,
    D5: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2, D3, D4, D5)) -> Self {
        Self {
            dispatcher: vec![
                Box::new(dispatchers.0),
                Box::new(dispatchers.1),
                Box::new(dispatchers.2),
                Box::new(dispatchers.3),
                Box::new(dispatchers.4),
            ],
        }
    }
}

impl<D1, D2, D3, D4, D5, D6, G> From<(D1, D2, D3, D4, D5, D6)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    D3: Dispatcher<G> + 'static,
    D4: Dispatcher<G> + 'static,
    D5: Dispatcher<G> + 'static,
    D6: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2, D3, D4, D5, D6)) -> Self {
        Self {
            dispatcher: vec![
                Box::new(dispatchers.0),
                Box::new(dispatchers.1),
                Box::new(dispatchers.2),
                Box::new(dispatchers.3),
                Box::new(dispatchers.4),
                Box::new(dispatchers.5),
            ],
        }
    }
}

impl<D1, D2, D3, D4, D5, D6, D7, G> From<(D1, D2, D3, D4, D5, D6, D7)> for Dispatchers<G>
where
    D1: Dispatcher<G> + 'static,
    D2: Dispatcher<G> + 'static,
    D3: Dispatcher<G> + 'static,
    D4: Dispatcher<G> + 'static,
    D5: Dispatcher<G> + 'static,
    D6: Dispatcher<G> + 'static,
    D7: Dispatcher<G> + 'static,
    G: Display,
{
    fn from(dispatchers: (D1, D2, D3, D4, D5, D6, D7)) -> Self {
        Self {
            dispatcher: vec![
                Box::new(dispatchers.0),
                Box::new(dispatchers.1),
                Box::new(dispatchers.2),
                Box::new(dispatchers.3),
                Box::new(dispatchers.4),
                Box::new(dispatchers.5),
                Box::new(dispatchers.6),
            ],
        }
    }
}

impl<G> std::ops::Deref for Dispatchers<G> {
    type Target = Vec<Box<dyn Dispatcher<G> + 'static>>;

    fn deref(&self) -> &Self::Target {
        &self.dispatcher
    }
}

impl<G> From<Dispatchers<G>> for Vec<Box<dyn Dispatcher<G> + 'static>> {
    fn from(val: Dispatchers<G>) -> Self {
        val.dispatcher
    }
}