Struct hamlet::attr::AttributeList [] [src]

pub struct AttributeList<'a>(_);

A list of Attributes.

This is stored as a plain list instead of a set as in most cases it will be a small collection over which a linear search will be more efficient.

Methods

impl<'a> AttributeList<'a>

fn empty() -> AttributeList<'a>

Return an empty AttributeList

fn from_vec(attrs: Vec<Attribute<'a>>) -> AttributeList<'a>

Note that this does not check for duplicate attribute names. Generally, end users are not expected to call this method, and instead use high-level builder APIs or macros available to make construction easier, such as the provided attrs! macro.

fn into_vec(self) -> Vec<Attribute<'a>>

Pull all attributes out of this collection, useful if you need to perform some more extensive modification.

use hamlet::attr::{ Attribute, AttributeList };
let attrs = attrs!(dataBar = "bar", dataBaz = "baz");

// Namespace all data attributes for some reason.
let attrs = AttributeList::from_vec(
    attrs.into_vec().into_iter()
        .map(|Attribute { name, value }| {
            Attribute::new(name.replace("data-", "data-foo-"), value)
        })
        .collect());

assert_eq!(attrs.get("data-foo-bar"), Some("bar"));

fn get<S>(&self, name: S) -> Option<&str> where S: AsRef<str>

Try and get the value of an attribute.

Examples

let attrs = attrs!(id = "foo");
assert_eq!(attrs.get("id"), Some("foo"));
let attrs = attrs!(id = "foo");
assert_eq!(attrs.get("class"), None);

fn set<N, V>(&mut self, name: N, value: V) where N: Into<Cow<'a, str>>, V: Into<Cow<'a, str>>

Unconditionally set an attribute to a value. If the attribute already exists in the list, update its value, otherwise add a new attribute to the list.

Examples

let mut attrs = attrs!(id = "foo");

attrs.set("id", "bar");

assert_eq!(attrs.get("id"), Some("bar"));
let mut attrs = attrs!(id = "foo");

attrs.set("class", "bar");

assert_eq!(attrs.get("class"), Some("bar"));

fn remove<S>(&mut self, name: S) -> Option<Attribute<'a>> where S: AsRef<str>

Removes and returns the attribute if there was one.

Examples

let mut attrs = attrs!(id = "foo");

assert_eq!(attrs.remove("id").map(|a| a.value).unwrap().as_ref(), "foo");

fn iter<'b>(&'b self) -> Iter<'b, 'a>

Returns an iterator over the list.

Trait Implementations

impl<'a, 'b> PartialEq<AttributeList<'b>> for AttributeList<'a>

fn eq(&self, other: &AttributeList<'b>) -> bool

fn ne(&self, other: &Rhs) -> bool

Derived Implementations

impl<'a> Eq for AttributeList<'a>

impl<'a> Debug for AttributeList<'a>

fn fmt(&self, __arg_0: &mut Formatter) -> Result

impl<'a> Clone for AttributeList<'a>

fn clone(&self) -> AttributeList<'a>

fn clone_from(&mut self, source: &Self)