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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use core::{convert::TryFrom, fmt::Debug, str};
use serde::{
de::{Deserialize, Deserializer, Visitor},
ser::{Serialize, Serializer},
};
use static_assertions::assert_impl_all;
use crate::{Basic, EncodingFormat, Error, Result, Signature, Str, Type};
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct ObjectPath<'a>(Str<'a>);
assert_impl_all!(ObjectPath<'_>: Send, Sync, Unpin);
impl<'a> ObjectPath<'a> {
pub fn as_ref(&self) -> ObjectPath<'_> {
ObjectPath(self.0.as_ref())
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
pub unsafe fn from_bytes_unchecked<'s: 'a>(bytes: &'s [u8]) -> Self {
Self(std::str::from_utf8_unchecked(bytes).into())
}
pub fn from_str_unchecked<'s: 'a>(path: &'s str) -> Self {
Self(path.into())
}
pub fn from_static_str(name: &'static str) -> Result<Self> {
ensure_correct_object_path_str(name.as_bytes())?;
Ok(Self::from_static_str_unchecked(name))
}
pub fn from_static_str_unchecked(name: &'static str) -> Self {
Self(Str::from_static(name))
}
pub fn from_string_unchecked(path: String) -> Self {
Self(path.into())
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn to_owned(&self) -> ObjectPath<'static> {
ObjectPath(self.0.to_owned())
}
pub fn into_owned(self) -> ObjectPath<'static> {
ObjectPath(self.0.into_owned())
}
}
impl std::default::Default for ObjectPath<'_> {
fn default() -> Self {
ObjectPath::from_str_unchecked("/")
}
}
impl<'a> Basic for ObjectPath<'a> {
const SIGNATURE_CHAR: char = 'o';
const SIGNATURE_STR: &'static str = "o";
fn alignment(format: EncodingFormat) -> usize {
match format {
EncodingFormat::DBus => <&str>::alignment(format),
#[cfg(feature = "gvariant")]
EncodingFormat::GVariant => 1,
}
}
}
impl<'a> Type for ObjectPath<'a> {
fn signature() -> Signature<'static> {
Signature::from_static_str_unchecked(Self::SIGNATURE_STR)
}
}
impl<'a> TryFrom<&'a [u8]> for ObjectPath<'a> {
type Error = Error;
fn try_from(value: &'a [u8]) -> Result<Self> {
ensure_correct_object_path_str(value)?;
unsafe { Ok(Self::from_bytes_unchecked(value)) }
}
}
impl<'a> TryFrom<&'a str> for ObjectPath<'a> {
type Error = Error;
fn try_from(value: &'a str) -> Result<Self> {
Self::try_from(value.as_bytes())
}
}
impl<'a> TryFrom<String> for ObjectPath<'a> {
type Error = Error;
fn try_from(value: String) -> Result<Self> {
ensure_correct_object_path_str(value.as_bytes())?;
Ok(Self::from_string_unchecked(value))
}
}
impl<'o> From<&ObjectPath<'o>> for ObjectPath<'o> {
fn from(o: &ObjectPath<'o>) -> Self {
o.clone()
}
}
impl<'a> std::ops::Deref for ObjectPath<'a> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl<'a> PartialEq<str> for ObjectPath<'a> {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl<'a> PartialEq<&str> for ObjectPath<'a> {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl<'a> Debug for ObjectPath<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ObjectPath").field(&self.as_str()).finish()
}
}
impl<'a> std::fmt::Display for ObjectPath<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.as_str(), f)
}
}
impl<'a> Serialize for ObjectPath<'a> {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de: 'a, 'a> Deserialize<'de> for ObjectPath<'a> {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let visitor = ObjectPathVisitor;
deserializer.deserialize_str(visitor)
}
}
struct ObjectPathVisitor;
impl<'de> Visitor<'de> for ObjectPathVisitor {
type Value = ObjectPath<'de>;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("an ObjectPath")
}
#[inline]
fn visit_borrowed_str<E>(self, value: &'de str) -> core::result::Result<ObjectPath<'de>, E>
where
E: serde::de::Error,
{
ObjectPath::try_from(value).map_err(serde::de::Error::custom)
}
#[inline]
fn visit_str<E>(self, value: &str) -> core::result::Result<ObjectPath<'de>, E>
where
E: serde::de::Error,
{
ObjectPath::try_from(String::from(value)).map_err(serde::de::Error::custom)
}
}
fn ensure_correct_object_path_str(path: &[u8]) -> Result<()> {
let mut prev = b'\0';
if path.is_empty() {
return Err(serde::de::Error::invalid_length(0, &"> 0 character"));
}
for i in 0..path.len() {
let c = path[i];
if i == 0 && c != b'/' {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Char(c as char),
&"/",
));
} else if c == b'/' && prev == b'/' {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str("//"),
&"/",
));
} else if path.len() > 1 && i == (path.len() - 1) && c == b'/' {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Char('/'),
&"an alphanumeric character or `_`",
));
} else if !c.is_ascii_alphanumeric() && c != b'/' && c != b'_' {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Char(c as char),
&"an alphanumeric character, `_` or `/`",
));
}
prev = c;
}
Ok(())
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, serde::Serialize, Type)]
pub struct OwnedObjectPath(ObjectPath<'static>);
assert_impl_all!(OwnedObjectPath: Send, Sync, Unpin);
impl OwnedObjectPath {
pub fn into_inner(self) -> ObjectPath<'static> {
self.0
}
}
impl std::ops::Deref for OwnedObjectPath {
type Target = ObjectPath<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::convert::From<OwnedObjectPath> for ObjectPath<'static> {
fn from(o: OwnedObjectPath) -> Self {
o.into_inner()
}
}
impl std::convert::From<OwnedObjectPath> for crate::Value<'static> {
fn from(o: OwnedObjectPath) -> Self {
o.into_inner().into()
}
}
impl<'unowned, 'owned: 'unowned> From<&'owned OwnedObjectPath> for ObjectPath<'unowned> {
fn from(o: &'owned OwnedObjectPath) -> Self {
ObjectPath::from_str_unchecked(o.as_str())
}
}
impl<'a> std::convert::From<ObjectPath<'a>> for OwnedObjectPath {
fn from(o: ObjectPath<'a>) -> Self {
OwnedObjectPath(o.into_owned())
}
}
impl TryFrom<&'_ str> for OwnedObjectPath {
type Error = Error;
fn try_from(value: &str) -> Result<Self> {
Ok(Self::from(ObjectPath::try_from(value)?))
}
}
impl TryFrom<String> for OwnedObjectPath {
type Error = Error;
fn try_from(value: String) -> Result<Self> {
Ok(Self::from(ObjectPath::try_from(value)?))
}
}
impl<'de> Deserialize<'de> for OwnedObjectPath {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let visitor = ObjectPathVisitor;
deserializer
.deserialize_string(visitor)
.map(|v| OwnedObjectPath(v.to_owned()))
}
}