1use std::fmt;
5
6use name::{Name, OwnedName};
7use escape::escape_str_attribute;
8
9#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
13pub struct Attribute<'a> {
14 pub name: Name<'a>,
16
17 pub value: &'a str
19}
20
21impl<'a> fmt::Display for Attribute<'a> {
22 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23 write!(f, "{}=\"{}\"", self.name, escape_str_attribute(self.value))
24 }
25}
26
27impl<'a> Attribute<'a> {
28 #[inline]
30 pub fn to_owned(&self) -> OwnedAttribute {
31 OwnedAttribute {
32 name: self.name.into(),
33 value: self.value.into(),
34 }
35 }
36
37 #[inline]
39 pub fn new(name: Name<'a>, value: &'a str) -> Attribute<'a> {
40 Attribute { name, value, }
41 }
42}
43
44#[derive(Clone, Eq, PartialEq, Hash, Debug)]
48pub struct OwnedAttribute {
49 pub name: OwnedName,
51
52 pub value: String
54}
55
56impl OwnedAttribute {
57 pub fn borrow(&self) -> Attribute {
59 Attribute {
60 name: self.name.borrow(),
61 value: &*self.value,
62 }
63 }
64
65 #[inline]
67 pub fn new<S: Into<String>>(name: OwnedName, value: S) -> OwnedAttribute {
68 OwnedAttribute {
69 name,
70 value: value.into(),
71 }
72 }
73}
74
75impl fmt::Display for OwnedAttribute {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 write!(f, "{}=\"{}\"", self.name, escape_str_attribute(&*self.value))
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::{Attribute};
84
85 use name::Name;
86
87 #[test]
88 fn attribute_display() {
89 let attr = Attribute::new(
90 Name::qualified("attribute", "urn:namespace", Some("n")),
91 "its value with > & \" ' < weird symbols"
92 );
93
94 assert_eq!(
95 &*attr.to_string(),
96 "{urn:namespace}n:attribute=\"its value with > & " ' < weird symbols\""
97 )
98 }
99}