pub struct SingularAttr {
pub name: String,
pub value: ArgValue,
}Expand description
Structure describing a procedural macro attribute with an optional value.
The means that if one has something like #[name1], #[name2 = "value"],
#[name3 = ::std::path::PathBuf)] than name1, name2 = "value", and
name3 = ::std::path::PathBuf are three different attributes which can be
parsed and represented by the SingularAttr structure.
NB: For #[attr(arg1, arg2 = value)] style of proc macros use
ParametrizedAttr structure. If you need to support both use Attr
enum.
Internally the structure is composed of the name and value fields,
where name is always a syn::Ident (corresponding name1,
name2, name3 from the sample above) and value is an optional literal
Lit, with corresponding cases of None,
Some(ArgValue::Literal(Lit::Str(LitStr))), and
Some(ArgValue::Type(Type::Path(Path))).
Fields§
§name: StringOptional attribute argument path part; for instance in
#[my(name = value)] or in #[name = value] this is a name part
value: ArgValueAttribute argument value part; for instance in #[name = value] this is
the value part
Implementations§
Source§impl SingularAttr
impl SingularAttr
Sourcepub fn new(name: impl ToString) -> Self
pub fn new(name: impl ToString) -> Self
Constructs named SingularAttr without value
Sourcepub fn with(name: impl ToString, attrs: &[Attribute]) -> Result<Self, Error>
pub fn with(name: impl ToString, attrs: &[Attribute]) -> Result<Self, Error>
Constructs SingularAttr from a vector of all syn-parsed attributes,
selecting single attribute matching the provided name. If there are
multiple instances of the same attribute, fails with
Error::SingularAttrRequired
Sourcepub fn with_literal(name: impl ToString, lit: Lit) -> Self
pub fn with_literal(name: impl ToString, lit: Lit) -> Self
Constructs named SingularAttr setting its value to the provided
literal
Sourcepub fn with_type(name: impl ToString, ty: Type) -> Self
pub fn with_type(name: impl ToString, ty: Type) -> Self
Constructs named SingularAttr setting its value to the provided
rust type value
Sourcepub fn from_attribute(attr: &Attribute) -> Result<Self, Error>
pub fn from_attribute(attr: &Attribute) -> Result<Self, Error>
Constructs SingularAttr from a given syn::Attribute by parsing
its data. Accepts only attributes having form #[attr(name = value)]
and errors for other attribute types with Error::ArgNameMustBeIdent
and Error::SingularAttrRequired
Sourcepub fn literal_value(&self) -> Result<Lit, Error>
pub fn literal_value(&self) -> Result<Lit, Error>
Returns literal value, if any, or fails with
Error::ArgValueRequired. See ArgValue::to_literal_value for the
details.
Sourcepub fn type_value(&self) -> Result<Type, Error>
pub fn type_value(&self) -> Result<Type, Error>
Returns type value, if any, or fails with Error::ArgValueRequired.
See ArgValue::to_literal_value for the details.
Sourcepub fn merge(&mut self, other: Self) -> Result<(), Error>
pub fn merge(&mut self, other: Self) -> Result<(), Error>
Merges data from the other into the self.
§Errors
- Fails with
Error::NamesDontMatchif the names of the self and theotherdo not match - Fails with
Error::MultipleSingularValuesif both self and theotherhas a named argument with the same name but different values.
Sourcepub fn merged(self, other: Self) -> Result<Self, Error>
pub fn merged(self, other: Self) -> Result<Self, Error>
Does merging as in SingularAttr::merge, but unlike it consumes
the self and returns a merged structure in case of the successful
operation. Useful in operation chains.
Sourcepub fn enrich(&mut self, attr: &Attribute) -> Result<(), Error>
pub fn enrich(&mut self, attr: &Attribute) -> Result<(), Error>
Enriches current attribute data by adding information from the provided
syn::Attribute.
§Errors
- Fails with
Error::NamesDontMatchif the names of the self and the provided attribute do not match - Fails with
Error::MultipleSingularValuesif both self and the provided attribute has a named argument with the same name but different values.
Sourcepub fn enriched(self, attr: &Attribute) -> Result<Self, Error>
pub fn enriched(self, attr: &Attribute) -> Result<Self, Error>
Performs enrich operation as in SingularAttr::enrich, but unlike it
consumes the self and returns an enriched structure in case of the
successful operation. Useful in operation chains.
Sourcepub fn check(&mut self, req: ArgValueReq) -> Result<(), Error>
pub fn check(&mut self, req: ArgValueReq) -> Result<(), Error>
Checks that the structure meets provided value requirements (see
ArgValueReq), generating Error if the requirements are not met.
Sourcepub fn checked(self, req: ArgValueReq) -> Result<Self, Error>
pub fn checked(self, req: ArgValueReq) -> Result<Self, Error>
Performs check as in SingularAttr::check, but unlike it consumes the
self and returns a itself in case of the successful operation.
Useful in operation chains.
Trait Implementations§
Source§impl Clone for SingularAttr
impl Clone for SingularAttr
Source§fn clone(&self) -> SingularAttr
fn clone(&self) -> SingularAttr
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SingularAttr
impl RefUnwindSafe for SingularAttr
impl !Send for SingularAttr
impl !Sync for SingularAttr
impl Unpin for SingularAttr
impl UnwindSafe for SingularAttr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 288 bytes