| 1 | //! Contains XML attributes manipulation types and functions. |
| 2 | //! |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 5 | |
| 6 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::escaperust-analyzer cargo xml-rs 0.8.19 escape/::{AttributeEscapesrust-analyzer cargo xml-rs 0.8.19 escape/AttributeEscapes#, Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#}; |
| 7 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::namerust-analyzer cargo xml-rs 0.8.19 name/::{Namerust-analyzer cargo xml-rs 0.8.19 name/Name#, OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#}; |
| 8 | |
| 9 | /// A borrowed version of an XML attribute. |
| 10 | /// |
| 11 | /// Consists of a borrowed qualified name and a borrowed string value. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 13 | pub struct Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#<'a> { |
| 14 | /// Attribute name. |
| 15 | pub namerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#name.: Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a>, |
| 16 | |
| 17 | /// Attribute value. |
| 18 | pub valuerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#value.: &'a str, |
| 19 | } |
| 20 | |
| 21 | impl<'a> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#<'a> { |
| 22 | fn fmtrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[`Attribute<'a>`][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 23 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}=\"{}\"", self.namerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#name., Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#::<AttributeEscapesrust-analyzer cargo xml-rs 0.8.19 escape/AttributeEscapes#>::newrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`]new().(self.valuerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#value.)) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl<'a> Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#<'a> { |
| 28 | /// Creates an owned attribute out of this borrowed one. |
| 29 | #[inline] |
| 30 | #[must_use] |
| 31 | pub fn to_ownedrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[`Attribute<'a>`]to_owned().(&self) -> OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 32 | OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 33 | namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name.: self.namerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#name..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 34 | valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value.: self.valuerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#value..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Creates a borrowed attribute using the provided borrowed name and a borrowed string value. |
| 39 | #[inline] |
| 40 | #[must_use] |
| 41 | pub fn newrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[`Attribute<'a>`]new().(name: Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a>, value: &'a str) -> Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#<'a> { |
| 42 | Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute# { namerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#name., valuerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#value. } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// An owned version of an XML attribute. |
| 47 | /// |
| 48 | /// Consists of an owned qualified name and an owned string value. |
| 49 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 50 | pub struct OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 51 | /// Attribute name. |
| 52 | pub namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name.: OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, |
| 53 | |
| 54 | /// Attribute value. |
| 55 | pub valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 56 | } |
| 57 | |
| 58 | impl OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 59 | /// Returns a borrowed `Attribute` out of this owned one. |
| 60 | #[must_use] |
| 61 | #[inline] |
| 62 | pub fn borrowrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[OwnedAttribute]borrow().(&self) -> Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#<'_> { |
| 63 | Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute# { |
| 64 | namerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#name.: self.namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name..borrowrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]borrow().(), |
| 65 | valuerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#value.: &self.valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value., |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Creates a new owned attribute using the provided owned name and an owned string value. |
| 70 | #[inline] |
| 71 | pub fn newrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[OwnedAttribute]new().<S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>(name: OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, value: S) -> OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 72 | OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 73 | namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name., |
| 74 | valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value.: value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { |
| 80 | fn fmtrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[OwnedAttribute][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 81 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}=\"{}\"", self.namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name., Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#::<AttributeEscapesrust-analyzer cargo xml-rs 0.8.19 escape/AttributeEscapes#>::newrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`]new().(&self.valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value.)) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #[cfg(test)] |
| 86 | mod testsrust-analyzer cargo xml-rs 0.8.19 attribute/tests/ { |
| 87 | use superrust-analyzer cargo xml-rs 0.8.19 attribute/::Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#; |
| 88 | |
| 89 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::namerust-analyzer cargo xml-rs 0.8.19 name/::Namerust-analyzer cargo xml-rs 0.8.19 name/Name#; |
| 90 | |
| 91 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 92 | fn attribute_displayrust-analyzer cargo xml-rs 0.8.19 attribute/tests/attribute_display().() { |
| 93 | let attr = Attributerust-analyzer cargo xml-rs 0.8.19 attribute/Attribute#::newrust-analyzer cargo xml-rs 0.8.19 attribute/impl#[`Attribute<'a>`]new().( |
| 94 | Namerust-analyzer cargo xml-rs 0.8.19 name/Name#::qualifiedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]qualified().("attribute", "urn:namespace", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("n")), |
| 95 | "its value with > & \" ' < weird symbols", |
| 96 | ); |
| 97 | |
| 98 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 99 | &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().attr.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 100 | "{urn:namespace}n:attribute=\"its value with > & " ' < weird symbols\"" |
| 101 | ); |
| 102 | } |
| 103 | } |