Crate serde_tokenstream
source · [−]Expand description
This is a serde::Deserializer
implementation for
proc_macro2::TokenStream
. It is intended for proc_macro builders who want
rich configuration in their custom attributes.
If you’d like the consumers of your macro use it like this:
#[my_macro {
settings = {
reticulate_splines = true,
normalizing_power = false,
},
disaster = "pandemic",
}]
Your macro will start like this:
#[proc_macro_attribute]
pub fn my_macro(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
// ...
Use serde_tokenstream
to deserialize attr
into a structure with the
Deserialize
trait (typically via a derive
macro):
let config = match from_tokenstream::<Config>(&TokenStream::from(attr)) {
Ok(c) => c,
Err(err) => return err.to_compile_error().into(),
};
Structs
A Wrapper around proc_macro2::TokenStream that is Deserializable, albeit only in the context of from_tokenstream(). You can use this if, say, your macro allows users to pass in Rust tokens as a configuration option. This can be useful, for example, in a macro that generates code where the caller of that macro might want to augment the generated code.
Functions
Deserialize an instance of type T from a TokenStream.