Skip to main content

Module ux

Module ux 

Source
Expand description

Better UX for proc-macro. Inspired by crabtime.

Allows to receiving inputs and producing outputs in non TokenStream way. This is the boring goblin craft: fewer raw token piles, more typed little bundles.

E.g. instead of:



fn foo(input: TokenStream) -> TokenStream {
   let parser = syn::punctuated::Punctuated::<syn::LitStr, syn::Token![,]>::parse_terminated;
   let lit_components = parser.parse2(input).unwrap();
   let components = lit_components.iter().map(|c| c.value()).collect::<Vec<_>>();
   // Handling of `components`
}

One could write:


fn foo(components: CommaSeparated<Token>) -> TokenStream {
   // Handling of `components`
}

Since extending syn::parse::Parse with std types is not possible due to orphan rule. We use macro parse_into!, that hardcodes checks for specific types.

Note: having String and Vec<String> in input params remove span information, and reduce IDE/diagnostics quality.

Output is a little bit more simple, it expected in three forms:

  • String - For strings that should be converted to TokenStream without input span information
  • TokenStream - as basic case.
  • and in empty form - for cases where output is already emitted as output_str!, output! macros.

So we have a trait IntoTokenStream that is solely focused on converting specific types into TokenStream.

The user can extend it as well, to support custom types in output.

Structs§

CommaSeparated
Represents a comma separated list of parsable values.
SnifedEntries
Represents a group of snif! entries.
SnifedEntry
Represents a single entry in snif! route.

Enums§

Token
Represents either Ident or LitStr token.

Traits§

IntoTokenStream
Convert specific type into TokenStream.

Functions§

push_output
For some usages, user might want to emit output streamingly, like println! or write! macros.