Function virtue::utils::parse_tagged_attribute

source ·
pub fn parse_tagged_attribute(
    group: &Group,
    prefix: &str
) -> Result<Option<Vec<ParsedAttribute>>>
Expand description

Parse a tagged attribute. This is very helpful for implementing FromAttribute.

A tagged attribute is an attribute in the form of #[prefix(result)]. This function will return Some(result) if the prefix matches.

The contents of the result can be either:

  • ParsedAttribute::Tagged(Ident), e.g. #[serde(skip)] will be Tagged("skip")
  • ParsedAttribute::Property(Ident, lit), e.g. #[bincode(crate = "foo")] will be Property("crate", "foo")

§Examples

use virtue::utils::{parse_tagged_attribute, ParsedAttribute};

// The attribute being parsed
let group: Group = parse_token_stream_group("#[prefix(result, foo = \"bar\")]");

let attributes = parse_tagged_attribute(&group, "prefix").unwrap().unwrap();
let mut iter = attributes.into_iter();

// The stream will contain the contents of the `prefix(...)`
match iter.next() {
    Some(ParsedAttribute::Tag(i)) => {
        assert_eq!(i.to_string(), String::from("result"));
    },
    x => panic!("Unexpected attribute: {:?}", x)
}
 match iter.next() {
    Some(ParsedAttribute::Property(key, val)) => {
        assert_eq!(key.to_string(), String::from("foo"));
        assert_eq!(val.to_string(), String::from("\"bar\""));
    },
    x => panic!("Unexpected attribute: {:?}", x)
}