Skip to content

Commit 6886452

Browse files
committed
primitives - AdUnit - use primitives::IPFS
1 parent ba2a94f commit 6886452

File tree

7 files changed

+39
-13
lines changed

7 files changed

+39
-13
lines changed

primitives/src/ad_unit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use chrono::{DateTime, Utc};
22
use serde::{Deserialize, Serialize};
33

4-
use crate::ValidatorId;
4+
use crate::{ValidatorId, IPFS};
55

66
#[derive(Serialize, Deserialize, Debug, Clone)]
77
#[serde(rename_all = "camelCase")]
88
pub struct AdUnit {
99
/// valid ipfs hash of spec props below
10-
pub ipfs: String,
10+
pub ipfs: IPFS,
1111
/// the type of the ad unit
1212
/// currently, possible values are:
1313
/// legacy_300x250, legacy_250x250, legacy_240x400, legacy_336x280,

primitives/src/ipfs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use thiserror::Error;
55

66
const URL_PREFIX: &str = "ipfs://";
77

8+
pub use cid::{Cid, Error};
9+
810
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq)]
911
#[serde(try_from = "String", into = "String")]
1012
pub struct IPFS(pub cid::Cid);
@@ -43,6 +45,14 @@ impl TryFrom<String> for IPFS {
4345
}
4446
}
4547

48+
impl TryFrom<&String> for IPFS {
49+
type Error = cid::Error;
50+
51+
fn try_from(value: &String) -> Result<Self, Self::Error> {
52+
Self::try_from(value.as_str())
53+
}
54+
}
55+
4656
impl<'a> TryFrom<&'a str> for IPFS {
4757
type Error = cid::Error;
4858

@@ -202,6 +212,9 @@ mod test {
202212
for &ipfs_str in TESTS_IPFS_V1.iter() {
203213
check(ipfs_str, cid::Version::V1)
204214
}
215+
216+
// v0 != v1
217+
assert_ne!(IPFS::try_from(TESTS_IPFS_V0[0]), IPFS::try_from(TESTS_IPFS_V1[0]))
205218
}
206219

207220
#[test]

primitives/src/supermarket.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ pub enum Finalized {
4242

4343
pub mod units_for_slot {
4444
pub mod response {
45+
4546
use crate::{
4647
targeting::{Map, Rule},
47-
BigNum, ChannelId, ChannelSpec, SpecValidators, ValidatorId,
48+
BigNum, ChannelId, ChannelSpec, SpecValidators, ValidatorId, IPFS,
4849
};
4950
use chrono::{
5051
serde::{ts_milliseconds, ts_milliseconds_option},
@@ -131,7 +132,7 @@ pub mod units_for_slot {
131132
#[serde(rename_all = "camelCase")]
132133
pub struct AdUnit {
133134
/// Same as `ipfs`
134-
pub id: String,
135+
pub id: IPFS,
135136
pub media_url: String,
136137
pub media_mime: String,
137138
pub target_url: String,

primitives/src/targeting.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mod input {
4343
match key {
4444
"adUnitId" => {
4545
let ipfs = self.ad_unit.as_ref().map(|ad_unit| ad_unit.id.clone());
46-
Ok(Value::String(ipfs.unwrap_or_default()))
46+
Ok(Value::String(ipfs.map(|ipfs| ipfs.to_string()).unwrap_or_default()))
4747
}
4848

4949
"advertiserId" if channel.is_some() => {
@@ -336,8 +336,9 @@ pub mod input {
336336
.global
337337
.ad_unit
338338
.as_ref()
339-
.map(|ad_unit| ad_unit.ipfs.clone());
340-
Ok(Value::String(ipfs.unwrap_or_default()))
339+
.map(|ad_unit| ad_unit.ipfs.to_string());
340+
341+
Ok(Value::String(ipfs.unwrap_or_default()))
341342
}
342343
"advertiserId" => self
343344
.global
@@ -461,13 +462,13 @@ pub mod input {
461462
#[cfg(test)]
462463
mod test {
463464
use super::*;
464-
use crate::util::tests::prep_db::{DUMMY_CHANNEL, IDS};
465+
use crate::util::tests::prep_db::{DUMMY_CHANNEL, IDS, DUMMY_IPFS};
465466
use chrono::Utc;
466467

467468
#[test]
468469
fn test_try_get_of_input() {
469470
let ad_unit = AdUnit {
470-
ipfs: "Hash".to_string(),
471+
ipfs: DUMMY_IPFS[0].clone(),
471472
ad_type: "legacy_300x250".to_string(),
472473
media_url: "media_url".to_string(),
473474
media_mime: "media_mime".to_string(),

primitives/src/targeting/eval_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use super::*;
22
use crate::{
33
targeting::input,
4-
util::tests::prep_db::{DUMMY_CHANNEL, IDS},
4+
util::tests::prep_db::{DUMMY_CHANNEL, IDS, DUMMY_IPFS},
55
AdUnit, BalancesMap,
66
};
77
use chrono::Utc;
88

99
fn get_default_input() -> input::Source {
1010
let ad_unit = AdUnit {
11-
ipfs: "Hash".to_string(),
11+
ipfs: DUMMY_IPFS[0].clone(),
1212
ad_type: "legacy_300x250".to_string(),
1313
media_url: "media_url".to_string(),
1414
media_mime: "media_mime".to_string(),

primitives/src/util/tests/prep_db.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
channel::{Pricing, PricingBounds},
33
BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, SpecValidators, ValidatorDesc,
4-
ValidatorId,
4+
ValidatorId, IPFS,
55
};
66
use chrono::{TimeZone, Utc};
77
use fake::faker::{Faker, Number};
@@ -84,4 +84,15 @@ lazy_static! {
8484
},
8585
}
8686
};
87+
88+
// CID V0
89+
pub static ref DUMMY_IPFS: [IPFS; 5] = [
90+
IPFS::try_from("QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR").expect("Valid IPFS V0"),
91+
IPFS::try_from("Qmasg8FrbuSQpjFu3kRnZF9beg8rEBFrqgi1uXDRwCbX5f").expect("Valid IPFS V0"),
92+
IPFS::try_from("QmQnu8zrHsuVvnTJsEgDHYA8c1MmRL7YLiMD8uzDUJKcNq").expect("Valid IPFS V0"),
93+
IPFS::try_from("QmYYBULc9QDEaDr8HAXvVWHDmFfL2GvyumYRr1g4ERBC96").expect("Valid IPFS V0"),
94+
// V1 of the V0 ipfs: `QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR`
95+
IPFS::try_from("bafybeif2h3mynaf3ylgdbs6arf6mczqycargt5cqm3rmel3wpjarlswway").expect("Valid IPFS V1"),
96+
];
97+
8798
}

sentry/src/payout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn get_payout(logger: &Logger, channel: &Channel, event: &Event, session: &S
4242
} else {
4343
let ad_unit = ad_unit
4444
.as_ref()
45-
.and_then(|ipfs| channel.spec.ad_units.iter().find(|u| &u.ipfs == ipfs));
45+
.and_then(|ipfs| channel.spec.ad_units.iter().find(|u| &u.ipfs.to_string() == ipfs));
4646

4747
let source = input::Source {
4848
ad_view: None,

0 commit comments

Comments
 (0)