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