Jooyung Han | a7cd238 | 2021-07-16 01:22:24 +0000 | [diff] [blame] | 1 | //! Contains XML attributes manipulation types and functions. |
| 2 | //! |
| 3 | |
| 4 | use std::fmt; |
| 5 | |
| 6 | use name::{Name, OwnedName}; |
| 7 | use escape::escape_str_attribute; |
| 8 | |
| 9 | /// A borrowed version of an XML attribute. |
| 10 | /// |
| 11 | /// Consists of a borrowed qualified name and a borrowed string value. |
| 12 | #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
| 13 | pub struct Attribute<'a> { |
| 14 | /// Attribute name. |
| 15 | pub name: Name<'a>, |
| 16 | |
| 17 | /// Attribute value. |
| 18 | pub value: &'a str |
| 19 | } |
| 20 | |
| 21 | impl<'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 | |
| 27 | impl<'a> Attribute<'a> { |
| 28 | /// Creates an owned attribute out of this borrowed one. |
| 29 | #[inline] |
| 30 | pub fn to_owned(&self) -> OwnedAttribute { |
| 31 | OwnedAttribute { |
| 32 | name: self.name.into(), |
| 33 | value: self.value.into(), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Creates a borrowed attribute using the provided borrowed name and a borrowed string value. |
| 38 | #[inline] |
| 39 | pub fn new(name: Name<'a>, value: &'a str) -> Attribute<'a> { |
| 40 | Attribute { name, value, } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// An owned version of an XML attribute. |
| 45 | /// |
| 46 | /// Consists of an owned qualified name and an owned string value. |
| 47 | #[derive(Clone, Eq, PartialEq, Hash, Debug)] |
| 48 | pub struct OwnedAttribute { |
| 49 | /// Attribute name. |
| 50 | pub name: OwnedName, |
| 51 | |
| 52 | /// Attribute value. |
| 53 | pub value: String |
| 54 | } |
| 55 | |
| 56 | impl OwnedAttribute { |
| 57 | /// Returns a borrowed `Attribute` out of this owned one. |
| 58 | pub fn borrow(&self) -> Attribute { |
| 59 | Attribute { |
| 60 | name: self.name.borrow(), |
| 61 | value: &*self.value, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// Creates a new owned attribute using the provided owned name and an owned string value. |
| 66 | #[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 | |
| 75 | impl 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)] |
| 82 | mod 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 | } |