Skip to content

Commit a545bc0

Browse files
committed
primitives - PricingBounds::to_vec & From<&Channel> for Output
1 parent 2d7a4bc commit a545bc0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

primitives/src/channel.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ pub struct PricingBounds {
102102
pub click: Option<Pricing>,
103103
}
104104

105+
impl PricingBounds {
106+
pub fn to_vec(&self) -> Vec<(&str, Pricing)> {
107+
let mut vec = Vec::new();
108+
109+
if let Some(pricing) = self.impression.as_ref() {
110+
vec.push(("IMPRESSION", pricing.clone()));
111+
}
112+
113+
if let Some(pricing) = self.click.as_ref() {
114+
vec.push(("CLICK", pricing.clone()))
115+
}
116+
117+
vec
118+
}
119+
}
120+
105121
#[derive(Serialize, Deserialize, Debug, Clone)]
106122
#[serde(rename_all = "camelCase")]
107123
pub struct ChannelSpec {

primitives/src/targeting.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::BigNum;
1+
use crate::{BigNum, Channel};
22
use std::collections::HashMap;
33

44
pub use eval::*;
@@ -139,6 +139,25 @@ pub struct Output {
139139
pub price: HashMap<String, BigNum>,
140140
}
141141

142+
impl From<&Channel> for Output {
143+
fn from(channel: &Channel) -> Self {
144+
let price = match &channel.spec.pricing_bounds {
145+
Some(pricing_bounds) => pricing_bounds
146+
.to_vec()
147+
.into_iter()
148+
.map(|(key, price)| (key.to_string(), price.min))
149+
.collect(),
150+
_ => Default::default(),
151+
};
152+
153+
Self {
154+
show: true,
155+
boost: 1.0,
156+
price,
157+
}
158+
}
159+
}
160+
142161
#[cfg(test)]
143162
mod test {
144163
use super::*;

0 commit comments

Comments
 (0)