Skip to content

Commit ec887e6

Browse files
committed
primitives - sentry - EventType fix
- rustfmt & clippy fixes
1 parent db6b81b commit ec887e6

File tree

8 files changed

+37
-33
lines changed

8 files changed

+37
-33
lines changed

adview-manager/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1111

1212
[dependencies]
1313
# Domain
14-
adex_primitives = { version="0.2.0", path = "../primitives", package = "primitives" }
14+
adex_primitives = { version = "0.2.0", path = "../primitives", package = "primitives" }
1515
chrono = "0.4"
1616
num-integer = "0.1"
1717
# (De)Serialization & Http requests
@@ -30,5 +30,4 @@ rand = "0.8"
3030

3131
[dev-dependencies]
3232
# enable the `test-util` only in dev
33-
adex_primitives = { version="0.2.0", path = "../primitives", package = "primitives", features = ["test-util"] }
34-
33+
adex_primitives = { version = "0.2.0", path = "../primitives", package = "primitives", features = ["test-util"] }

adview-manager/serve/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{sync::Arc, net::SocketAddr};
1+
use std::{net::SocketAddr, sync::Arc};
22

33
use axum::{routing::get, Extension, Router, Server};
44
use log::info;
55

66
use tera::Tera;
77

8-
use crate::routes::{get_preview_ad, get_index, get_preview_video};
8+
use crate::routes::{get_index, get_preview_ad, get_preview_video};
99

1010
#[derive(Debug)]
1111
pub struct State {

adview-manager/serve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod app;
2-
pub mod routes;
2+
pub mod routes;

primitives/src/sentry.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ mod event {
133133
use once_cell::sync::Lazy;
134134
use parse_display::{Display, FromStr};
135135
use serde::{Deserialize, Serialize};
136-
use std::fmt;
136+
use std::{
137+
fmt,
138+
hash::{Hash, Hasher},
139+
};
137140

138141
use crate::{Address, IPFS};
139142

@@ -145,20 +148,7 @@ mod event {
145148
static IMPRESSION_STRING: Lazy<String> = Lazy::new(|| EventType::Impression.to_string());
146149
static CLICK_STRING: Lazy<String> = Lazy::new(|| EventType::Click.to_string());
147150

148-
#[derive(
149-
Debug,
150-
Display,
151-
FromStr,
152-
Serialize,
153-
Deserialize,
154-
Hash,
155-
Ord,
156-
Eq,
157-
PartialEq,
158-
PartialOrd,
159-
Clone,
160-
Copy,
161-
)]
151+
#[derive(Debug, Display, FromStr, Serialize, Deserialize, Ord, Eq, PartialOrd, Clone, Copy)]
162152
#[display(style = "SNAKE_CASE")]
163153
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
164154
pub enum EventType {
@@ -175,9 +165,24 @@ mod event {
175165
}
176166
}
177167

168+
/// Due to the fact that we want to impl `Borrow<str>`
169+
/// we must provide custom hash impl using the capitalized
170+
/// version of the [`EventType`] as a string.
171+
impl Hash for EventType {
172+
fn hash<H: Hasher>(&self, state: &mut H) {
173+
self.as_str().hash(state);
174+
}
175+
}
176+
177+
impl PartialEq for EventType {
178+
fn eq(&self, other: &Self) -> bool {
179+
self.as_str() == other.as_str()
180+
}
181+
}
182+
178183
impl std::borrow::Borrow<str> for EventType {
179184
fn borrow(&self) -> &str {
180-
&self.as_str()
185+
self.as_str()
181186
}
182187
}
183188

@@ -187,6 +192,7 @@ mod event {
187192
}
188193
}
189194

195+
/// All the [`Event`]s available in the validator stack.
190196
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
191197
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
192198
pub enum Event {
@@ -250,12 +256,14 @@ mod event {
250256

251257
#[cfg(test)]
252258
mod test {
259+
use std::borrow::Borrow;
260+
253261
use crate::sentry::event::{CLICK_STRING, IMPRESSION_STRING};
254262

255263
use super::EventType;
256264

257265
#[test]
258-
fn event_type_parsing_and_de_serialization() {
266+
fn test_event_type_parsing_and_de_serialization() {
259267
let impression_parse = "IMPRESSION"
260268
.parse::<EventType>()
261269
.expect("Should parse IMPRESSION");
@@ -273,6 +281,8 @@ mod event {
273281
assert_eq!(EventType::Impression, impression_json);
274282
assert_eq!(EventType::Click, click_parse);
275283
assert_eq!(EventType::Click, click_json);
284+
285+
assert_eq!(Borrow::<str>::borrow(&EventType::Impression), "IMPRESSION");
276286
}
277287
}
278288
}

primitives/src/targeting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod test {
8484
let output = Output {
8585
show: false,
8686
boost: 5.5,
87-
price: vec![(IMPRESSION, 100.into())].into_iter().collect(),
87+
price: [(IMPRESSION, 100.into())].into_iter().collect(),
8888
};
8989

9090
assert_eq!(Ok(Value::Bool(false)), output.try_get("show"));

sentry/src/payout.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@ pub fn get_payout(
6565
let mut output = Output {
6666
show: true,
6767
boost: 1.0,
68-
price: vec![(event_type.to_string(), pricing.min)]
69-
.into_iter()
70-
.collect(),
68+
price: [(event_type, pricing.min)].into_iter().collect(),
7169
};
7270

7371
let on_type_error = |error, rule| error!(logger, "Rule evaluation error for {:?}", campaign.id; "error" => ?error, "rule" => ?rule);
7472

7573
eval_with_callback(&targeting_rules, &input, &mut output, Some(on_type_error));
7674

7775
if output.show {
78-
let price = match output.price.get(event_type.as_str()) {
76+
let price = match output.price.get(&event_type) {
7977
Some(output_price) => max(pricing.min, min(pricing.max, *output_price)),
8078
None => max(pricing.min, pricing.max),
8179
};

sentry/src/routes/routers.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,7 @@ mod analytics_router_test {
404404
query::{AllowedKey, Time},
405405
AnalyticsQuery, Metric, OperatingSystem, Timeframe,
406406
},
407-
sentry::{
408-
AnalyticsResponse, DateHour, FetchedAnalytics, FetchedMetric, UpdateAnalytics, CLICK,
409-
IMPRESSION,
410-
},
407+
sentry::{AnalyticsResponse, DateHour, FetchedMetric, UpdateAnalytics, CLICK, IMPRESSION},
411408
test_util::{ADVERTISER, DUMMY_CAMPAIGN, DUMMY_IPFS, IDS, LEADER, PUBLISHER, PUBLISHER_2},
412409
UnifiedNum,
413410
};

sentry/src/routes/units_for_slot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ async fn apply_targeting(
357357
show: true,
358358
boost: 1.0,
359359
// only "IMPRESSION" event can be used for this `Output`
360-
price: vec![(IMPRESSION.into(), pricing_bounds.min)]
360+
price: [(IMPRESSION, pricing_bounds.min)]
361361
.into_iter()
362362
.collect(),
363363
};

0 commit comments

Comments
 (0)