Skip to content

Commit b09a04d

Browse files
committed
use Rules and impl ToSql for Rules
1 parent c8e5b80 commit b09a04d

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

primitives/src/ad_unit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub struct AdUnit {
4545
#[serde(default)]
4646
pub archived: bool,
4747
/// UTC timestamp in milliseconds, changed every time modifiable property is changed
48-
#[serde(default, with = "ts_milliseconds_option", skip_serializing_if = "Option::is_none")]
48+
#[serde(
49+
default,
50+
with = "ts_milliseconds_option",
51+
skip_serializing_if = "Option::is_none"
52+
)]
4953
pub modified: Option<DateTime<Utc>>,
5054
}

primitives/src/channel.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use chrono::{DateTime, Utc};
88
use serde::{Deserialize, Deserializer, Serialize};
99
use serde_hex::{SerHex, StrictPfx};
1010

11-
use crate::{targeting::Rule, AdUnit, BigNum, EventSubmission, ValidatorDesc, ValidatorId};
11+
use crate::{targeting::Rules, AdUnit, BigNum, EventSubmission, ValidatorDesc, ValidatorId};
1212
use hex::{FromHex, FromHexError};
1313

1414
#[derive(Serialize, Deserialize, PartialEq, Eq, Copy, Clone, Hash)]
@@ -96,7 +96,7 @@ pub struct Channel {
9696
#[serde(with = "ts_seconds")]
9797
pub valid_until: DateTime<Utc>,
9898
#[serde(default)]
99-
pub targeting_rules: Vec<Rule>,
99+
pub targeting_rules: Rules,
100100
pub spec: ChannelSpec,
101101
}
102102

@@ -181,7 +181,7 @@ pub struct ChannelSpec {
181181
#[serde(default, skip_serializing_if = "Vec::is_empty")]
182182
pub ad_units: Vec<AdUnit>,
183183
#[serde(default)]
184-
pub targeting_rules: Vec<Rule>,
184+
pub targeting_rules: Rules,
185185
}
186186

