Skip to content

Commit 2a90e20

Browse files
committed
primitives:
- sentry - Add Borrow<str> for EventTyep - targeting - Output - use EventType for price
1 parent 5e2d5e3 commit 2a90e20

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

primitives/src/sentry.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ mod event {
175175
}
176176
}
177177

178+
impl std::borrow::Borrow<str> for EventType {
179+
fn borrow(&self) -> &str {
180+
&self.as_str()
181+
}
182+
}
183+
178184
impl From<EventType> for String {
179185
fn from(event_type: EventType) -> Self {
180186
event_type.to_string()

primitives/src/targeting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Output {
3434
/// For example: price.IMPRESSION
3535
/// The default is the min of the bound of event type:
3636
/// Default: pricingBounds.IMPRESSION.min
37-
pub price: HashMap<String, UnifiedNum>,
37+
pub price: HashMap<EventType, UnifiedNum>,
3838
}
3939

4040
impl Output {
@@ -62,7 +62,7 @@ impl From<&Campaign> for Output {
6262
let price = campaign
6363
.pricing_bounds
6464
.iter()
65-
.map(|(key, price)| (key.to_string(), price.min))
65+
.map(|(key, price)| (*key, price.min))
6666
.collect();
6767

6868
Self {
@@ -84,7 +84,7 @@ mod test {
8484
let output = Output {
8585
show: false,
8686
boost: 5.5,
87-
price: vec![("one".to_string(), 100.into())].into_iter().collect(),
87+
price: vec![(IMPRESSION, 100.into())].into_iter().collect(),
8888
};
8989

9090
assert_eq!(Ok(Value::Bool(false)), output.try_get("show"));
@@ -96,7 +96,7 @@ mod test {
9696
);
9797
assert_eq!(
9898
Ok(Value::UnifiedNum(100.into())),
99-
output.try_get("price.one")
99+
output.try_get("price.IMPRESSION")
100100
);
101101
assert_eq!(Err(Error::UnknownVariable), output.try_get("price.unknown"));
102102
assert_eq!(Err(Error::UnknownVariable), output.try_get("unknown"));

primitives/src/targeting/eval.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::{unified_num::FromWhole, UnifiedNum};
1+
use crate::{
2+
sentry::{CLICK, IMPRESSION},
3+
unified_num::FromWhole,
4+
UnifiedNum,
5+
};
26
use serde::{Deserialize, Serialize};
37
use serde_json::{value::Value as SerdeValue, Number};
48
use std::{
@@ -1004,7 +1008,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
10041008
.try_unified()?;
10051009

10061010
// we do not care about any other old value
1007-
output.price.insert("IMPRESSION".to_string(), price);
1011+
output.price.insert(IMPRESSION, price);
10081012
}
10091013
"price.CLICK" => {
10101014
let price = rule
@@ -1013,7 +1017,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
10131017
.try_unified()?;
10141018

10151019
// we do not care about any other old value
1016-
output.price.insert("CLICK".to_string(), price);
1020+
output.price.insert(CLICK, price);
10171021
}
10181022
_ => return Err(Error::UnknownVariable),
10191023
}

0 commit comments

Comments
 (0)