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
use crate::net::utils::resource_name;
use crate::net::ResKey;
use crate::Path;
use std::convert::{From, TryFrom};
use std::fmt;
use zenoh_util::core::{ZError, ZErrorKind, ZResult};
use zenoh_util::zerror;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PathExpr {
pub(crate) p: String,
}
impl PathExpr {
fn is_valid(path: &str) -> bool {
!path.is_empty() && !path.contains(|c| c == '?' || c == '#' || c == '[' || c == ']')
}
pub fn new(p: String) -> ZResult<PathExpr> {
if !Self::is_valid(&p) {
zerror!(ZErrorKind::InvalidPathExpr { path: p })
} else {
Ok(PathExpr {
p: Path::remove_useless_slashes(&p),
})
}
}
pub fn as_str(&self) -> &str {
self.p.as_str()
}
pub fn is_relative(&self) -> bool {
!self.p.starts_with('/')
}
pub fn is_a_path(&self) -> bool {
!self.p.contains('*')
}
pub fn with_prefix(&self, prefix: &Path) -> Self {
if self.is_relative() {
Self {
p: format!("{}/{}", prefix.p, self.p),
}
} else {
Self {
p: format!("{}{}", prefix.p, self.p),
}
}
}
pub fn strip_prefix(&self, prefix: &Path) -> Option<Self> {
self.p
.strip_prefix(&prefix.p)
.map(|p| PathExpr { p: p.to_string() })
}
pub fn matches(&self, path: &Path) -> bool {
resource_name::intersect(&self.p, &path.p)
}
}
impl fmt::Display for PathExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.p)
}
}
impl TryFrom<String> for PathExpr {
type Error = ZError;
fn try_from(p: String) -> Result<Self, Self::Error> {
PathExpr::new(p)
}
}
impl TryFrom<&str> for PathExpr {
type Error = ZError;
fn try_from(p: &str) -> ZResult<PathExpr> {
Self::try_from(p.to_string())
}
}
impl From<&Path> for PathExpr {
fn from(path: &Path) -> Self {
PathExpr { p: path.p.clone() }
}
}
impl From<Path> for PathExpr {
fn from(path: Path) -> Self {
PathExpr { p: path.p }
}
}
impl From<PathExpr> for ResKey {
fn from(path: PathExpr) -> Self {
ResKey::from(path.p.as_str())
}
}
impl From<&PathExpr> for ResKey {
fn from(path: &PathExpr) -> Self {
ResKey::from(path.p.as_str())
}
}