google / rust-async-coap

A flexible, asynchronous library for using and serving CoAP resources in Rust.
Apache License 2.0
51 stars 17 forks source link

async-caop-uri-macros: rewrote and updated them #32

Open Luro02 opened 4 years ago

Luro02 commented 4 years ago

I updated the proc-macros to use the latest versions of syn and quote and improved the error messages by using syn::Error and Spans.

For example this error message is now returned

error: ascii control chars are forbidden for security reasons
 --> $DIR/escaped_ascii_control.rs:7:29
  |
7 |     assert_rel_ref_literal!("https://www.example.com/a/%00/c");
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

which previously was

error: proc-macro derive panicked
 --> src/main.rs:4:5
  |
4 |     uri!("https://www.example.com/a/%00/c");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: Malformed percent encoding at index 29
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

In the future it would be possible to further improve the error messages to

error: ascii control chars are forbidden for security reasons
 --> $DIR/escaped_ascii_control.rs:7:29
  |
7 |     assert_rel_ref_literal!("https://www.example.com/a/%00/c");
  |                                                        ^^^
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

but this requires proc_macro::Literal::subspan to be stable.


I had to add proc-macro2 as a dependency, because syn only returns proc-macro2 types and the conversion between proc-macro and proc-macro2 is not always possible.


For the new error messages I added trybuild, which allows to test the stderr returned by proc-macros. I set up some basic tests for some of them, but not all of them.


I also improved the verification of the uri encoding, by removing a lot of redundant code and simplifying the remaining code, which should have improved the parsing speed :)

darconeous commented 3 years ago

Sorry for the delay in reviewing this!

Whoah! This is a big pull request. I'll try to get it reviewed this weekend. The proc macros stuff definitely needed some love.