1use super::*;
2use std::fmt;
3use std::hash::Hash;
4use uuid::Uuid;
5#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
6pub struct ParamPayload {
7 pub param: String,
8 pub payload: String,
9}
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
11pub enum NumDescriptor {
12 Range((i64, i64)),
14 List(Vec<i64>),
16 Random,
17}
18impl NumDescriptor {
19 pub fn matches(&self, num: i64) -> bool {
20 match self {
21 Self::Range((s, e)) => &num >= s && &num <= e,
22 Self::List(l) => l.contains(&num),
23 Self::Random => true,
24 }
25 }
26}
27impl Default for NumDescriptor {
28 fn default() -> Self {
29 Self::Random
30 }
31}
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
33pub enum StringDescriptor {
34 Similar,
35 Uuid(u8),
36 List(Vec<String>),
37 Random,
38}
39impl StringDescriptor {
40 pub fn matches(&self, string: &str) -> bool {
41 match self {
42 Self::Uuid(v) => {
43 if let Ok(u) = Uuid::parse_str(string) {
44 u.get_version_num() as u8 == *v
45 } else {
46 false
47 }
48 }
49 Self::List(l) => l.contains(&(string.to_string())),
50 Self::Random => true,
51 _ => true,
52 }
53 }
54}
55impl Default for StringDescriptor {
56 fn default() -> Self {
57 Self::Random
58 }
59}
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
61pub enum NumType {
62 Integer,
63 Float,
64 }
66impl Default for NumType {
67 fn default() -> Self {
68 Self::Integer
69 }
70}
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
72pub struct ParamDescriptor {
73 pub from: QuePay,
74 pub name: String,
75 pub value: ValueDescriptor,
76}
77#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
78pub struct PayloadDescriptor {
79 pub params: Vec<ParamDescriptor>,
80}
81#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
82pub struct DPath {
83 pub path_ext: String,
84 pub params: PayloadDescriptor,
85}
86#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
87pub enum ValueDescriptor {
88 Number((NumDescriptor, NumType)),
89 String(StringDescriptor),
90 Bool,
91 Unknown,
92}
93impl Default for ValueDescriptor {
94 fn default() -> Self {
95 Self::Unknown
96 }
97}
98#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
99pub enum QuePay {
100 Headers,
101 Path,
102 Query,
103 Payload,
104 Response,
105 None,
106}
107impl Default for QuePay {
108 fn default() -> Self {
109 Self::Payload
110 }
111}
112impl fmt::Display for QuePay {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 use colored::*;
115 match self {
116 Self::Headers => write!(f, "{:16}", "Headers".bold().truecolor(253, 186, 116)),
117 Self::Path => write!(f, "{:16}", "Path".bold().truecolor(147, 197, 253)),
118 Self::Query => write!(f, "{:16}", "Query".bold().truecolor(134, 239, 172)),
119 Self::Payload => write!(f, "{:16}", "Request Payload".bold().truecolor(253, 224, 71)),
120 Self::Response => write!(
121 f,
122 "{:16}",
123 "Response Payload".bold().truecolor(165, 180, 252)
124 ),
125 Self::None => write!(f, ""),
126 }
127 }
128}
129#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
140pub enum StrNum {
141 String(String),
142 Number(u32),
143}
144impl Default for StrNum {
145 fn default() -> Self {
146 Self::String(String::new())
147 }
148}
149#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
150pub enum EpHeaderValue {
151 Payload(ParamDescriptor),
152 Const(StrNum),
153 AuthToken,
154}
155impl Default for EpHeaderValue {
156 fn default() -> Self {
157 Self::Const(StrNum::default())
158 }
159}
160#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
161pub struct EpHeader {
162 pub name: String,
163 pub value: EpHeaderValue,
164}
165#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
181pub struct Split<T> {
182 pub values: Vec<T>,
183 percentages: Vec<u8>,
184}
185impl<T> Default for Split<T>
186where
187 T: PartialEq + Eq + Hash + Clone,
188{
189 fn default() -> Self {
190 Split {
191 values: vec![],
192 percentages: vec![],
193 }
194 }
195}
196impl<T> Split<T>
197where
198 T: PartialEq + Eq + Hash + Clone + Default,
199{
200 pub fn insert(&mut self, value: T, percentage: u8) {
201 if !self.values.contains(&value) {
203 self.values.push(value);
204 self.percentages.push(percentage);
205 }
206 }
207 pub fn from_hashmap(hashmap: &HashMap<T, u32>) -> Self {
208 let total: u32 = hashmap.values().sum();
209 let values = hashmap.keys().cloned().collect::<Vec<T>>();
210 let percentages = hashmap
211 .values()
212 .map(|v| ((v * 100) / total) as u8)
213 .collect::<Vec<u8>>();
214 Split {
215 values,
216 percentages,
217 }
218 }
219 pub fn greatest(&self) -> (T, u8) {
220 let mut p = 0;
221 let mut v = T::default();
222 for i in 0..self.values.len() {
223 if self.percentages[i] > p {
224 p = self.percentages[i];
225 v = self.values[i].clone();
226 }
227 }
228 (v, p)
229 }
230 pub fn filter(&mut self) {
231 let mut new_vals = vec![];
232 let mut new_pers = vec![];
233 for (v, p) in self.values.iter().zip(self.percentages.iter()) {
234 if *p > 2 {
235 new_vals.push(v.clone());
236 new_pers.push(*p);
237 }
238 }
239 self.values = new_vals;
240 self.percentages = new_pers;
241 }
242 pub fn get(&self, val: &T) -> Option<u8> {
243 self.values
244 .iter()
245 .position(|v| v == val)
246 .map(|pos| self.percentages[pos])
247 }
248}
249#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
252pub enum Method {
253 GET,
254 POST,
255 OPTIONS,
256 PATCH,
257 PUT,
258 DELETE,
259 HEAD,
260 TRACE,
261 Other,
262}
263impl Default for Method {
264 fn default() -> Self {
265 Method::GET
266 }
267}
268impl Method {
269 pub fn method_from_str(s: &str) -> Self {
270 match s {
271 "GET" => Method::GET,
272 "POST" => Method::POST,
273 "PUT" => Method::PUT,
274 "PATCH" => Method::PATCH,
275 "DELETE" => Method::DELETE,
276 "OPTIONS" => Method::OPTIONS,
277 "HEAD" => Method::HEAD,
278 "TRACE" => Method::TRACE,
279 _ => Method::Other,
280 }
281 }
282}
283impl std::fmt::Display for Method {
284 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
285 match self {
286 Self::GET => write!(f, "GET"),
287 Self::POST => write!(f, "POST"),
288 Self::PUT => write!(f, "PUT"),
289 Self::OPTIONS => write!(f, "OPTIONS"),
290 Self::PATCH => write!(f, "PATCH"),
291 Self::DELETE => write!(f, "DELETE"),
292 Self::HEAD => write!(f, "HEAD"),
293 Self::TRACE => write!(f, "TRACE"),
294 Self::Other => write!(f, "other"),
295 }
296 }
297}
298#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
299pub enum Token {
300 Uuid(Uuid),
301 String(String),
302 Number(u64),
303 JWT(String),
304}
305impl Default for Token {
306 fn default() -> Self {
307 Self::String(String::new())
308 }
309}
310impl Token {
311 pub fn read(s: String) -> Token {
312 if let Ok(u) = Uuid::parse_str(&s) {
313 Token::Uuid(u)
314 } else if let Ok(i) = s.parse::<u64>() {
315 Token::Number(i)
316 } else {
317 Token::String(s)
318 }
319 }
320}
321pub fn conv_json_pairs(s: &str) -> Vec<ParamPayload> {
322 if let Ok(serde_json::Value::Object(json)) = serde_json::from_str::<serde_json::Value>(s) {
324 let mut ret = vec![];
325 for (param, payload) in json {
326 ret.push(ParamPayload {
327 param,
328 payload: payload.to_string(),
329 });
330 }
331 ret
332 } else if s.trim().starts_with('?') {
333 s[1..]
334 .split('&')
335 .map(|p| {
336 let mut split = p.split('=');
337 ParamPayload {
338 param: split.next().unwrap().to_string(),
339 payload: split.next().unwrap().to_string(),
340 }
341 })
342 .collect::<Vec<ParamPayload>>()
343 } else {
344 vec![]
345 }
346}