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§
- This is a container for pairs that are deserialized from map syntax without requiring the keys to be unique. This is useful for types that don’t implement traits such as
Hash
orOrd
required for map types that offer efficient lookups. The only mechanism to extract data is viainto_iter()
. - A wrapper around the syn::parse::Parse trait that is Deserializable, albeit only in the context of from_tokenstream(). This extends TokenStreamWrapper by further interpreting the TokenStream and guiding the user in the case of parse errors.
- 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.
- Deserialize an instance of type T from a TokenStream with data inside, along with a
DelimSpan
for the surrounding braces.
Type Aliases§
- Alias for
syn::Error
. - Alias for a Result with the error type serde_tokenstream::Error.