A derive macro for syn
's Parse
trait
This is a relatively simple derive macro that produces an implementation syn::parse::Parse
for the
type it's applied to.
A common pattern when writing custom syn
parsers is repeating <name>: input.parse()?
for
each field in the output. #[derive(Parse)]
handles that for you, with some extra helpful
customization.
Usage
Using this crate is as simple as adding it to your 'Cargo.toml' and importing the derive macro:
# Cargo.toml
[]
= "0.1"
// your_file.rs
use Parse;
The derived implementation of Parse
always parses in the order that the fields are given.
This crate is intended for users who are already making heavy use of syn
.
Motivation
When writing rust code that makes heavy use of syn
's parsing functionality, we often end up
writing things like:
use ;
use ;
// A simplified struct field
//
// x: i32
This is really repetetive! Ideally, we'd like to just #[derive(Parse)]
and have it work. And
so we can! (for the most part) Adding #[derive(Parse)]
to the previous struct produces an
equivalent implementation of Parse
:
use ;
use Parse;
Of course, there are more complicated cases. This is mainly covered immediately below in the 'Advanced usage' section.
Advanced usage
There's a moderate collection of helper attributes that can be applied to fields to customize the
generated implementation of Parse
. Each of these are demonstrated with the implementation that
they produce. Please note that the produced implementation is typically not identical to what's
shown here.
All of the examples are fairly contrived, I know. The reality of the matter is that - if you would find this useful - it's probably true that your use-case is much more complicated than would make sense for a short example. (If it isn't, let me know! It would be great to include it here!)
List of helper attributes
#[paren]
/ #[bracket]
/ #[brace]
Because the derive macro has no fool-proof method for determining by itself whether a field type
is any of syn::token::{Paren, Bracket, Brace}
, these three serve to provide that information
instead.
These are typically used in conjunction with #[inside]
.
// A single-argument function call
//
// so_long(and_thanks + for_all * the_fish)
produces
#[inside(..)]
This is a companion to #[paren]
/#[bracket]
/#[brace]
- given a field name to use, this
attribute indicates that the field should be parsed using a previous field as the source.
use ;
use Bracket;
// An array type required to have a length
//
// [i32; 4]
produces
#[call(..)]
Given a path to a function, this attribute specifies that the value of the field should be
instead calculated by a call to input.parse(..)
with a given path. The best example is taken
straight from the syn
documentation itself:
use ;
// Parses a unit struct with attributes.
//
// #[path = "s.tmpl"]
// struct S;
produces
#[parse_terminated(..)]
Just as we have #[call(..)]
for ParseStream::call
, we have #[parse_terminated]
for ParseStream::parse_terminated
. Here's the same example that the ParseStream
method
uses:
// Parse a simplified tuple struct syntax like:
//
// struct S(A, B);
produces
Known limitations
The derive macro is only available for structs. While actually possible, it's currently
considered outside of the scope of this crate to generate implementations of Parse
for enums.
This is because they will always require some kind of lookahead (either via
ParseStream::peek
or ParseStream::fork
).