187187
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
@@ -370,7 +370,7 @@ mod test {
370370
pub mod postgres {
371371
use super::ChannelId;
372372
use super::{Channel, ChannelSpec};
373-
use crate::targeting::Rule;
373+
use crate::targeting::Rules;
374374
use bytes::BytesMut;
375375
use hex::FromHex;
376376
use postgres_types::{accepts, to_sql_checked, FromSql, IsNull, Json, ToSql, Type};
@@ -385,7 +385,7 @@ pub mod postgres {
385385
deposit_asset: row.get("deposit_asset"),
386386
deposit_amount: row.get("deposit_amount"),
387387
valid_until: row.get("valid_until"),
388-
targeting_rules: row.get::<_, Json<Vec<Rule>>>("targeting_rules").0,
388+
targeting_rules: row.get::<_, Json<Rules>>("targeting_rules").0,
389389
spec: row.get::<_, Json<ChannelSpec>>("spec").0,
390390
}
391391
}

primitives/src/sentry.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::validator::MessageTypes;
22
use crate::{BigNum, Channel, ChannelId, ValidatorId};
33
use chrono::{DateTime, Utc};
4-
use serde::{Deserialize, Serialize};
4+
use serde::{Deserialize, Deserializer, Serialize};
55
use std::collections::HashMap;
66
use std::fmt;
77
use std::hash::Hash;
@@ -122,9 +122,21 @@ pub struct ChannelListResponse {
122122
pub channels: Vec<Channel>,
123123
pub total_pages: u64,
124124
pub total: u64,
125+
// TODO:
126+
#[serde(deserialize_with = "page_u64_from_string")]
125127
pub page: u64,
126128
}
127129

130+
fn page_u64_from_string<'de, D>(deserializer: D) -> Result<u64, D::Error>
131+
where
132+
D: Deserializer<'de>,
133+
{
134+
let page_string = String::deserialize(deserializer)?;
135+
136+
// parse from `String` to `u64`
137+
page_string.parse().map_err(serde::de::Error::custom)
138+
}
139+
128140
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
129141
#[serde(rename_all = "camelCase")]
130142
pub struct LastApprovedResponse {

primitives/src/supermarket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod units_for_slot {
4444
pub mod response {
4545

4646
use crate::{
47-
targeting::{Input, Rule},
47+
targeting::{Input, Rules},
4848
BigNum, ChannelId, ChannelSpec, SpecValidators, ValidatorId, IPFS,
4949
};
5050
use chrono::{
@@ -75,7 +75,7 @@ pub mod units_for_slot {
7575
pub struct Campaign {
7676
#[serde(flatten)]
7777
pub channel: Channel,
78-
pub targeting_rules: Vec<Rule>,
78+
pub targeting_rules: Rules,
7979
pub units_with_price: Vec<UnitsWithPrice>,
8080
}
8181

primitives/src/targeting/eval.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,38 @@ mod rules {
7979
de::{SeqAccess, Visitor},
8080
Deserialize, Deserializer, Serialize,
8181
};
82-
use std::fmt;
82+
use std::{
83+
fmt,
84+
ops::{Deref, DerefMut},
85+
};
8386

8487
use super::Rule;
8588

86-
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
89+
#[derive(Serialize, Debug, Default, Clone, Eq, PartialEq)]
8790
#[serde(transparent)]
8891
/// The Rules is just a `Vec<Rule>` with one difference:
8992
/// When Deserializing it will skip invalid `Rule` instead of returning an error
90-
pub struct Rules(Vec<Rule>);
93+
pub struct Rules(pub Vec<Rule>);
94+
95+
impl Rules {
96+
pub fn new() -> Self {
97+
Self(vec![])
98+
}
99+
}
100+
101+
impl Deref for Rules {
102+
type Target = Vec<Rule>;
103+
104+
fn deref(&self) -> &Self::Target {
105+
&self.0
106+
}
107+
}
108+
109+
impl DerefMut for Rules {
110+
fn deref_mut(&mut self) -> &mut Self::Target {
111+
&mut self.0
112+
}
113+
}
91114

92115
impl<'de> Deserialize<'de> for Rules {
93116
fn deserialize<D>(deserializer: D) -> Result<Rules, D::Error>
@@ -1251,7 +1274,7 @@ pub mod postgres {
12511274
use postgres_types::{accepts, to_sql_checked, IsNull, Json, ToSql, Type};
12521275
use std::error::Error;
12531276

1254-
impl ToSql for Rule {
1277+
impl ToSql for Rules {
12551278
fn to_sql(
12561279
&self,
12571280
ty: &Type,

primitives/src/util/tests/prep_db.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
channel::{Pricing, PricingBounds},
3+
targeting::Rules,
34
BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, SpecValidators, ValidatorDesc,
45
ValidatorId, IPFS,
56
};
@@ -64,15 +65,15 @@ lazy_static! {
6465
creator: ValidatorId::try_from("033ed90e0fec3f3ea1c9b005c724d704501e0196").expect("Should be valid ValidatorId"),
6566
deposit_asset: "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359".to_string(),
6667
deposit_amount: 1_000.into(),
67-
targeting_rules: vec![],
68+
targeting_rules: Rules::new(),
6869
// UNIX timestamp for 2100-01-01
6970
valid_until: Utc.timestamp(4_102_444_800, 0),
7071
spec: ChannelSpec {
7172
title: None,
7273
validators: SpecValidators::new(DUMMY_VALIDATOR_LEADER.clone(), DUMMY_VALIDATOR_FOLLOWER.clone()),
7374
max_per_impression: 10.into(),
7475
min_per_impression: 1.into(),
75-
targeting_rules: vec![],
76+
targeting_rules: Rules::new(),
7677
event_submission: Some(EventSubmission { allow: vec![] }),
7778
// July 29, 2019 7:00:00 AM
7879
created: Utc.timestamp(1_564_383_600, 0),

0 commit comments

Comments
 (0)