@@ -31,6 +31,7 @@ pub fn function_enum(
3131
3232 let enum_name = & item. ident ;
3333 let mut as_str_arms = Vec :: new ( ) ;
34+ let mut from_str_arms = Vec :: new ( ) ;
3435 let mut base_arms = Vec :: new ( ) ;
3536
3637 for func in ALL_OPERATIONS . iter ( ) {
@@ -40,6 +41,7 @@ pub fn function_enum(
4041
4142 // Match arm for `fn as_str(self)` matcher
4243 as_str_arms. push ( quote ! { Self :: #ident => #fn_name } ) ;
44+ from_str_arms. push ( quote ! { #fn_name => Self :: #ident } ) ;
4345
4446 // Match arm for `fn base_name(self)` matcher
4547 base_arms. push ( quote ! { Self :: #ident => #base_enum:: #bname_ident } ) ;
@@ -50,24 +52,45 @@ pub fn function_enum(
5052 item. variants . push ( variant) ;
5153 }
5254
55+ let variants = item. variants . iter ( ) ;
56+
5357 let res = quote ! {
5458 // Instantiate the enum
5559 #item
5660
5761 impl #enum_name {
62+ /// All variants of this enum.
63+ pub const ALL : & [ Self ] = & [
64+ #( Self :: #variants, ) *
65+ ] ;
66+
5867 /// The stringified version of this function name.
5968 pub const fn as_str( self ) -> & ' static str {
6069 match self {
6170 #( #as_str_arms , ) *
6271 }
6372 }
6473
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+
6583 /// The base name enum for this function.
6684 pub const fn base_name( self ) -> #base_enum {
6785 match self {
6886 #( #base_arms, ) *
6987 }
7088 }
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+ }
7194 }
7295 } ;
7396
0 commit comments