macroex_derive/
lib.rs

1//! See the documentation of [`macroex`](https://docs.rs/macroex/latest/macroex/)
2
3use proc_macro_error::proc_macro_error;
4mod macros;
5mod metalist;
6
7/// Generates a `FromMacro` implementation
8/// 
9/// * struct `from_one` parses a `{field: value, ..}`, 
10/// * struct `from_many` parses `TypeName {field: value, ..}`
11/// * tuple struct `from_one` parses a `(value, ..)`, 
12/// * tuple struct `from_many` parses `TypeName (value, ..)`
13/// * enum `from_one` parses a `Discriminant` on fieldless variants, 
14/// * enum `from_many` parses a `Discriminant (value, ..)` on tuple variants,
15/// * enum `from_many` parses a `Discriminant {field: value, ..}` on named variants,
16/// 
17/// Attributes:
18/// * `#[macroex(rename_all="snake_case")]`: 
19/// 
20/// Rename all field names. 
21/// 
22/// Allowed on named structs, enums and named enum variants.
23/// * `#[macroex(rename="something_else")]`: 
24/// 
25/// Rename the struct or field name.
26/// * #[macroex(required)]`: 
27/// 
28/// We supply default values to fields by default, unlike serde.
29/// This attribute opts out of that. Required on types not implementing
30/// `Default`.
31/// * `#[macroex(default="4")]`: 
32/// 
33/// Use this expression as the default value.
34/// * `#[macroex(repeat)]`: 
35/// 
36/// Collect duplicate keys into a [`FromIterator`](std::iter::FromIterator).
37#[proc_macro_error]
38#[proc_macro_derive(FromMacro, attributes(macroex))]
39pub fn from_macro(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
40    macros::from_macro(tokens.into()).into()
41}
42
43/// Generates a simple `FromMacro` implementation for macro attributes.
44/// 
45/// This macro is only allowed on named structs and supports 3 basic syntax:
46/// 
47/// * `.., name, ..` parses to `name: true`, which matches a boolean value.
48/// * `.., name = expr, ..` parses to `name: expr`
49/// * `.., name(..), ..` parses to `name: T{ .. }`
50/// 
51/// Other types like fieldless enums can potentially use [`FromMacro`] 
52/// to generated compatible [`FromMacro`] implementations to use with this macro.
53/// 
54/// Unlike [`FromMacro`], we provide `from_many` with no delimiters since that's
55/// how `syn` produces its `TokenStreams`.
56/// 
57#[proc_macro_error]
58#[proc_macro_derive(FromAttrs, attributes(macroex))]
59pub fn from_attrs(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
60    metalist::from_meta(tokens.into()).into()
61}