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
use crate::error::{DisplayError, DisplayResult, Error, FormatCode::Tag as TagCode};
use crate::util::illegal_name;
use crate::{impl_de_by_from_str, impl_ser_by_to_string};
use fxhash::FxHashSet as HashSet;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
pub type TagSet = HashSet<Tag>;
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct TagSelectorGroup(Vec<TagSelector>);
impl TagSelectorGroup {
pub fn push(&mut self, selector: TagSelector) {
if selector.append {
self.0.push(selector);
} else {
self.0 = vec![selector];
}
}
pub fn select(&self, tags: &TagSet) -> bool {
let mut pass = false;
for f in self.0.iter() {
let res = f.select(tags);
if f.mandatory {
if res != Some(true) {
return false;
}
} else if let Some(res) = res {
pass = res;
}
}
pass
}
}
impl From<TagSelector> for TagSelectorGroup {
fn from(t: TagSelector) -> Self {
TagSelectorGroup(vec![t])
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TagSelector {
tags: Vec<TagControl>,
pub append: bool,
pub mandatory: bool,
}
impl_de_by_from_str!(TagSelector);
impl_ser_by_to_string!(TagSelector);
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TagControl {
allow: bool,
tag: Tag,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
pub struct Tag(String);
impl AsRef<str> for Tag {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Tag {
pub fn match_all(&self) -> bool {
&self.0 == "all"
}
}
impl FromStr for Tag {
type Err = DisplayError;
fn from_str(s: &str) -> DisplayResult<Self> {
if illegal_name(s) {
log::error!("標籤格式不符:{}", s);
return Err(Error::Format(TagCode, s.to_owned()).into());
}
Ok(Tag(s.to_owned()))
}
}
impl FromStr for TagControl {
type Err = DisplayError;
fn from_str(mut s: &str) -> DisplayResult<Self> {
let allow = if s.starts_with('^') {
s = &s[1..s.len()];
false
} else {
true
};
Ok(TagControl {
tag: s.parse()?,
allow,
})
}
}
const MANDATORY_SUFFIX: &str = "!";
const APPEND_PREFIX: &str = "+";
impl FromStr for TagSelector {
type Err = DisplayError;
fn from_str(mut s: &str) -> DisplayResult<Self> {
let append = if s.starts_with(APPEND_PREFIX) {
s = &s[APPEND_PREFIX.len()..];
true
} else {
false
};
let mandatory = if s.ends_with(MANDATORY_SUFFIX) {
s = &s[0..(s.len() - MANDATORY_SUFFIX.len())];
true
} else {
false
};
let mut tags = vec![];
for ctrl in s.split(',') {
tags.push(ctrl.parse()?);
}
if tags.is_empty() {
return Err(Error::Format(TagCode, s.to_owned()).into());
}
Ok(TagSelector {
tags,
append,
mandatory,
})
}
}
impl Display for TagSelector {
fn fmt(&self, w: &mut Formatter<'_>) -> FmtResult {
let mut first = true;
if self.append {
write!(w, "{}", APPEND_PREFIX)?;
}
for f in self.tags.iter() {
if !first {
write!(w, ",")?;
}
first = false;
if !f.allow {
write!(w, "^")?;
}
write!(w, "{}", f.tag.0)?;
}
if self.mandatory {
write!(w, "{}", MANDATORY_SUFFIX)?;
}
Ok(())
}
}
impl TagSelector {
pub fn push(&mut self, flow: Self) {
if flow.append {
self.tags.extend(flow.tags.into_iter());
} else {
*self = flow
}
}
pub fn fill_allowed_map<U>(self, set: &mut std::collections::HashSet<Tag, U>)
where
U: std::hash::BuildHasher,
{
for control in self.tags.into_iter() {
if control.allow {
if control.tag.match_all() {
continue;
}
set.insert(control.tag);
} else {
if control.tag.match_all() {
set.clear();
continue;
}
set.remove(&control.tag);
}
}
}
pub fn into_allowed_iter(self) -> impl Iterator<Item = Tag> {
let mut set = HashSet::default();
self.fill_allowed_map(&mut set);
set.into_iter()
}
pub fn select(&self, tags: &TagSet) -> Option<bool> {
let mut pass: Option<bool> = None;
for ctrl in self.tags.iter() {
if ctrl.tag.match_all() || tags.contains(&&ctrl.tag) {
pass = Some(ctrl.allow);
}
}
pass
}
}