Debian Codegraph @

src/name.rs

raw symbols SCIP
1 //! Contains XML qualified names 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 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
6
7 use craterust-analyzer cargo xml-rs 0.8.19 crate/::namespacerust-analyzer cargo xml-rs 0.8.19 namespace/::NS_NO_PREFIXrust-analyzer cargo xml-rs 0.8.19 namespace/NS_NO_PREFIX.;
8
9 /// Represents a qualified XML name.
10 ///
11 /// A qualified name always consists at least of a local name. It can optionally contain
12 /// a prefix; when reading an XML document, if it contains a prefix, it must also contain a
13 /// namespace URI, but this is not enforced statically; see below. The name can contain a
14 /// namespace without a prefix; in that case a default, empty prefix is assumed.
15 ///
16 /// When writing XML documents, it is possible to omit the namespace URI, leaving only
17 /// the prefix. In this case the writer will check that the specifed prefix is bound to some
18 /// URI in the current namespace context. If both prefix and namespace URI are specified,
19 /// it is checked that the current namespace context contains this exact correspondence
20 /// between prefix and namespace URI.
21 ///
22 /// # Prefixes and URIs
23 ///
24 /// A qualified name with a prefix must always contain a proper namespace URI --- names with
25 /// a prefix but without a namespace associated with that prefix are meaningless. However,
26 /// it is impossible to obtain proper namespace URI by a prefix without a context, and such
27 /// context is only available when parsing a document (or it can be constructed manually
28 /// when writing a document). Tying a name to a context statically seems impractical. This
29 /// may change in future, though.
30 ///
31 /// # Conversions
32 ///
33 /// `Name` implements some `From` instances for conversion from strings and tuples. For example:
34 ///
35 /// ```rust
36 /// # use xml::name::Name;
37 /// let n1: Name = "p:some-name".into();
38 /// let n2: Name = ("p", "some-name").into();
39 ///
40 /// assert_eq!(n1, n2);
41 /// assert_eq!(n1.local_name, "some-name");
42 /// assert_eq!(n1.prefix, Some("p"));
43 /// assert!(n1.namespace.is_none());
44 /// ```
45 ///
46 /// This is added to support easy specification of XML elements when writing XML documents.
47 #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, 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:)]
48 pub struct Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
49 /// A local name, e.g. `string` in `xsi:string`.
50 pub local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.: &'a str,
51
52 /// A namespace URI, e.g. `http://www.w3.org/2000/xmlns/`.
53 pub namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>,
54
55 /// A name prefix, e.g. `xsi` in `xsi:string`.
56 pub prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>,
57 }
58
59 impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a str> for Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
60 fn fromrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`][`From<&'a str>`]from().(s: &'a str) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
61 let mut parts = s.splitnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]splitn().(2, ':').fuserust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#fuse().();
62 match (parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/impl#[`Fuse<I>`][Iterator]next().(), parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/fuse/impl#[`Fuse<I>`][Iterator]next().()) {
63 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => Namerust-analyzer cargo xml-rs 0.8.19 name/Name#::localrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]local().(name),
64 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name)) => Namerust-analyzer cargo xml-rs 0.8.19 name/Name#::prefixedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]prefixed().(name, prefix),
65 _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
66 }
67 }
68 }
69
70 impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<(&'a str, &'a str)> for Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
71 fn fromrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`][`From<(&'a str, &'a str)>`]from().((prefix, name): (&'a str, &'a str)) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
72 Namerust-analyzer cargo xml-rs 0.8.19 name/Name#::prefixedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]prefixed().(name, prefix)
73 }
74 }
75
76 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 Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
77 fn fmtrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'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# {
78 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(namespace) = self.namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace. {
79 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{{{namespace}}}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
80 }
81
82 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix) = self.prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix. {
83 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{prefix}:")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
84 }
85
86 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(self.local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.)
87 }
88 }
89
90 impl<'a> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
91 /// Returns an owned variant of the qualified name.
92 #[must_use]
93 pub fn to_ownedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]to_owned().(&self) -> OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
94 OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
95 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: self.local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
96 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: self.namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace..maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().),
97 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: self.prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix..maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().),
98 }
99 }
100
101 /// Returns a new `Name` instance representing plain local name.
102 #[inline]
103 #[must_use]
104 pub fn localrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]local().(local_name: &str) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'_> {
105 Namerust-analyzer cargo xml-rs 0.8.19 name/Name# {
106 local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.,
107 prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
108 namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
109 }
110 }
111
112 /// Returns a new `Name` instance with the given local name and prefix.
113 #[inline]
114 #[must_use]
115 pub fn prefixedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]prefixed().(local_name: &'a str, prefix: &'a str) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
116 Namerust-analyzer cargo xml-rs 0.8.19 name/Name# {
117 local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.,
118 namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
119 prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix),
120 }
121 }
122
123 /// Returns a new `Name` instance representing a qualified name with or without a prefix and
124 /// with a namespace URI.
125 #[inline]
126 #[must_use]
127 pub fn qualifiedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]qualified().(local_name: &'a str, namespace: &'a str, prefix: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a> {
128 Namerust-analyzer cargo xml-rs 0.8.19 name/Name# {
129 local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.,
130 namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(namespace),
131 prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix.,
132 }
133 }
134
135 /// Returns a correct XML representation of this local name and prefix.
136 ///
137 /// This method is different from the autoimplemented `to_string()` because it does not
138 /// include namespace URI in the result.
139 #[must_use]
140 pub fn to_reprrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]to_repr().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
141 self.repr_displayrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]repr_display().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
142 }
143
144 /// Returns a structure which can be displayed with `std::fmt` machinery to obtain this
145 /// local name and prefix.
146 ///
147 /// This method is needed for efficiency purposes in order not to create unnecessary
148 /// allocations.
149 #[inline]
150 #[must_use]
151 pub fn repr_displayrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]repr_display().(&self) -> ReprDisplayrust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#<'_, '_> {
152 ReprDisplayrust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#(self)
153 }
154
155 /// Returns either a prefix of this name or `namespace::NS_NO_PREFIX` constant.
156 #[inline]
157 #[must_use]
158 pub fn prefix_reprrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]prefix_repr().(&self) -> &str {
159 self.prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(NS_NO_PREFIXrust-analyzer cargo xml-rs 0.8.19 namespace/NS_NO_PREFIX.)
160 }
161 }
162
163 /// A wrapper around `Name` whose `Display` implementation prints the wrapped name as it is
164 /// displayed in an XML document.
165 pub struct ReprDisplayrust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#<'a, 'b>(&'a Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'b>);
166
167 impl<'a, 'b: '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 ReprDisplayrust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#<'a, 'b> {
168 fn fmtrust-analyzer cargo xml-rs 0.8.19 name/impl#[`ReprDisplay<'a, 'b>`][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# {
169 match self.0rust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#0..prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix. {
170 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}:{}", prefix, self.0rust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#0..local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.),
171 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => self.0rust-analyzer cargo xml-rs 0.8.19 name/ReprDisplay#0..local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name..fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[str][Display]fmt().(f),
172 }
173 }
174 }
175
176 /// An owned variant of `Name`.
177 ///
178 /// Everything about `Name` applies to this structure as well.
179 #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, 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:)]
180 pub struct OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
181 /// A local name, e.g. `string` in `xsi:string`.
182 pub local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
183
184 /// A namespace URI, e.g. `http://www.w3.org/2000/xmlns/`.
185 pub namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
186
187 /// A name prefix, e.g. `xsi` in `xsi:string`.
188 pub prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
189 }
190
191 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 OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
192 #[inline]
193 fn fmtrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName][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# {
194 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#::fmtrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`][Display]fmt().(&self.borrowrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]borrow().(), f)
195 }
196 }
197
198 impl OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
199 /// Constructs a borrowed `Name` based on this owned name.
200 #[must_use]
201 #[inline]
202 pub fn borrowrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]borrow().(&self) -> Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'_> {
203 Namerust-analyzer cargo xml-rs 0.8.19 name/Name# {
204 local_namerust-analyzer cargo xml-rs 0.8.19 name/Name#local_name.: &self.local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.,
205 namespacerust-analyzer cargo xml-rs 0.8.19 name/Name#namespace.: self.namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().(),
206 prefixrust-analyzer cargo xml-rs 0.8.19 name/Name#prefix.: self.prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().(),
207 }
208 }
209
210 /// Returns a new `OwnedName` instance representing a plain local name.
211 #[inline]
212 pub fn localrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]local().<S>(local_name: S) -> OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# where 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#> {
213 OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
214 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: local_name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(),
215 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
216 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
217 }
218 }
219
220 /// Returns a new `OwnedName` instance representing a qualified name with or without
221 /// a prefix and with a namespace URI.
222 #[inline]
223 pub fn qualifiedrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]qualified().<S1, S2, S3>(local_name: S1, namespace: S2, prefix: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<S3>) -> OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#
224 where S1: 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#>, S2: 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#>, S3: 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#>
225 {
226 OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
227 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: local_name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(),
228 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(namespace.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()),
229 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: prefix.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().),
230 }
231 }
232
233 /// Returns an optional prefix by reference, equivalent to `self.borrow().prefix`
234 /// but avoids extra work.
235 #[inline]
236 #[must_use]
237 pub fn prefix_refrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]prefix_ref().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
238 self.prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
239 }
240
241 /// Returns an optional namespace by reference, equivalen to `self.borrow().namespace`
242 /// but avoids extra work.
243 #[inline]
244 #[must_use]
245 pub fn namespace_refrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName]namespace_ref().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
246 self.namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
247 }
248 }
249
250 impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a>> for OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
251 #[inline]
252 fn fromrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName][`From<Name<'a>>`]from().(n: Namerust-analyzer cargo xml-rs 0.8.19 name/Name#<'a>) -> OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
253 n.to_ownedrust-analyzer cargo xml-rs 0.8.19 name/impl#[`Name<'a>`]to_owned().()
254 }
255 }
256
257 impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
258 type Errrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName][FromStr]Err# = ();
259
260 /// Parses the given string slice into a qualified name.
261 ///
262 /// This function, when finishes sucessfully, always return a qualified
263 /// name without a namespace (`name.namespace == None`). It should be filled later
264 /// using proper `NamespaceStack`.
265 ///
266 /// It is supposed that all characters in the argument string are correct
267 /// as defined by the XML specification. No additional checks except a check
268 /// for emptiness are done.
269 fn from_strrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()> {
270 let mut it = s.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(':');
271
272 let r = match (it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().(), it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().(), it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().()) {
273 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(local_name), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().prefix.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() &&
274 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().local_name.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() =>
275 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((local_name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))),
276 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(local_name), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().local_name.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() =>
277 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((local_name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)),
278 (_, _, _) => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
279 };
280 r.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(local_name, prefix)| OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
281 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.,
282 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
283 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.
284 }).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(())
285 }
286 }
287
288 #[cfg(test)]
289 mod testsrust-analyzer cargo xml-rs 0.8.19 name/tests/ {
290 use superrust-analyzer cargo xml-rs 0.8.19 name/::OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#;
291
292 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
293 fn test_owned_name_from_strrust-analyzer cargo xml-rs 0.8.19 name/tests/test_owned_name_from_str().() {
294 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("prefix:name".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
295 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "name".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
296 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
297 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("prefix".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
298 }));
299
300 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("name".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# {
301 local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "name".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
302 namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
303 prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
304 }));
305
306 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()>(()));
307 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(":".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()>(()));
308 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(":a".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()>(()));
309 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("a:".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()>(()));
310 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("a:b:c".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().(), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#, ()>(()));
311 }
312 }