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::{
Contextable, Error, FormatCode::FilterQuery as FilterQueryCode, FormatCode::Regex as RegexCode,
FormatCode::ScriptQuery as ScriptQueryCode, Result,
};
use crate::script::{IntoScriptName, ScriptName};
use crate::tag::TagFilter;
use regex::Regex;
use serde::Serialize;
use std::str::FromStr;
mod util;
pub use util::*;
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum EditQuery {
NewAnonimous,
Query(ScriptQuery),
}
impl Default for EditQuery {
fn default() -> Self {
EditQuery::Query(ScriptQuery {
inner: ScriptQueryInner::Prev(1),
bang: false,
})
}
}
impl FromStr for EditQuery {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Ok(if s == "." {
EditQuery::NewAnonimous
} else {
EditQuery::Query(s.parse()?)
})
}
}
use crate::util::serialize_to_string;
#[derive(Debug, Serialize)]
pub enum ListQuery {
#[serde(serialize_with = "serialize_to_string")]
Pattern(Regex),
Query(ScriptQuery),
}
impl FromStr for ListQuery {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
if s.find("*").is_some() {
let s = s.replace(".", r"\.");
let s = s.replace("*", ".*");
let re = Regex::new(&format!("^{}$", s)).map_err(|e| {
log::error!("正規表達式錯誤:{}", e);
Error::Format(RegexCode, s)
})?;
Ok(ListQuery::Pattern(re))
} else {
Ok(ListQuery::Query(s.parse()?))
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ScriptQuery {
inner: ScriptQueryInner,
bang: bool,
}
impl std::fmt::Display for ScriptQuery {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.inner {
ScriptQueryInner::Fuzz(fuzz) => write!(f, "{}", fuzz),
ScriptQueryInner::Exact(e) => write!(f, "={}", e),
ScriptQueryInner::Prev(p) => write!(f, "^{}", p),
}?;
if self.bang {
write!(f, "!")?;
}
Ok(())
}
}
impl Serialize for ScriptQuery {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
let s = self.to_string();
serializer.serialize_str(&s)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
enum ScriptQueryInner {
Fuzz(String),
Exact(ScriptName),
Prev(usize),
}
impl IntoScriptName for ScriptQuery {
fn into_script_name(self) -> Result<ScriptName> {
match self.inner {
ScriptQueryInner::Fuzz(s) => s.into_script_name(),
ScriptQueryInner::Exact(name) => Ok(name),
_ => panic!("歷史查詢沒有名字"),
}
}
}
fn parse_prev(s: &str) -> Result<usize> {
let mut is_pure_prev = true;
for ch in s.chars() {
if ch != '^' {
is_pure_prev = false;
break;
}
}
if is_pure_prev {
return Ok(s.len());
}
match s[1..s.len()].parse::<usize>() {
Ok(0) => Err(Error::Format(ScriptQueryCode, s.to_owned())).context("歷史查詢不可為0"),
Ok(prev) => Ok(prev),
Err(e) => Err(Error::Format(ScriptQueryCode, s.to_owned()))
.context(format!("解析整數錯誤:{}", e)),
}
}
impl FromStr for ScriptQuery {
type Err = Error;
fn from_str(mut s: &str) -> Result<Self> {
let bang = if s.ends_with("!") {
if s == "!" {
return Ok(ScriptQuery {
inner: ScriptQueryInner::Prev(1),
bang: true,
});
}
s = &s[..s.len() - 1];
true
} else {
false
};
let inner = if s.starts_with('=') {
s = &s[1..s.len()];
let name = s.to_owned().into_script_name()?;
ScriptQueryInner::Exact(name)
} else if s == "-" {
ScriptQueryInner::Prev(1)
} else if s.starts_with('^') {
ScriptQueryInner::Prev(parse_prev(s)?)
} else {
ScriptName::valid(s).context("模糊搜尋仍需符合腳本名格式!")?;
ScriptQueryInner::Fuzz(s.to_owned())
};
Ok(ScriptQuery { inner, bang })
}
}
#[derive(Debug, Serialize)]
pub struct FilterQuery {
pub name: Option<String>,
pub content: TagFilter,
}
impl FromStr for FilterQuery {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let arr: Vec<&str> = s.split("=").collect();
match AsRef::<[&str]>::as_ref(&arr) {
&[s] => {
log::trace!("解析無名篩選器:{}", s);
Ok(FilterQuery {
name: None,
content: s.parse()?,
})
}
&[name, s] => {
log::trace!("解析有名篩選器:{} = {}", name, s);
let content: TagFilter = if s.len() == 0 {
Default::default()
} else {
s.parse()?
};
Ok(FilterQuery {
name: Some(name.to_owned()),
content,
})
}
_ => Err(Error::Format(FilterQueryCode, s.to_owned())),
}
}
}