pub struct ParametrizedAttr {
pub name: String,
pub args: HashMap<String, ArgValue>,
pub paths: Vec<Path>,
pub string: Option<LitStr>,
pub bytes: Option<LitByteStr>,
pub chars: Vec<LitChar>,
pub integers: Vec<LitInt>,
pub floats: Vec<LitFloat>,
pub bool: Option<LitBool>,
}
Expand description
Representation for all allowed forms of #[attr(...)]
attribute.
If attribute has a multiple occurrences they are all assembled into a single
list. Repeated named arguments are not allowed and result in errors.
For situations like in #[attr("string literal")]
, ParametrizedAttr
will have a name
field set to attr
, literal
field set to
Lit::LitStr(LitStr("string literal"))
, args
will be an empty HashSet
and paths
will be represented by an empty vector.
Fields§
§name: String
Attribute name - attr
part of #[attr(...)]
args: HashMap<String, ArgValue>
All attribute arguments that have form of #[attr(ident = "literal")]
or #[attr(ident = TypeName)]
mapped to their name identifiers.
paths: Vec<Path>
All attribute arguments that are paths or identifiers without any
specific value, like #[attr(std::io::Error, crate, super::SomeType)]
.
NB: All named arguments without the value assigned are getting into this
field by default, even if they do not represent any known rust path or
type name, like #[attr(some_id, other)]
. However, with
ParametrizedAttr::check
and ParametrizedAttr::checked
those
values matching ones specified in AttrReq::arg_req
with values set
to crate::ListReq::Predefined::default
are moved into
ParametrizedAttr::args
.
string: Option<LitStr>
Unnamed string literal found within attribute arguments.
If multiple string literals are present they are concatenated into a
single value, like it is done by the rust compiler for
#[doc = "..."]
attributes
bytes: Option<LitByteStr>
Unnamed byte string literal found within attribute arguments.
If multiple byte string literals are present they are concatenated into
a single value, like it is done by the rust compiler for
#[doc = "..."]
attributes
chars: Vec<LitChar>
Unnamed char literals found within attribute arguments
integers: Vec<LitInt>
Unnamed integer literals found within attribute arguments
floats: Vec<LitFloat>
Unnamed float literals found within attribute arguments
bool: Option<LitBool>
Unnamed bool literal found within attribute arguments.
If multiple bool literals are present this will generate an error.
Implementations§
Source§impl ParametrizedAttr
impl ParametrizedAttr
Sourcepub fn new(name: impl ToString) -> Self
pub fn new(name: impl ToString) -> Self
Constructs named SingularAttr
with empty internal data
Sourcepub fn with(
name: impl ToString + AsRef<str>,
attrs: &[Attribute],
) -> Result<Self, Error>
pub fn with( name: impl ToString + AsRef<str>, attrs: &[Attribute], ) -> Result<Self, Error>
Constructs ParametrizedAttr
from a vector of all syn-parsed
attributes, selecting attributes matching the provided name.
Sourcepub fn from_attribute(attr: &Attribute) -> Result<Self, Error>
pub fn from_attribute(attr: &Attribute) -> Result<Self, Error>
Constructs ParametrizedAttr
from a given syn::Attribute
Sourcepub fn arg_value<T>(&self, name: &str) -> Result<T, Error>
pub fn arg_value<T>(&self, name: &str) -> Result<T, Error>
Returns value for a given argument with name name
, if it is defined,
or fails with Error::ArgValueRequired
.
Sourcepub fn unwrap_arg_value<T>(&self, name: &str) -> T
pub fn unwrap_arg_value<T>(&self, name: &str) -> T
Returns value for a given argument with name name
, if it is defined,
or panics otherwise.
Sourcepub fn arg_literal_value(&self, name: &str) -> Result<Lit, Error>
👎Deprecated: use ArgValue::arg_value
pub fn arg_literal_value(&self, name: &str) -> Result<Lit, Error>
Returns literal value for a given argument with name name
, if it is
defined, or fails with Error::ArgValueRequired
. See
ArgValue::to_literal_value
for the details.
Sourcepub fn has_verbatim(&self, verbatim: &str) -> bool
pub fn has_verbatim(&self, verbatim: &str) -> bool
Checks if the attribute has a verbatim argument matching the provided
verbatim
string.
Verbatim arguments are arguments in form of #[attr(verbatim1, verbatim2]
, i.e. path arguments containing single path segment and no
value or nested arguments.
Sourcepub fn verbatim(&self) -> HashSet<String>
pub fn verbatim(&self) -> HashSet<String>
Returns set of verbatim attribute arguments.
Verbatim arguments are arguments in form of #[attr(verbatim1, verbatim2]
, i.e. path arguments containing single path segment and no
value or nested arguments.
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::NamesDontMatch
if the names of the self and theother
do not match - Fails with
Error::MultipleLiteralValues
if both self and theother
has a literals which values are not equal.
Sourcepub fn merged(self, other: Self) -> Result<Self, Error>
pub fn merged(self, other: Self) -> Result<Self, Error>
Does merging as in ParametrizedAttr::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::NamesDontMatch
if the names of the self and the provided attribute do not match - Fails with
Error::MultipleLiteralValues
if both self and the provided attribute has a literals which values are not equal.
Sourcepub fn enriched(self, attr: &Attribute) -> Result<Self, Error>
pub fn enriched(self, attr: &Attribute) -> Result<Self, Error>
Performs enrich operation as in ParametrizedAttr::enrich
, but unlike
it consumes the self and returns an enriched structure in case of
the successful operation. Useful in operation chains.
Sourcepub fn fuse(&mut self, attr: &Attribute) -> Result<(), Error>
pub fn fuse(&mut self, attr: &Attribute) -> Result<(), Error>
Fuses data from a nested attribute arguments (see Attribute
) into
the attribute parameters.
The operation is similar to the ParametrizedAttr::enrich
with the
only difference that enrichment operation takes the whole attribute, and
fusion takes a nested meta data.
Sourcepub fn fused(self, attr: &Attribute) -> Result<Self, Error>
pub fn fused(self, attr: &Attribute) -> Result<Self, Error>
Performs enrich operation as in ParametrizedAttr::fuse
, 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: AttrReq) -> Result<(), Error>
pub fn check(&mut self, req: AttrReq) -> Result<(), Error>
Checks that the structure meets provided value requirements (see
AttrReq
), generating Error
if the requirements are not met.
The procedure modifies the ParametrizedAttr
data in the following
ways:
- First, it fills in
ParametrizedAttr::paths
,ParametrizedAttr::integers
,ParametrizedAttr::floats
,ParametrizedAttr::chars
,ParametrizedAttr::bytes
,ParametrizedAttr::string
with default values fromAttrReq::path_req
,AttrReq::integer_req
,AttrReq::float_req
,AttrReq::char_req
,AttrReq::bytes_req
,AttrReq::string_req
correspondingly. ParametrizedAttr::paths
values matching ones specified inAttrReq::arg_req
with values set tocrate::ListReq::Predefined::default
are moved intoParametrizedAttr::args
field.
Trait Implementations§
Source§impl Clone for ParametrizedAttr
impl Clone for ParametrizedAttr
Source§fn clone(&self) -> ParametrizedAttr
fn clone(&self) -> ParametrizedAttr
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 ParametrizedAttr
impl RefUnwindSafe for ParametrizedAttr
impl !Send for ParametrizedAttr
impl !Sync for ParametrizedAttr
impl Unpin for ParametrizedAttr
impl UnwindSafe for ParametrizedAttr
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: 192 bytes