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>
impl<'a> Def<'a>
Sourcepub const fn new(
path: &'a str,
name: &'a str,
required: bool,
default: DefaultValue,
) -> Self
pub const fn new( path: &'a str, name: &'a str, required: bool, default: DefaultValue, ) -> Self
create a new field definition
Sourcepub fn strip(&self, attrs: &mut Vec<Attribute>)
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]);
Sourcepub fn get_meta(&self, attrs: &[Attribute]) -> Result<Option<Meta>>
pub fn get_meta(&self, attrs: &[Attribute]) -> Result<Option<Meta>>
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
Sourcepub fn get_lit(&self, attrs: &[Attribute]) -> Result<Option<Lit>>
pub fn get_lit(&self, attrs: &[Attribute]) -> Result<Option<Lit>>
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
Sourcepub fn get<L: Parse>(&self, attrs: &[Attribute]) -> Result<Option<L>>
pub fn get<L: Parse>(&self, attrs: &[Attribute]) -> Result<Option<L>>
try to parse the literal that has the value for this field
§Errors
may return the error if parsing fails
Sourcepub fn get_value<V: FromMeta>(&self, attrs: &[Attribute]) -> Result<V>
pub fn get_value<V: FromMeta>(&self, attrs: &[Attribute]) -> Result<V>
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