1//! Provides an `Attribute` type which represents an attribute in an XML document.
2
3use xml::escape::escape_str_attribute;
4
5use std::fmt;
6
7/// An attribute of a DOM element.
8///
9/// This is of the form: `name`="`value`"
10///
11/// This does not support prefixed/namespaced attributes yet.
12#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
13pub struct Attribute {
14 /// The name of the attribute.
15 pub name: String,
16 /// The value of the attribute.
17 pub value: String,
18}
19
20impl fmt::Display for Attribute {
21 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
22 write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value))
23 }
24}
25
26impl Attribute {
27 /// Construct a new attribute from the given `name` and `value`.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use minidom::Attribute;
33 ///
34 /// let attr = Attribute::new("name", "value");
35 ///
36 /// assert_eq!(attr.name, "name");
37 /// assert_eq!(attr.value, "value");
38 /// ```
39 pub fn new<N: Into<String>, V: Into<String>>(name: N, value: V) -> Attribute {
40 Attribute {
41 name: name.into(),
42 value: value.into(),
43 }
44 }
45}