Skip to content

Commit 181c2ae

Browse files
committed
fix: refactor analyticsquery is_valid
1 parent a8eb0ba commit 181c2ae

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

primitives/src/analytics.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
use serde::{Deserialize, Serialize};
22

3-
#[derive(Debug, Deserialize)]
4-
pub struct AnalyticsQuery {
5-
#[serde(default = "default_limit")]
6-
pub limit: u32,
7-
#[serde(default = "default_event_type")]
8-
pub event_type: String,
9-
#[serde(default = "default_metric")]
10-
pub metric: String,
11-
#[serde(default = "default_timeframe")]
12-
pub timeframe: String,
13-
}
14-
153
#[derive(Debug, Serialize, Deserialize)]
164
pub struct AnalyticsResponse {
175
time: u32,
@@ -33,23 +21,35 @@ pub mod postgres {
3321
}
3422
}
3523

24+
#[derive(Debug, Deserialize)]
25+
pub struct AnalyticsQuery {
26+
#[serde(default = "default_limit")]
27+
pub limit: u32,
28+
#[serde(default = "default_event_type")]
29+
pub event_type: String,
30+
#[serde(default = "default_metric")]
31+
pub metric: String,
32+
#[serde(default = "default_timeframe")]
33+
pub timeframe: String,
34+
}
35+
3636
impl AnalyticsQuery {
3737
pub fn is_valid(&self) -> Result<(), String> {
3838
let valid_event_types = ["IMPRESSION"];
3939
let valid_metric = ["eventPayouts", "eventCounts"];
4040
let valid_timeframe = ["year", "month", "week", "day", "hour"];
4141

42-
if !valid_event_types.iter().any(|e| *e == &self.event_type[..]) {
42+
if !valid_event_types.contains(&self.event_type.as_str()) {
4343
Err(format!(
4444
"invalid event_type, possible values are: {}",
4545
valid_event_types.join(" ,")
4646
))
47-
} else if !valid_metric.iter().any(|e| *e == &self.metric[..]) {
47+
} else if !valid_metric.contains(&self.metric.as_str()) {
4848
Err(format!(
4949
"invalid metric, possible values are: {}",
5050
valid_metric.join(" ,")
5151
))
52-
} else if !valid_timeframe.iter().any(|e| *e == &self.timeframe[..]) {
52+
} else if !valid_timeframe.contains(&self.timeframe.as_str()) {
5353
Err(format!(
5454
"invalid timeframe, possible values are: {}",
5555
valid_timeframe.join(" ,")

0 commit comments

Comments
 (0)