1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::fmt;
use std::str::FromStr;
use std::borrow::{Cow, ToOwned};
use util::OptionBorrowExt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Name<'a> {
pub local_name: &'a str,
pub namespace: Option<&'a str>,
pub prefix: Option<&'a str>
}
impl<'a> fmt::Display for Name<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(namespace) = self.namespace {
try! { write!(f, "{{{}}}", namespace) }
}
if let Some(prefix) = self.prefix {
try! { write!(f, "{}:", prefix) }
}
write!(f, "{}", self.local_name)
}
}
impl<'a> Name<'a> {
pub fn to_owned(&self) -> OwnedName {
OwnedName {
local_name: self.local_name.to_owned(),
namespace: self.namespace.map(|s| s.to_owned()),
prefix: self.prefix.map(|s| s.to_owned())
}
}
#[inline]
pub fn local(local_name: &str) -> Name {
Name {
local_name: local_name,
prefix: None,
namespace: None
}
}
#[inline]
pub fn qualified(local_name: &'a str, namespace: &'a str, prefix: Option<&'a str>) -> Name<'a> {
Name {
local_name: local_name,
namespace: Some(namespace),
prefix: prefix,
}
}
pub fn to_repr(&self) -> String {
match self.prefix {
Some(prefix) => format!("{:?}:{:?}", prefix, self.local_name),
None => self.local_name.to_owned()
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct OwnedName {
pub local_name: String,
pub namespace: Option<String>,
pub prefix: Option<String>,
}
impl fmt::Display for OwnedName {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.borrow(), f)
}
}
impl OwnedName {
pub fn borrow(&self) -> Name {
Name {
local_name: &*self.local_name,
namespace: self.namespace.as_ref().map(|s| &**s),
prefix: self.prefix.as_ref().map(|s| &**s),
}
}
#[inline]
pub fn local<'s, S: Into<Cow<'s, str>>>(local_name: S) -> OwnedName {
OwnedName {
local_name: local_name.into().into_owned(),
namespace: None,
prefix: None,
}
}
#[inline]
pub fn qualified<'s1, 's2, 's3, S1, S2, S3>(local_name: S1, namespace: S2,
prefix: Option<S3>) -> OwnedName
where S1: Into<Cow<'s1, str>>,
S2: Into<Cow<'s2, str>>,
S3: Into<Cow<'s3, str>> {
OwnedName {
local_name: local_name.into().into_owned(),
namespace: Some(namespace.into().into_owned()),
prefix: prefix.map(|v| v.into().into_owned())
}
}
#[inline]
pub fn prefix_as_ref(&self) -> Option<&str> {
self.prefix.borrow_internals()
}
#[inline]
pub fn namespace_as_ref(&self) -> Option<&str> {
self.namespace.borrow_internals()
}
#[inline]
pub fn to_repr(&self) -> String {
self.borrow().to_repr()
}
}
impl FromStr for OwnedName {
type Err = ();
fn from_str(s: &str) -> Result<OwnedName, ()> {
let mut it = s.split(':');
let r = match (it.next(), it.next(), it.next()) {
(Some(prefix), Some(local_name), None) if !prefix.is_empty() &&
!local_name.is_empty() =>
Some((local_name.to_owned(), Some(prefix.to_owned()))),
(Some(local_name), None, None) if !local_name.is_empty() =>
Some((local_name.to_owned(), None)),
(_, _, _) => None
};
r.map(|(local_name, prefix)| OwnedName {
local_name: local_name,
namespace: None,
prefix: prefix
}).ok_or(())
}
}
#[cfg(test)]
mod tests {
use super::OwnedName;
#[test]
fn test_owned_name_from_str() {
assert_eq!("prefix:name".parse(), Ok(OwnedName {
local_name: "name".to_string(),
namespace: None,
prefix: Some("prefix".to_string())
}));
assert_eq!("name".parse(), Ok(OwnedName {
local_name: "name".to_string(),
namespace: None,
prefix: None
}));
assert_eq!("".parse(), Err::<OwnedName, ()>(()));
assert_eq!(":".parse(), Err::<OwnedName, ()>(()));
assert_eq!(":a".parse(), Err::<OwnedName, ()>(()));
assert_eq!("a:".parse(), Err::<OwnedName, ()>(()));
assert_eq!("a:b:c".parse(), Err::<OwnedName, ()>(()));
}
}