union_impl/expr_chain/group/
command_group.rs1use syn::Expr;
7
8use super::super::expr::{DefaultExpr, ProcessExpr};
9
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
11pub enum CommandGroup {
12 Map,
14 Dot,
16 Inspect,
18 Then,
20 AndThen,
22 Or,
24 OrElse,
26 MapErr,
28 Initial,
30}
31
32impl CommandGroup {
33 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 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}