Skip to main content

Crate serde_tokenstream

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(),
};

§Nested attributes

For attributes that are nested inside a top-level macro, use the from_tokenstream_spanned function. See its help for an example.

Structs§

OrderedMap
A container for pairs that are deserialized from map syntax if the keys are non-unique, or don’t implement Hash or Ord.
ParseWrapper
A wrapper around syn’s Parse trait that implements Deserialize in the context of from_tokenstream.
TokenStreamWrapper
A wrapper around TokenStream that implements Deserialize in the context of from_tokenstream.

Functions§

from_tokenstream
Deserialize an instance of type T from a TokenStream.
from_tokenstream_spanned
Deserialize an instance of type T from a TokenStream with data inside, along with a DelimSpan for the surrounding braces.

Type Aliases§

Error
Alias for syn::Error.
Result
Alias for a Result with the error type serde_tokenstream::Error.