[][src]Derive Macro macro_input::MacroInput

    // Attributes available to this derive:

automatically derive TryFrom<&[syn::Attribute]> and fn strip(attrs: &mut Vec<syn::Attribute>)

supported types:

  • u8, i32, f32, char, bool, String or Vec<u8> for parsing literals eg #[foo(bar = 3)]
  • Option<u8>, Option<i32>, Option<f32>, Option<char>, Option<bool>, Option<String> or Option<Vec<u8>> for optionally parsing literals eg either #[foo(bar = 3)] or nothing
  • Option<()> for parsing flags eg #[foo(bar)]
  • Vec<Ident> and Option<Vec<Ident>> for parsing idents eg #[foo(bar(baz, qux))]

paths get converted to lower_snake unless rename is specified

use #[macro_input] for customization:

  • rename to rename either the path or field name eg #[macro_input(rename = "some_name")]
  • default_value for default values eg #[macro_input(default_value = "some literal")] (this is not supported for idents)


use macro_input_macros::MacroInput;
use std::convert::TryFrom;
use syn::{parse_quote, Attribute};

#[derive(MacroInput, PartialEq, Debug)]
pub struct SomeInput {
    pub flag: Option<()>,
    pub optional: Option<i32>,
    #[macro_input(default_value = 3)]
    pub with_default: i32,
    pub required: i32,

#[derive(MacroInput, PartialEq, Debug)]
#[macro_input(rename = "YetAnotherName")]
pub struct OtherInput {
    #[macro_input(rename = "new_name")]
    pub renamed: i32,

// construct some attributes
let attr1: Attribute = parse_quote!(#[some_input(flag, required = 5)]);
let attr2: Attribute = parse_quote!(#[some_input(optional = 8, with_default = 4)]);
let attr3: Attribute = parse_quote!(#[YetAnotherName(new_name = 6)]);

// parse SomeInput with only some attributes
let input1 = SomeInput::try_from(&[attr1.clone()] as &[Attribute])?;
// parse SomeInput with all attributes
let input2 = SomeInput::try_from(&[attr1, attr2] as &[Attribute])?;
// parse OtherInput
let input3 = OtherInput::try_from(&[attr3] as &[Attribute])?;

assert_eq!(input1, SomeInput { flag: Some(()), optional: None, with_default: 3, required: 5 });
assert_eq!(input2, SomeInput { flag: Some(()), optional: Some(8), with_default: 4, required: 5 });
assert_eq!(input3, OtherInput { renamed: 6 });