@@ -35,6 +35,7 @@ mod campaign_id {
35
35
pub struct CampaignId ( [ u8 ; 16 ] ) ;
36
36
37
37
impl CampaignId {
38
+ /// Generates randomly a `CampaignId` using `Uuid::new_v4().to_simple()`
38
39
pub fn new ( ) -> Self {
39
40
Self :: default ( )
40
41
}
@@ -184,32 +185,26 @@ pub struct Campaign {
184
185
}
185
186
186
187
impl Campaign {
187
- pub fn find_validator ( & self , validator : ValidatorId ) -> Option < & ' _ ValidatorDesc > {
188
+ pub fn find_validator ( & self , validator : & ValidatorId ) -> Option < ValidatorRole < ' _ > > {
188
189
match ( self . leader ( ) , self . follower ( ) ) {
189
- ( Some ( leader) , _) if leader. id == validator => Some ( leader) ,
190
- ( _, Some ( follower) ) if follower. id == validator => Some ( follower) ,
190
+ ( Some ( leader) , _) if & leader. id == validator => Some ( ValidatorRole :: Leader ( leader) ) ,
191
+ ( _, Some ( follower) ) if & follower. id == validator => {
192
+ Some ( ValidatorRole :: Follower ( follower) )
193
+ }
191
194
_ => None ,
192
195
}
193
196
}
194
197
195
198
/// Matches the Channel.leader to the Campaign.spec.leader
196
199
/// If they match it returns `Some`, otherwise, it returns `None`
197
200
pub fn leader ( & self ) -> Option < & ' _ ValidatorDesc > {
198
- if self . channel . leader == self . validators . leader ( ) . id {
199
- Some ( self . validators . leader ( ) )
200
- } else {
201
- None
202
- }
201
+ self . validators . find ( & self . channel . leader )
203
202
}
204
203
205
204
/// Matches the Channel.follower to the Campaign.spec.follower
206
205
/// If they match it returns `Some`, otherwise, it returns `None`
207
206
pub fn follower ( & self ) -> Option < & ' _ ValidatorDesc > {
208
- if self . channel . follower == self . validators . follower ( ) . id {
209
- Some ( self . validators . follower ( ) )
210
- } else {
211
- None
212
- }
207
+ self . validators . find ( & self . channel . follower )
213
208
}
214
209
215
210
/// Returns the pricing of a given event
@@ -289,10 +284,10 @@ pub mod validators {
289
284
use serde:: { Deserialize , Serialize } ;
290
285
291
286
#[ derive( Serialize , Deserialize , Debug , Clone , Eq , PartialEq ) ]
292
- /// A ( leader, follower) tuple
287
+ /// Unordered list of the validators representing the leader & follower
293
288
pub struct Validators ( ValidatorDesc , ValidatorDesc ) ;
294
289
295
- #[ derive( Debug ) ]
290
+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
296
291
pub enum ValidatorRole < ' a > {
297
292
Leader ( & ' a ValidatorDesc ) ,
298
293
Follower ( & ' a ValidatorDesc ) ,
@@ -308,33 +303,15 @@ pub mod validators {
308
303
}
309
304
310
305
impl Validators {
311
- pub fn new ( leader : ValidatorDesc , follower : ValidatorDesc ) -> Self {
312
- Self ( leader, follower)
313
- }
314
-
315
- pub fn leader ( & self ) -> & ValidatorDesc {
316
- & self . 0
306
+ pub fn new ( validators : ( ValidatorDesc , ValidatorDesc ) ) -> Self {
307
+ Self ( validators. 0 , validators. 1 )
317
308
}
318
309
319
- pub fn follower ( & self ) -> & ValidatorDesc {
320
- & self . 1
321
- }
322
-
323
- pub fn find ( & self , validator_id : & ValidatorId ) -> Option < ValidatorRole < ' _ > > {
324
- if & self . leader ( ) . id == validator_id {
325
- Some ( ValidatorRole :: Leader ( & self . leader ( ) ) )
326
- } else if & self . follower ( ) . id == validator_id {
327
- Some ( ValidatorRole :: Follower ( & self . follower ( ) ) )
328
- } else {
329
- None
330
- }
331
- }
332
-
333
- pub fn find_index ( & self , validator_id : & ValidatorId ) -> Option < u32 > {
334
- if & self . leader ( ) . id == validator_id {
335
- Some ( 0 )
336
- } else if & self . follower ( ) . id == validator_id {
337
- Some ( 1 )
310
+ pub fn find ( & self , validator_id : & ValidatorId ) -> Option < & ValidatorDesc > {
311
+ if & self . 0 . id == validator_id {
312
+ Some ( & self . 0 )
313
+ } else if & self . 1 . id == validator_id {
314
+ Some ( & self . 1 )
338
315
} else {
339
316
None
340
317
}
@@ -346,8 +323,8 @@ pub mod validators {
346
323
}
347
324
348
325
impl From < ( ValidatorDesc , ValidatorDesc ) > for Validators {
349
- fn from ( ( leader , follower ) : ( ValidatorDesc , ValidatorDesc ) ) -> Self {
350
- Self ( leader , follower )
326
+ fn from ( validators : ( ValidatorDesc , ValidatorDesc ) ) -> Self {
327
+ Self ( validators . 0 , validators . 1 )
351
328
}
352
329
}
353
330
@@ -383,19 +360,115 @@ pub mod validators {
383
360
0 => {
384
361
self . index += 1 ;
385
362
386
- Some ( self . validators . leader ( ) )
363
+ Some ( & self . validators . 0 )
387
364
}
388
365
1 => {
389
366
self . index += 1 ;
390
367
391
- Some ( self . validators . follower ( ) )
368
+ Some ( & self . validators . 1 )
392
369
}
393
370
_ => None ,
394
371
}
395
372
}
396
373
}
397
374
}
398
375
399
- // TODO: Postgres Campaign
400
- // TODO: Postgres CampaignSpec
401
- // TODO: Postgres Validators
376
+ #[ cfg( feature = "postgres" ) ]
377
+ mod postgres {
378
+ use super :: { Active , Campaign , CampaignId , PricingBounds , Validators } ;
379
+ use bytes:: BytesMut ;
380
+ use postgres_types:: { accepts, to_sql_checked, FromSql , IsNull , Json , ToSql , Type } ;
381
+ use std:: error:: Error ;
382
+ use tokio_postgres:: Row ;
383
+
384
+ impl From < & Row > for Campaign {
385
+ fn from ( row : & Row ) -> Self {
386
+ Self {
387
+ id : row. get ( "id" ) ,
388
+ channel : row. get ( "channel" ) ,
389
+ creator : row. get ( "creator" ) ,
390
+ budget : row. get ( "budget" ) ,
391
+ validators : row. get ( "validators" ) ,
392
+ title : row. get ( "title" ) ,
393
+ pricing_bounds : row. get ( "pricing_bounds" ) ,
394
+ event_submission : row. get ( "event_submission" ) ,
395
+ ad_units : row. get :: < _ , Json < _ > > ( "ad_units" ) . 0 ,
396
+ targeting_rules : row. get ( "targeting_rules" ) ,
397
+ created : row. get ( "created" ) ,
398
+ active : Active {
399
+ from : row. get ( "active_from" ) ,
400
+ to : row. get ( "active_to" ) ,
401
+ } ,
402
+ }
403
+ }
404
+ }
405
+
406
+ impl < ' a > FromSql < ' a > for CampaignId {
407
+ fn from_sql ( ty : & Type , raw : & ' a [ u8 ] ) -> Result < Self , Box < dyn Error + Sync + Send > > {
408
+ let str_slice = <& str as FromSql >:: from_sql ( ty, raw) ?;
409
+
410
+ Ok ( str_slice. parse ( ) ?)
411
+ }
412
+
413
+ accepts ! ( TEXT , VARCHAR ) ;
414
+ }
415
+
416
+ impl ToSql for CampaignId {
417
+ fn to_sql (
418
+ & self ,
419
+ ty : & Type ,
420
+ w : & mut BytesMut ,
421
+ ) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
422
+ self . to_string ( ) . to_sql ( ty, w)
423
+ }
424
+
425
+ accepts ! ( TEXT , VARCHAR ) ;
426
+ to_sql_checked ! ( ) ;
427
+ }
428
+
429
+ impl < ' a > FromSql < ' a > for Validators {
430
+ fn from_sql ( ty : & Type , raw : & ' a [ u8 ] ) -> Result < Self , Box < dyn Error + Sync + Send > > {
431
+ let json = <Json < Self > as FromSql >:: from_sql ( ty, raw) ?;
432
+
433
+ Ok ( json. 0 )
434
+ }
435
+
436
+ accepts ! ( JSONB ) ;
437
+ }
438
+
439
+ impl ToSql for Validators {
440
+ fn to_sql (
441
+ & self ,
442
+ ty : & Type ,
443
+ w : & mut BytesMut ,
444
+ ) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
445
+ Json ( self ) . to_sql ( ty, w)
446
+ }
447
+
448
+ accepts ! ( JSONB ) ;
449
+ to_sql_checked ! ( ) ;
450
+ }
451
+
452
+ impl < ' a > FromSql < ' a > for PricingBounds {
453
+ fn from_sql ( ty : & Type , raw : & ' a [ u8 ] ) -> Result < Self , Box < dyn Error + Sync + Send > > {
454
+ let json = <Json < Self > as FromSql >:: from_sql ( ty, raw) ?;
455
+
456
+ Ok ( json. 0 )
457
+ }
458
+
459
+ accepts ! ( JSONB ) ;
460
+ }
461
+
462
+ impl ToSql for PricingBounds {
463
+ fn to_sql (
464
+ & self ,
465
+ ty : & Type ,
466
+ w : & mut BytesMut ,
467
+ ) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
468
+ Json ( self ) . to_sql ( ty, w)
469
+ }
470
+
471
+ accepts ! ( JSONB ) ;
472
+ to_sql_checked ! ( ) ;
473
+ }
474
+ }
0 commit comments