[][src]Struct macro_input_core::Def

pub struct Def<'a> {
    pub path: &'a str,
    pub name: &'a str,
    pub required: bool,
    pub default: DefaultValue,
}

a field definition

Example

use macro_input::{DefaultValue, Def};

const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
const BAZ_FIELD: Def = Def::new("foo", "baz", false, DefaultValue::Str(None));

Fields

path: &'a str

the path/namespace of the field

name: &'a str

the name of the field

required: bool

whether or not this field is required

default: DefaultValue

the typed default value

Implementations

impl<'a> Def<'a>[src]

#[must_use]pub const fn new(
    path: &'a str,
    name: &'a str,
    required: bool,
    default: DefaultValue
) -> Self
[src]

create a new field definition

pub fn strip(&self, attrs: &mut Vec<Attribute>)[src]

strip away the attributes for this field

This is useful for attribute macros because rust has no way of knowing which attributes were used.

use macro_input::{DefaultValue, Def};
use syn::{parse_quote, Attribute};

// construct some attributes
let foo_attr: Attribute = parse_quote!(#[foo(bar = false)]);
let other_attr: Attribute = parse_quote!(#[some(thing = "value")]);
let mut attrs = vec![foo_attr, other_attr.clone()];

// strip away all mentions of the field bar in foo
const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
BAR_FIELD.strip(&mut attrs);

// the Vec no longer contains #[foo(bar = false)] but #[some(thing = "value")] remains
assert_eq!(attrs, vec![other_attr]);

pub fn get_meta(&self, attrs: &[Attribute]) -> Result<Option<Meta>>[src]

try to find the meta that has the value for this field

Errors

may return the error if the field is required but not found

pub fn get_lit(&self, attrs: &[Attribute]) -> Result<Option<Lit>>[src]

try to find the literal that has the value for this field

Errors

may return the error if the field is required but not found

pub fn get<L: Parse>(&self, attrs: &[Attribute]) -> Result<Option<L>>[src]

try to parse the literal that has the value for this field

Errors

may return the error if parsing fails

pub fn get_value<V: FromMeta>(&self, attrs: &[Attribute]) -> Result<V>[src]

try to extract the value from the literal that has the value for this field

use macro_input::{DefaultValue, Def};
use syn::{parse_quote, Attribute};

let attr = parse_quote!(#[foo(bar = false)]);
const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
let value = BAR_FIELD.get_value::<bool>(&[attr])?;
assert_eq!(value, false);

Errors

may return an error if the field doesn't exist or has a value of the wrong type

Auto Trait Implementations

impl<'a> RefUnwindSafe for Def<'a>[src]

impl<'a> !Send for Def<'a>[src]

impl<'a> !Sync for Def<'a>[src]

impl<'a> Unpin for Def<'a>[src]

impl<'a> UnwindSafe for Def<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.