@@ -5,7 +5,7 @@ use quote::quote;
5
5
use syn:: spanned:: Spanned ;
6
6
use syn:: { Fields , ItemEnum , Variant } ;
7
7
8
- use crate :: { ALL_FUNCTIONS_FLAT , base_name} ;
8
+ use crate :: { ALL_OPERATIONS , base_name} ;
9
9
10
10
/// Implement `#[function_enum]`, see documentation in `lib.rs`.
11
11
pub fn function_enum (
@@ -31,15 +31,17 @@ 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
- for func in ALL_FUNCTIONS_FLAT . iter ( ) {
37
+ for func in ALL_OPERATIONS . iter ( ) {
37
38
let fn_name = func. name ;
38
39
let ident = Ident :: new ( & fn_name. to_upper_camel_case ( ) , Span :: call_site ( ) ) ;
39
40
let bname_ident = Ident :: new ( & base_name ( fn_name) . to_upper_camel_case ( ) , Span :: call_site ( ) ) ;
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
@@ -85,8 +108,7 @@ pub fn base_name_enum(
85
108
return Err ( syn:: Error :: new ( sp. span ( ) , "no attributes expected" ) ) ;
86
109
}
87
110
88
- let mut base_names: Vec < _ > =
89
- ALL_FUNCTIONS_FLAT . iter ( ) . map ( |func| base_name ( func. name ) ) . collect ( ) ;
111
+ let mut base_names: Vec < _ > = ALL_OPERATIONS . iter ( ) . map ( |func| base_name ( func. name ) ) . collect ( ) ;
90
112
base_names. sort_unstable ( ) ;
91
113
base_names. dedup ( ) ;
92
114
0 commit comments