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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![warn(missing_docs, clippy::all)]
use lazy_static::lazy_static;
use regex::Regex;
use serde::de::{self, Deserialize, Deserializer, Unexpected};
use serde::ser::{Serialize, Serializer};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::error::Error;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
#[cfg(test)]
mod test;
const RID_CLASS: &str = "ri";
const SEPARATOR: &str = ".";
lazy_static! {
static ref PARSE_REGEX: Regex = Regex::new(
r"(?x)
^
ri
\.
([a-z][a-z0-9\-]*) #service
\.
((?:[a-z0-9][a-z0-9\-]*)?) #instance
\.
([a-z][a-z0-9\-]*) #type
\.
([a-zA-Z0-9_\-\.]+) #locator
$
",
)
.unwrap();
}
#[derive(Clone)]
pub struct ResourceIdentifier {
rid: String,
service_end: usize,
instance_end: usize,
type_end: usize,
}
impl ResourceIdentifier {
#[inline]
pub fn new(s: &str) -> Result<ResourceIdentifier, ParseError> {
s.parse()
}
pub fn from_components(
service: &str,
instance: &str,
type_: &str,
locator: &str,
) -> Result<ResourceIdentifier, ParseError> {
if service.contains('.') || instance.contains('.') || type_.contains('.') {
return Err(ParseError(()));
}
format!("ri.{}.{}.{}.{}", service, instance, type_, locator).parse()
}
#[inline]
pub fn service(&self) -> &str {
let start = RID_CLASS.len() + SEPARATOR.len();
&self.rid[start..self.service_end]
}
#[inline]
pub fn instance(&self) -> &str {
let start = self.service_end + SEPARATOR.len();
&self.rid[start..self.instance_end]
}
#[inline]
pub fn type_(&self) -> &str {
let start = self.instance_end + SEPARATOR.len();
&self.rid[start..self.type_end]
}
#[inline]
pub fn locator(&self) -> &str {
let start = self.type_end + SEPARATOR.len();
&self.rid[start..]
}
#[inline]
pub fn as_str(&self) -> &str {
&self.rid
}
#[inline]
pub fn into_string(self) -> String {
self.rid
}
}
impl FromStr for ResourceIdentifier {
type Err = ParseError;
fn from_str(s: &str) -> Result<ResourceIdentifier, ParseError> {
let captures = match PARSE_REGEX.captures(s) {
Some(captures) => captures,
None => return Err(ParseError(())),
};
Ok(ResourceIdentifier {
rid: s.to_string(),
service_end: captures.get(1).unwrap().end(),
instance_end: captures.get(2).unwrap().end(),
type_end: captures.get(3).unwrap().end(),
})
}
}
impl Serialize for ResourceIdentifier {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.rid.serialize(s)
}
}
impl<'de> Deserialize<'de> for ResourceIdentifier {
fn deserialize<D>(d: D) -> Result<ResourceIdentifier, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(d)?;
ResourceIdentifier::new(&s)
.map_err(|_| de::Error::invalid_value(Unexpected::Str(&s), &"a resource identifier"))
}
}
impl AsRef<str> for ResourceIdentifier {
#[inline]
fn as_ref(&self) -> &str {
&self.rid
}
}
impl Borrow<str> for ResourceIdentifier {
#[inline]
fn borrow(&self) -> &str {
&self.rid
}
}
impl fmt::Debug for ResourceIdentifier {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.rid, fmt)
}
}
impl fmt::Display for ResourceIdentifier {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.rid, fmt)
}
}
impl PartialEq for ResourceIdentifier {
#[inline]
fn eq(&self, other: &ResourceIdentifier) -> bool {
self.rid == other.rid
}
}
impl Eq for ResourceIdentifier {}
impl PartialOrd for ResourceIdentifier {
#[inline]
fn partial_cmp(&self, other: &ResourceIdentifier) -> Option<Ordering> {
self.rid.partial_cmp(&other.rid)
}
#[inline]
fn gt(&self, other: &ResourceIdentifier) -> bool {
self.rid > other.rid
}
#[inline]
fn ge(&self, other: &ResourceIdentifier) -> bool {
self.rid >= other.rid
}
#[inline]
fn lt(&self, other: &ResourceIdentifier) -> bool {
self.rid < other.rid
}
#[inline]
fn le(&self, other: &ResourceIdentifier) -> bool {
self.rid <= other.rid
}
}
impl Ord for ResourceIdentifier {
#[inline]
fn cmp(&self, other: &ResourceIdentifier) -> Ordering {
self.rid.cmp(&other.rid)
}
}
impl Hash for ResourceIdentifier {
#[inline]
fn hash<H>(&self, hasher: &mut H)
where
H: Hasher,
{
self.rid.hash(hasher)
}
}
#[derive(Debug)]
pub struct ParseError(());
impl fmt::Display for ParseError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("invalid resource identifier")
}
}
impl Error for ParseError {}