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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
use crate::{Groupped, ProgramCollect};
use crate::error::ChainProcessError;
#[doc(hidden)]
pub mod group;
/// Any type output
///
/// Accepts any type that implements `Send + Groupped<G>`
/// After being passed into `AnyOutput`, it will be converted to `Box<dyn Any + Send + 'static>`
///
/// Note:
/// - If an enum value that does not belong to this type is incorrectly specified, it will be **unsafely** unwrapped by the scheduler
/// - Structured output via `--json`/`--yaml` is only available for types that implement
/// [`StructuralData`], which implies `serde::Serialize`.
/// - It is recommended to use the `pack!` macro from [mingling_macros](https://crates.io/crates/mingling_macros) to create types that can be converted to `AnyOutput`, which guarantees runtime safety
#[derive(Debug)]
pub struct AnyOutput<G> {
pub(crate) inner: Box<dyn std::any::Any + Send + 'static>,
pub type_id: std::any::TypeId,
pub member_id: G,
}
impl<G> AnyOutput<G> {
/// Create an `AnyOutput` from a `Send + Groupped<G>` type
pub fn new<T>(value: T) -> Self
where
T: Send + Groupped<G> + 'static,
{
Self {
inner: Box::new(value),
type_id: std::any::TypeId::of::<T>(),
member_id: T::member_id(),
}
}
/// Attempt to downcast the `AnyOutput` to a concrete type.
///
/// # Errors
///
/// Returns `Err(self)` if the downcast fails.
///
/// # Panics
///
/// Panics if the inner value is not of type `T`.
pub fn downcast<T: 'static>(self) -> Result<T, Self> {
if self.type_id == std::any::TypeId::of::<T>() {
Ok(*self.inner.downcast::<T>().unwrap())
} else {
Err(self)
}
}
/// Check if the inner value is of type T
pub fn is<T: 'static>(&self) -> bool {
self.type_id == std::any::TypeId::of::<T>()
}
/// Route the output to the next Chain
pub fn route_chain(self) -> ChainProcess<G> {
ChainProcess::Ok((self, NextProcess::Chain))
}
/// Route the output to the Renderer, ending execution
pub fn route_renderer(self) -> ChainProcess<G> {
ChainProcess::Ok((self, NextProcess::Renderer))
}
/// Restore `AnyOutput` back to the original concrete type.
///
/// # Safety
///
/// This is only safe when `T` matches the `TypeId` stored in the `AnyOutput`.
/// Generated code (via `gen_program!()`) guarantees this by dispatching on
/// `member_id` before calling `restore`.
pub fn restore<T: 'static>(self) -> Option<T> {
if self.type_id == std::any::TypeId::of::<T>() {
match self.inner.downcast::<T>() {
Ok(boxed) => Some(*boxed),
Err(_) => None,
}
} else {
None
}
}
}
impl<G> std::ops::Deref for AnyOutput<G> {
type Target = dyn std::any::Any + Send + 'static;
fn deref(&self) -> &Self::Target {
&*self.inner
}
}
impl<G> std::ops::DerefMut for AnyOutput<G> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.inner
}
}
/// Chain exec result type
///
/// Stores `Ok` and `Err` types of execution results, used to notify the scheduler what to execute next
/// - Returns `Ok((`[`AnyOutput`](./struct.AnyOutput.html)`, `[`NextProcess::Chain`](./enum.NextProcess.html)`))` to continue execution with this type next
/// - Returns `Ok((`[`AnyOutput`](./struct.AnyOutput.html)`, `[`NextProcess::Renderer`](./enum.NextProcess.html)`))` to render this type next and output to the terminal
/// - Returns `Err(`[`ChainProcessError`](./error/enum.ChainProcessError.html)`]` to terminate the program directly
pub enum ChainProcess<G> {
Ok((AnyOutput<G>, NextProcess)),
Err(ChainProcessError),
}
/// Indicates the next step after processing
///
/// - `Chain`: Continue execution to the next chain
/// - `Renderer`: Send output to renderer and end execution
#[derive(Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum NextProcess {
Chain,
Renderer,
}
impl std::fmt::Display for NextProcess {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NextProcess::Chain => write!(f, "Chain"),
NextProcess::Renderer => write!(f, "Renderer"),
}
}
}
impl<G> From<AnyOutput<G>> for ChainProcess<G> {
fn from(value: AnyOutput<G>) -> Self {
ChainProcess::Ok((value, NextProcess::Chain))
}
}
impl<G> From<()> for ChainProcess<G> where G: ProgramCollect<Enum = G> {
fn from(_v: ()) -> Self {
G::build_empty_result().route_chain()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Groupped;
/// Mock enum for testing AnyOutput
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
enum MockGroup {
Alpha,
Beta,
Gamma,
}
impl std::fmt::Display for MockGroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MockGroup::Alpha => write!(f, "Alpha"),
MockGroup::Beta => write!(f, "Beta"),
MockGroup::Gamma => write!(f, "Gamma"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "structural_renderer", derive(serde::Serialize))]
struct AlphaData {
value: i32,
}
impl Groupped<MockGroup> for AlphaData {
fn member_id() -> MockGroup {
MockGroup::Alpha
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "structural_renderer", derive(serde::Serialize))]
struct BetaData {
name: String,
}
impl Groupped<MockGroup> for BetaData {
fn member_id() -> MockGroup {
MockGroup::Beta
}
}
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
#[cfg_attr(feature = "structural_renderer", derive(serde::Serialize))]
struct GammaData;
impl Groupped<MockGroup> for GammaData {
fn member_id() -> MockGroup {
MockGroup::Gamma
}
}
// AnyOutput::new
#[test]
fn test_any_output_new_stores_type_id_and_member_id() {
let data = AlphaData { value: 42 };
let output = AnyOutput::new(data);
assert_eq!(output.type_id, std::any::TypeId::of::<AlphaData>());
assert_eq!(output.member_id, MockGroup::Alpha);
}
// AnyOutput::downcast
#[test]
fn test_any_output_downcast_success() {
let data = AlphaData { value: 99 };
let output = AnyOutput::new(data);
let result: Result<AlphaData, _> = output.downcast::<AlphaData>();
assert!(result.is_ok());
assert_eq!(result.unwrap().value, 99);
}
#[test]
fn test_any_output_downcast_failure() {
let data = AlphaData { value: 10 };
let output = AnyOutput::new(data);
let result: Result<BetaData, _> = output.downcast::<BetaData>();
assert!(result.is_err());
}
// AnyOutput::is
#[test]
fn test_any_output_is_true_for_matching_type() {
let data = AlphaData { value: 7 };
let output = AnyOutput::new(data);
assert!(output.is::<AlphaData>());
}
#[test]
fn test_any_output_is_false_for_non_matching_type() {
let data = AlphaData { value: 7 };
let output = AnyOutput::new(data);
assert!(!output.is::<BetaData>());
}
// AnyOutput::route_chain
#[test]
fn test_route_chain_returns_ok_with_chain_next() {
let data = AlphaData { value: 1 };
let output = AnyOutput::new(data);
let result = output.route_chain();
match result {
ChainProcess::Ok((any, next)) => {
assert_eq!(any.member_id, MockGroup::Alpha);
assert_eq!(next, NextProcess::Chain);
}
_ => panic!("Expected ChainProcess::Ok"),
}
}
// AnyOutput::route_renderer
#[test]
fn test_route_renderer_returns_ok_with_renderer_next() {
let data = AlphaData { value: 2 };
let output = AnyOutput::new(data);
let result = output.route_renderer();
match result {
ChainProcess::Ok((any, next)) => {
assert_eq!(any.member_id, MockGroup::Alpha);
assert_eq!(next, NextProcess::Renderer);
}
_ => panic!("Expected ChainProcess::Ok"),
}
}
// AnyOutput: Deref / DerefMut
#[test]
fn test_any_output_deref_accesses_inner_any() {
let data = AlphaData { value: 5 };
let output = AnyOutput::new(data);
let inner: &dyn std::any::Any = &*output;
assert!(inner.downcast_ref::<AlphaData>().is_some());
}
#[test]
fn test_any_output_deref_mut_allows_modification() {
let data = AlphaData { value: 0 };
let mut output = AnyOutput::new(data);
let inner: &mut dyn std::any::Any = &mut *output;
if let Some(ref mut v) = inner.downcast_mut::<AlphaData>() {
v.value = 100;
}
let result: Result<AlphaData, _> = output.downcast::<AlphaData>();
assert_eq!(result.unwrap().value, 100);
}
// ChainProcess::From<AnyOutput>
#[test]
fn test_chain_process_from_any_output() {
let data = AlphaData { value: 3 };
let output = AnyOutput::new(data);
let cp: ChainProcess<MockGroup> = output.into();
match cp {
ChainProcess::Ok((any, next)) => {
assert_eq!(any.member_id, MockGroup::Alpha);
assert_eq!(next, NextProcess::Chain);
}
_ => panic!("Expected ChainProcess::Ok"),
}
}
// NextProcess::Display
#[test]
fn test_next_process_display_chain() {
assert_eq!(format!("{}", NextProcess::Chain), "Chain");
}
#[test]
fn test_next_process_display_renderer() {
assert_eq!(format!("{}", NextProcess::Renderer), "Renderer");
}
// AnyOutput::restore structural_renderer feature only
#[cfg(feature = "structural_renderer")]
#[test]
fn test_any_output_restore_success() {
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
struct SerData {
x: i32,
}
impl Groupped<MockGroup> for SerData {
fn member_id() -> MockGroup {
MockGroup::Gamma
}
}
let data = SerData { x: 42 };
let output = AnyOutput::new(data);
let restored: Option<SerData> = output.restore::<SerData>();
assert_eq!(restored, Some(SerData { x: 42 }));
}
#[cfg(feature = "structural_renderer")]
#[test]
fn test_any_output_restore_type_mismatch() {
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
struct SerA {
a: i32,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
struct SerB {
b: String,
}
impl Groupped<MockGroup> for SerA {
fn member_id() -> MockGroup {
MockGroup::Alpha
}
}
impl Groupped<MockGroup> for SerB {
fn member_id() -> MockGroup {
MockGroup::Beta
}
}
let data = SerA { a: 1 };
let output = AnyOutput::new(data);
let restored: Option<SerB> = output.restore::<SerB>();
assert_eq!(restored, None);
}
}
|