@@ -3,6 +3,7 @@ use proc_macro_error2::abort;
33use syn:: { Attribute , ItemFn , ReturnType , Type } ;
44
55pub ( crate ) struct InitFunc {
6+ pub name : String ,
67 pub func : ItemFn ,
78 pub state : Option < Type > ,
89 pub asyncness : bool ,
@@ -14,15 +15,15 @@ impl From<FunctionWithAttributes> for InitFunc {
1415 for ( attr, span) in attributes {
1516 match attr {
1617 FuncAttribute :: Init => { }
17- FuncAttribute :: Test => unreachable ! ( ) ,
18+ FuncAttribute :: Test ( _ ) => unreachable ! ( ) ,
1819 _ => abort ! ( span, "The `#[init]` function can not have this attribute" ) ,
1920 }
2021 }
2122 if check_fn_sig ( & func. sig ) . is_err ( ) || !func. sig . inputs . is_empty ( ) {
2223 abort ! (
23- func. sig,
24- "`#[init]` function must have signature `async fn() [-> Type]` (async/return type are optional)" ,
25- ) ;
24+ func. sig,
25+ "`#[init]` function must have signature `async fn() [-> Type]` (async/return type are optional)" ,
26+ ) ;
2627 }
2728
2829 if cfg ! ( not( feature = "embassy" ) ) && func. sig . asyncness . is_some ( ) {
@@ -37,6 +38,7 @@ impl From<FunctionWithAttributes> for InitFunc {
3738 ReturnType :: Type ( .., ty) => Some ( * ty. clone ( ) ) ,
3839 } ;
3940 InitFunc {
41+ name : func. sig . ident . to_string ( ) ,
4042 asyncness : func. sig . asyncness . is_some ( ) ,
4143 func,
4244 state,
@@ -52,6 +54,7 @@ pub(crate) struct TestFunc {
5254 pub ignore : bool ,
5355 pub asyncness : bool ,
5456 pub timeout : Option < u32 > ,
57+ pub custom_init : Option < syn:: Ident > ,
5558}
5659
5760impl From < FunctionWithAttributes > for TestFunc {
@@ -60,10 +63,11 @@ impl From<FunctionWithAttributes> for TestFunc {
6063 let mut should_panic = false ;
6164 let mut ignore = false ;
6265 let mut timeout = None ;
66+ let mut custom_init = None ;
6367 for ( attr, _span) in attributes {
6468 match attr {
6569 FuncAttribute :: Init => unreachable ! ( ) ,
66- FuncAttribute :: Test => { }
70+ FuncAttribute :: Test ( attr ) => custom_init = attr . init ,
6771 FuncAttribute :: ShouldPanic => should_panic = true ,
6872 FuncAttribute :: Ignore => ignore = true ,
6973 FuncAttribute :: Timeout ( t) => timeout = Some ( t. value ) ,
@@ -99,17 +103,31 @@ impl From<FunctionWithAttributes> for TestFunc {
99103 should_panic,
100104 ignore,
101105 timeout,
106+ custom_init,
107+ }
108+ }
109+ }
110+
111+ pub ( crate ) struct OtherFunc ( pub FunctionWithAttributes ) ;
112+ impl From < FunctionWithAttributes > for OtherFunc {
113+ fn from ( func : FunctionWithAttributes ) -> Self {
114+ if let Some ( ( _attr, span) ) = func. attributes . first ( ) {
115+ abort ! (
116+ span,
117+ "Only `#[test]` or `#[init]` functions can have such an attribute"
118+ ) ;
102119 }
120+ OtherFunc ( func)
103121 }
104122}
105123
106- pub ( crate ) enum Func {
124+ pub ( crate ) enum AnnotatedFunction {
107125 Init ( InitFunc ) ,
108126 Test ( TestFunc ) ,
109- Other ( FunctionWithAttributes ) ,
127+ Other ( OtherFunc ) ,
110128}
111129
112- impl From < FunctionWithAttributes > for Func {
130+ impl From < FunctionWithAttributes > for AnnotatedFunction {
113131 fn from ( func : FunctionWithAttributes ) -> Self {
114132 enum FuncKind {
115133 Init ,
@@ -119,8 +137,8 @@ impl From<FunctionWithAttributes> for Func {
119137 for ( attr, span) in & func. attributes {
120138 match attr {
121139 FuncAttribute :: Init if func_kind. is_none ( ) => func_kind = Some ( FuncKind :: Init ) ,
122- FuncAttribute :: Test if func_kind. is_none ( ) => func_kind = Some ( FuncKind :: Test ) ,
123- FuncAttribute :: Init | FuncAttribute :: Test => {
140+ FuncAttribute :: Test ( _ ) if func_kind. is_none ( ) => func_kind = Some ( FuncKind :: Test ) ,
141+ FuncAttribute :: Init | FuncAttribute :: Test ( _ ) => {
124142 abort ! (
125143 span,
126144 "A function can only be marked with one of `#[init]` or `#[test]`"
@@ -131,9 +149,9 @@ impl From<FunctionWithAttributes> for Func {
131149 }
132150
133151 match func_kind {
134- Some ( FuncKind :: Init ) => Func :: Init ( InitFunc :: from ( func) ) ,
135- Some ( FuncKind :: Test ) => Func :: Test ( TestFunc :: from ( func) ) ,
136- None => Func :: Other ( func) ,
152+ Some ( FuncKind :: Init ) => AnnotatedFunction :: Init ( InitFunc :: from ( func) ) ,
153+ Some ( FuncKind :: Test ) => AnnotatedFunction :: Test ( TestFunc :: from ( func) ) ,
154+ None => AnnotatedFunction :: Other ( OtherFunc :: from ( func) ) ,
137155 }
138156 }
139157}
0 commit comments