Struct amplify_syn::SingularAttr[][src]

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 [Ident] (corresponding name1, name2, name3 from the sample above) and value is an optional literal [Lit], with corresponding cases of None, Some([AttrArgValue::Lit](Lit::Str([LitStr]))), and Some([AttrArgValue::Type](Type::Path(Path))).

Fields

name: String

Optional attribute argument path part; for instance in #[my(name = value)] or in #[name = value] this is a name part

value: ArgValue

Attribute argument value part; for instance in #[name = value] this is the value part

Implementations

impl SingularAttr[src]

pub fn new(name: impl ToString) -> Self[src]

Constructs named SingularAttr without value

pub fn with(name: impl ToString, attrs: &[Attribute]) -> Result<Self, Error>[src]

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

pub fn with_literal(name: impl ToString, lit: Lit) -> Self[src]

Constructs named SingularAttr setting its value to the provided literal

pub fn with_type(name: impl ToString, ty: Type) -> Self[src]

Constructs named SingularAttr setting its value to the provided rust type value

pub fn from_attribute(attr: &Attribute) -> Result<Self, Error>[src]

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

pub fn literal_value(&self) -> Result<Lit, Error>[src]

Returns literal value, if any, or fails with Error::ArgValueRequired. See ArgValue::literal_value for the details.

pub fn type_value(&self) -> Result<Type, Error>[src]

Returns type value, if any, or fails with Error::ArgValueRequired. See ArgValue::literal_value for the details.

pub fn merge(&mut self, other: Self) -> Result<(), Error>[src]

Merges data from the other into the self.

Errors

pub fn merged(self, other: Self) -> Result<Self, Error>[src]

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.

pub fn enrich(&mut self, attr: &Attribute) -> Result<(), Error>[src]

Enriches current attribute data by adding information from the provided syn::Attribute.

Errors

pub fn enriched(self, attr: &Attribute) -> Result<Self, Error>[src]

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.

pub fn check(&mut self, req: ArgValueReq) -> Result<(), Error>[src]

Checks that the structure meets provided value requirements (see [ValueReq]), generating Error if the requirements are not met.

pub fn checked(self, req: ArgValueReq) -> Result<Self, Error>[src]

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

impl Clone for SingularAttr[src]

fn clone(&self) -> SingularAttr[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for SingularAttr[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

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

Performs the conversion.

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.

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

Performs the conversion.