Skip to content

Commit

Permalink
Refactor into try_test + syn::Result
Browse files Browse the repository at this point in the history
syn::Result allows us to return spanned errors which can be converted
into nice compile errors when there is unexpected input.

For example:
```
error: Failed to parse value, expected a string!
  --> tests/mod.rs:94:33
   |
94 | #[test_log(default_log_filter = 6)]
   |                                 ^
```
  • Loading branch information
DarrenTsung authored and d-e-s-o committed Nov 4, 2023
1 parent b5d07af commit e1d5d67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
----------
- Improved compile error output on wrong usage


0.2.13
------
- Improved interaction with nested attributes (such as those used by the
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ use syn::ItemFn;
/// ```
#[proc_macro_attribute]
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as ItemFn);
try_test(attr, item)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}


fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
let inner_test = if attr.is_empty() {
quote! { ::core::prelude::v1::test }
} else {
attr.into()
};
let input = parse_macro_input!(item as ItemFn);

let ItemFn {
attrs,
Expand Down Expand Up @@ -115,7 +122,7 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
#block
}
};
result.into()
Ok(result)
}


Expand Down

0 comments on commit e1d5d67

Please sign in to comment.