union_impl/expr_chain/group/
command_group.rs

1//!
2//! `CommandGroup` is an enum of all possible `ProcessExpr` and `DefaultExpr` operations.
3//! Used to express group which was found in input `ParseStream`
4//!
5
6use syn::Expr;
7
8use super::super::expr::{DefaultExpr, ProcessExpr};
9
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11pub enum CommandGroup {
12    /// [ProcessExpr::Map]
13    Map,
14    /// [ProcessExpr::Dot]
15    Dot,
16    /// [ProcessExpr::Inspect]
17    Inspect,
18    /// [ProcessExpr::Then]
19    Then,
20    /// [ProcessExpr::AndThen]
21    AndThen,
22    /// [DefaultExpr::Or]
23    Or,
24    /// [DefaultExpr::OrElse]
25    OrElse,
26    /// [ProcessExpr::MapErr]
27    MapErr,
28    /// [ProcessExpr::Initial]
29    Initial,
30}
31
32impl CommandGroup {
33    ///
34    /// Attempts to map expr to `ProcessExpr` with self type.
35    /// Returns None if self isn't of `ProcessExpr` type
36    ///
37    pub fn map_to_process_expr(self, expr: Expr) -> Option<ProcessExpr> {
38        match self {
39            CommandGroup::Map => Some(ProcessExpr::Map(expr)),
40            CommandGroup::AndThen => Some(ProcessExpr::AndThen(expr)),
41            CommandGroup::Dot => Some(ProcessExpr::Dot(expr)),
42            CommandGroup::Then => Some(ProcessExpr::Then(expr)),
43            CommandGroup::MapErr => Some(ProcessExpr::MapErr(expr)),
44            CommandGroup::Initial => Some(ProcessExpr::Initial(expr)),
45            CommandGroup::Inspect => Some(ProcessExpr::Inspect(expr)),
46            _ => None,
47        }
48    }
49
50    ///
51    /// Attempts to map expr to `DefaultExpr` with self type.
52    /// Returns None if self isn't of `DefaultExpr` type
53    ///
54    pub fn map_to_default_expr(self, expr: Expr) -> Option<DefaultExpr> {
55        match self {
56            CommandGroup::Or => Some(DefaultExpr::Or(expr)),
57            CommandGroup::OrElse => Some(DefaultExpr::OrElse(expr)),
58            _ => None,
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn it_tests_command_group_impl_process_expr() {
69        let expr: ::syn::Expr = ::syn::parse2(::quote::quote! { |v| v + 1 }).unwrap();
70
71        let groups_vec: Vec<(CommandGroup, Box<dyn Fn(Expr) -> ProcessExpr>)> = vec![
72            (CommandGroup::Map, Box::new(ProcessExpr::Map)),
73            (CommandGroup::Dot, Box::new(ProcessExpr::Dot)),
74            (CommandGroup::Inspect, Box::new(ProcessExpr::Inspect)),
75            (CommandGroup::Then, Box::new(ProcessExpr::Then)),
76            (CommandGroup::AndThen, Box::new(ProcessExpr::AndThen)),
77            (CommandGroup::MapErr, Box::new(ProcessExpr::MapErr)),
78            (CommandGroup::Initial, Box::new(ProcessExpr::Initial)),
79        ];
80
81        for (command_group, process_expr) in groups_vec.into_iter() {
82            assert_eq!(
83                command_group.map_to_process_expr(expr.clone()).unwrap(),
84                process_expr(expr.clone())
85            );
86            assert_eq!(command_group.map_to_default_expr(expr.clone()), None);
87        }
88    }
89
90    #[test]
91    fn it_tests_command_group_impl_default_expr() {
92        let expr: ::syn::Expr = ::syn::parse2(::quote::quote! { |v| v + 1 }).unwrap();
93
94        let groups_vec: Vec<(CommandGroup, Box<dyn Fn(Expr) -> DefaultExpr>)> = vec![
95            (CommandGroup::Or, Box::new(DefaultExpr::Or)),
96            (CommandGroup::OrElse, Box::new(DefaultExpr::OrElse)),
97        ];
98
99        for (command_group, process_expr) in groups_vec.into_iter() {
100            assert_eq!(
101                command_group.map_to_default_expr(expr.clone()).unwrap(),
102                process_expr(expr.clone())
103            );
104            assert_eq!(command_group.map_to_process_expr(expr.clone()), None);
105        }
106    }
107}