1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use chrono::{
serde::{ts_milliseconds, ts_milliseconds_option},
DateTime, Utc,
};
use serde::{Deserialize, Serialize};
use crate::{ValidatorId, IPFS};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AdUnit {
pub ipfs: IPFS,
#[serde(rename = "type")]
pub ad_type: String,
pub media_url: String,
pub media_mime: String,
pub target_url: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_targeting_score: Option<f64>,
pub owner: ValidatorId,
#[serde(with = "ts_milliseconds")]
pub created: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub archived: bool,
#[serde(
default,
with = "ts_milliseconds_option",
skip_serializing_if = "Option::is_none"
)]
pub modified: Option<DateTime<Utc>>,
}
#[cfg(feature = "postgres")]
mod postgres {
use super::AdUnit;
use bytes::BytesMut;
use std::error::Error;
use tokio_postgres::types::{accepts, to_sql_checked, FromSql, IsNull, Json, ToSql, Type};
impl<'a> FromSql<'a> for AdUnit {
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
let json = <Json<Self> as FromSql>::from_sql(ty, raw)?;
Ok(json.0)
}
accepts!(JSONB);
}
impl ToSql for AdUnit {
fn to_sql(
&self,
ty: &Type,
w: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
Json(self).to_sql(ty, w)
}
accepts!(JSONB);
to_sql_checked!();
}
}