Struct Def

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

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§

Source§

impl<'a> Def<'a>

Source

pub const fn new( path: &'a str, name: &'a str, required: bool, default: DefaultValue, ) -> Def<'a>

create a new field definition

Source

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

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]);
Source

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

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

Source

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

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

Source

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

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

§Errors

may return the error if parsing fails

Source

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

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

Trait Implementations§

Source§

impl Lint<Vec<Attribute>> for Def<'_>

Source§

fn lint(&self, input: &Vec<Attribute>, c: &mut Collector)

lint the macro input

Auto Trait Implementations§

§

impl<'a> Freeze for Def<'a>

§

impl<'a> RefUnwindSafe for Def<'a>

§

impl<'a> !Send for Def<'a>

§

impl<'a> !Sync for Def<'a>

§

impl<'a> Unpin for Def<'a>

§

impl<'a> UnwindSafe for Def<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.