Skip to content

Commit 4208916

Browse files
committed
More changes required and Options for some inputs
1 parent 85e5b22 commit 4208916

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

primitives/src/targeting.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ impl Input {
3030
.map(|ad_view| Value::Bool(ad_view.has_custom_preferences))
3131
.ok_or(Error::UnknownVariable),
3232
"adSlotId" => Ok(Value::String(self.global.ad_slot_id.clone())),
33+
"adSlotType" => Ok(Value::String(self.global.ad_slot_type.clone())),
3334
"adUnitId" => Ok(Value::String(self.global.ad_unit_id.clone())),
34-
"adUnitType" => Ok(Value::String(self.global.ad_unit_type.clone())),
3535
"publisherId" => Ok(Value::String(self.global.publisher_id.clone())),
3636
"advertiserId" => Ok(Value::String(self.global.advertiser_id.clone())),
37-
"country" => Ok(Value::String(self.global.country.clone())),
37+
"country" => self.global.country.clone().map(Value::String).ok_or(Error::UnknownVariable),
3838
"eventType" => Ok(Value::String(self.global.event_type.clone())),
3939
"campaignId" => Ok(Value::String(self.global.campaign_id.clone())),
4040
"campaignTotalSpent" => Ok(Value::String(self.global.campaign_total_spent.clone())),
@@ -51,9 +51,9 @@ impl Input {
5151
self.global.publisher_earned_from_campaign.clone(),
5252
)),
5353
"secondsSinceEpoch" => Ok(Value::Number(self.global.seconds_since_epoch.into())),
54-
"userAgentOS" => Ok(Value::String(self.global.user_agent_os.clone())),
54+
"userAgentOS" => self.global.user_agent_os.clone().map(Value::String).ok_or(Error::UnknownVariable),
5555
"userAgentBrowserFamily" => {
56-
Ok(Value::String(self.global.user_agent_browser_family.clone()))
56+
self.global.user_agent_browser_family.clone().map(Value::String).ok_or(Error::UnknownVariable)
5757
}
5858
"adSlot.categories" => self
5959
.ad_slot
@@ -74,8 +74,9 @@ impl Input {
7474
.ok_or(Error::UnknownVariable),
7575
"adSlot.alexaRank" => {
7676
let ad_slot = self.ad_slot.as_ref().ok_or(Error::UnknownVariable)?;
77+
let alexa_rank = ad_slot.alexa_rank.ok_or(Error::UnknownVariable)?;
7778

78-
match serde_json::Number::from_f64(ad_slot.alexa_rank) {
79+
match serde_json::Number::from_f64(alexa_rank) {
7980
Some(number) => Ok(Value::Number(number)),
8081
None => Err(Error::TypeError),
8182
}
@@ -95,13 +96,19 @@ pub struct AdView {
9596
#[derive(Debug, Clone)]
9697
#[cfg_attr(test, derive(Default))]
9798
pub struct Global {
99+
/// Global scope, accessible everywhere
98100
pub ad_slot_id: String,
99-
pub ad_unit_id: String,
100-
pub ad_unit_type: String,
101+
pub ad_slot_type: String,
101102
pub publisher_id: String,
102-
pub advertiser_id: String,
103-
pub country: String,
103+
pub country: Option<String>,
104104
pub event_type: String,
105+
pub seconds_since_epoch: u64,
106+
pub user_agent_os: Option<String>,
107+
pub user_agent_browser_family: Option<String>,
108+
/// Global scope, accessible everywhere, campaign-dependant
109+
pub ad_unit_id: String,
110+
// adUnitCategories
111+
pub advertiser_id: String,
105112
pub campaign_id: String,
106113
pub campaign_total_spent: String,
107114
pub campaign_seconds_active: u64,
@@ -110,17 +117,14 @@ pub struct Global {
110117
pub event_min_price: BigNum,
111118
pub event_max_price: BigNum,
112119
pub publisher_earned_from_campaign: BigNum,
113-
pub seconds_since_epoch: u64,
114-
pub user_agent_os: String,
115-
pub user_agent_browser_family: String,
116120
}
117121

118122
#[derive(Debug, Clone)]
119123
#[cfg_attr(test, derive(Default))]
120124
pub struct AdSlot {
121125
pub categories: Vec<String>,
122126
pub hostname: String,
123-
pub alexa_rank: f64,
127+
pub alexa_rank: Option<f64>,
124128
}
125129

126130
#[derive(Debug)]
@@ -197,6 +201,12 @@ mod test {
197201
.expect("Should get the global.campaign_budget field");
198202

199203
assert_eq!(Value::BigNum(BigNum::from(50)), global_campaign_budget);
204+
205+
assert_eq!(Err(Error::UnknownVariable), input.try_get("adSlot.alexaRank"));
206+
let mut ad_slot = AdSlot::default();
207+
ad_slot.alexa_rank = Some(20.0);
208+
input.ad_slot = Some(ad_slot);
209+
assert!(input.try_get("adSlot.alexaRank").is_ok());
200210
}
201211

202212
#[test]

primitives/src/targeting/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ mod test {
628628
input.ad_slot = Some(AdSlot {
629629
categories: vec!["Bitcoin".to_string(), "Ethereum".to_string()],
630630
hostname: Default::default(),
631-
alexa_rank: 0.0,
631+
alexa_rank: Some(0.0),
632632
});
633633

634634
let mut output = Output {
@@ -655,7 +655,7 @@ mod test {
655655
input.ad_slot = Some(AdSlot {
656656
categories: vec!["Advertisement".to_string(), "Programming".to_string()],
657657
hostname: Default::default(),
658-
alexa_rank: 0.0,
658+
alexa_rank: Some(0.0),
659659
});
660660

661661
let result = rules.eval(&input, &mut output).expect("Should eval rules");

0 commit comments

Comments
 (0)