1 | use axum::Json; |
2 | use serde::{Deserialize, Serialize}; |
3 | use serde_json::{json, Value}; |
4 | use std::io::Error; |
5 | use tracing::{debug, instrument}; |
6 |
|
7 | #[derive(Serialize, Deserialize, Debug)] |
8 | #[serde(tag = "type")] |
9 |
|
10 |
|
11 |
|
12 | pub(crate) enum Event { |
13 | Check(TextCount), |
14 | } |
15 |
|
16 | impl Event { |
17 | pub(crate) fn process(self) -> impl Serialize { |
18 | match self { |
19 | Self::Check(text_count) => text_count.digest().unwrap(), |
20 | } |
21 | } |
22 | } |
23 |
|
24 |
|
25 | trait Consume { |
26 | |
27 | type SerializableResult: Serialize; |
28 | |
29 | fn collect(&mut self) -> &mut Self; |
30 | |
31 | fn digest(self) -> Result<Self::SerializableResult, Error>; |
32 | } |
33 |
|
34 |
|
35 | trait Trigger { |
36 | |
37 | type SerializableResult: Serialize; |
38 | |
39 | fn dispatch(self) -> Result<Self::SerializableResult, Error>; |
40 | } |
41 |
|
42 |
|
43 | pub(crate) async fn post_event(Json(payload): Json<Event>) -> Json<Value> { |
44 | debug!("Received payload {:?}", payload); |
45 | let result = payload.process(); |
46 | Json(json!(result)) |
47 | } |
48 |
|
49 | #[derive(Serialize, Deserialize, Debug)] |
50 |
|
51 | pub(crate) struct TextCount { |
52 | target: String, |
53 | patterns: Vec<String>, |
54 | } |
55 |
|
56 | impl Consume for TextCount { |
57 | type SerializableResult = TextCountResult; |
58 | #[instrument] |
59 | fn collect(&mut self) -> &mut Self { |
60 | debug!("Collecting data for text count"); |
61 | self |
62 | } |
63 |
|
64 | #[instrument] |
65 | fn digest(self) -> Result<Self::SerializableResult, Error> { |
66 | |
67 | let mut pattern_results: Vec<PatternResult> = vec![]; |
68 |
|
69 | for pattern in self.patterns { |
70 | pattern_results.push(PatternResult::new(pattern, 0)) |
71 | } |
72 |
|
73 | let result = TextCountResult::new(self.target, pattern_results); |
74 | Ok(result) |
75 | } |
76 | } |
77 |
|
78 | #[derive(Serialize, Deserialize, Debug)] |
79 |
|
80 | pub(crate) struct PatternResult { |
81 | text: String, |
82 | count: i32, |
83 | } |
84 |
|
85 | impl PatternResult { |
86 | |
87 | fn new(text: String, count: i32) -> Self { |
88 | Self { text, count } |
89 | } |
90 | } |
91 |
|
92 | #[derive(Serialize, Deserialize, Debug)] |
93 |
|
94 | pub(crate) struct TextCountResult { |
95 | target: String, |
96 | patterns: Vec<PatternResult>, |
97 | } |
98 |
|
99 | impl TextCountResult { |
100 | |
101 | fn new(target: String, patterns: Vec<PatternResult>) -> Self { |
102 | Self { target, patterns } |
103 | } |
104 | } |