@@ -31,6 +31,7 @@ pub fn function_enum(
31
31
32
32
let enum_name = & item. ident ;
33
33
let mut as_str_arms = Vec :: new ( ) ;
34
+ let mut from_str_arms = Vec :: new ( ) ;
34
35
let mut base_arms = Vec :: new ( ) ;
35
36
36
37
for func in ALL_OPERATIONS . iter ( ) {
@@ -40,6 +41,7 @@ pub fn function_enum(
40
41
41
42
// Match arm for `fn as_str(self)` matcher
42
43
as_str_arms. push ( quote ! { Self :: #ident => #fn_name } ) ;
44
+ from_str_arms. push ( quote ! { #fn_name => Self :: #ident } ) ;
43
45
44
46
// Match arm for `fn base_name(self)` matcher
45
47
base_arms. push ( quote ! { Self :: #ident => #base_enum:: #bname_ident } ) ;
@@ -50,24 +52,45 @@ pub fn function_enum(
50
52
item. variants . push ( variant) ;
51
53
}
52
54
55
+ let variants = item. variants . iter ( ) ;
56
+
53
57
let res = quote ! {
54
58
// Instantiate the enum
55
59
#item
56
60
57
61
impl #enum_name {
62
+ /// All variants of this enum.
63
+ pub const ALL : & [ Self ] = & [
64
+ #( Self :: #variants, ) *
65
+ ] ;
66
+
58
67
/// The stringified version of this function name.
59
68
pub const fn as_str( self ) -> & ' static str {
60
69
match self {
61
70
#( #as_str_arms , ) *
62
71
}
63
72
}
64
73
74
+ /// If `s` is the name of a function, return it.
75
+ pub fn from_str( s: & str ) -> Option <Self > {
76
+ let ret = match s {
77
+ #( #from_str_arms , ) *
78
+ _ => return None ,
79
+ } ;
80
+ Some ( ret)
81
+ }
82
+
65
83
/// The base name enum for this function.
66
84
pub const fn base_name( self ) -> #base_enum {
67
85
match self {
68
86
#( #base_arms, ) *
69
87
}
70
88
}
89
+
90
+ /// Return information about this operation.
91
+ pub fn math_op( self ) -> & ' static crate :: op:: MathOpInfo {
92
+ crate :: op:: ALL_OPERATIONS . iter( ) . find( |op| op. name == self . as_str( ) ) . unwrap( )
93
+ }
71
94
}
72
95
} ;
73
96
0 commit comments