Closed rajivr closed 5 years ago
The output tokens are likely invalid syntax.
Thanks David for the pointer. :-) It looks like I had misunderstood where to insert the to_compile_error()
token stream.
Is there a better way to bubble up errors from nested closures rather than doing something like below?
Right now I am first checking to make sure there are no errors from field_attr_builder_each()
and then in the subsequent use of fields.iter()
, use unwrap()
.
Ideally I would have wanted a way to get a Result<some iterator, syn::Error>
from fields.iter()
, but was unable to make that happen.
// Deal with potential error in `field_attr_builder_each()`
if let Err(e) = fields.iter().try_for_each(|f| {
if field_attr_builder_each(f).is_some() {
field_attr_builder_each(f)
.unwrap()
.map(|_| Ok(()))
.unwrap_or_else(|err| Err(err))
} else {
Ok(())
}
}) {
return e.to_compile_error().into();
}
The main way to go from Iterator of Result to Result of Iterator is with collect:
let out = data.iter()
.map(|elem| {
/* Ok or Err */
})
.collect::<Result<Vec<_>>()?;
println!("{:?}", out); // Vec<_>
Thanks @dtolnay.
When I try to do
cargo expand
tests/08-unrecognized-attribute.rs
(copied asmain.rs
), instead of getting the error message (expected builder(each = "...")
) created bysyn::Error::new_spanned(...)
infield_attr_builder_each
, for some reason the error messageexpected one of ...
is being generated.I can't seem to understand where this error message is originating from, and why the token stream from
err.to_compile_error()
is not being displayed instead.I would greatly appreciate any pointers on how I can solve this! :-)