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
prelude! {}
use filter::{string_like, FilterExt};
pub type LabelFilter = string_like::StringLikeFilter<LabelSpec>;
pub type LabelPred = string_like::Pred;
pub type LabelUpdate = string_like::Update;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LabelSpec {
Anything,
Value(String),
#[serde(with = "serde_regex")]
Regex(Regex),
}
impl std::cmp::PartialEq for LabelSpec {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Anything, Self::Anything) => true,
(Self::Value(lft), Self::Value(rgt)) => lft == rgt,
(Self::Regex(lft), Self::Regex(rgt)) => lft.as_str() == rgt.as_str(),
(Self::Anything, _) | (Self::Value(_), _) | (Self::Regex(_), _) => false,
}
}
}
impl std::cmp::Eq for LabelSpec {}
impl FilterExt<str> for LabelSpec {
fn apply(&self, label: &str) -> bool {
match self {
LabelSpec::Value(value) => label == value,
LabelSpec::Regex(regex) => regex.is_match(label),
LabelSpec::Anything => true,
}
}
}
impl string_like::SpecExt for LabelSpec {
type Data = alloc::Str;
const DATA_DESC: &'static str = "label";
fn from_string(s: impl Into<String>) -> Res<Self> {
Self::new(s)
}
fn is_empty(&self) -> bool {
match self {
LabelSpec::Value(s) => s == "",
LabelSpec::Regex(_) => false,
LabelSpec::Anything => false,
}
}
fn data_of_alloc(alloc: &Alloc) -> Arc<Vec<Self::Data>> {
alloc.labels()
}
fn matches(&self, data: &Self::Data) -> bool {
match self {
LabelSpec::Value(value) => data == value,
LabelSpec::Regex(regex) => data.str_do(|s| regex.is_match(s)),
LabelSpec::Anything => true,
}
}
fn matches_anything(&self) -> bool {
match self {
Self::Anything => true,
Self::Value(_) => false,
Self::Regex(_) => false,
}
}
}
impl fmt::Display for LabelSpec {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Value(label) => label.fmt(fmt),
Self::Regex(regex) => write!(fmt, "#\"{}\"#", regex),
Self::Anything => write!(fmt, "**"),
}
}
}
impl Default for LabelSpec {
fn default() -> LabelSpec {
LabelSpec::Value("my label".into())
}
}
impl LabelSpec {
pub fn new(s: impl Into<String>) -> Res<Self> {
let label = s.into();
macro_rules! illegal {
() => {{
let err: err::Error = format!("illegal regex `{}`", label).into();
err
}};
}
if label.len() > 2 && &label[0..2] == "#\"" {
if &label[label.len() - 2..label.len()] != "\"#" {
bail!(illegal!().chain_err(|| "a regex must end with `\"#`"))
}
let regex = Regex::new(&label[2..label.len() - 2])
.map_err(|e| illegal!().chain_err(|| format!("{}", e)))?;
Ok(regex.into())
} else {
Ok(label.into())
}
}
pub fn matches_anything(&self) -> bool {
match self {
Self::Anything => true,
Self::Value(_) => false,
Self::Regex(_) => false,
}
}
}
impl From<String> for LabelSpec {
fn from(s: String) -> Self {
if &s == "**" {
Self::Anything
} else {
Self::Value(s)
}
}
}
impl<'a> From<&'a str> for LabelSpec {
fn from(s: &'a str) -> Self {
Self::Value(s.into())
}
}
impl From<Regex> for LabelSpec {
fn from(re: Regex) -> Self {
Self::Regex(re)
}
}
impl Default for string_like::StringLikeFilter<LabelSpec> {
fn default() -> Self {
Self::new(
string_like::Pred::Contain,
vec![
LabelSpec::Anything,
LabelSpec::default(),
LabelSpec::Anything,
],
)
}
}