|
1 | 1 | use chrono::serde::ts_milliseconds;
|
2 | 2 | use chrono::{DateTime, Utc};
|
3 | 3 | use serde::{Deserialize, Serialize};
|
| 4 | +use url::Url; |
4 | 5 |
|
5 | 6 | use std::fmt;
|
6 | 7 |
|
7 |
| -use crate::{BalancesMap, BigNum, Channel}; |
| 8 | +use crate::{AdSlot, AdUnit, BalancesMap, BigNum, Channel}; |
| 9 | +pub use ad_unit::AdUnitsResponse; |
8 | 10 |
|
9 | 11 | // Data structs specific to the market
|
10 | 12 | #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
@@ -55,3 +57,249 @@ pub struct Campaign {
|
55 | 57 | pub channel: Channel,
|
56 | 58 | pub status: Status,
|
57 | 59 | }
|
| 60 | + |
| 61 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 62 | +#[serde( |
| 63 | + rename_all = "camelCase", |
| 64 | + from = "ad_slot::ShimResponse", |
| 65 | + into = "ad_slot::ShimResponse" |
| 66 | +)] |
| 67 | +pub struct AdSlotResponse { |
| 68 | + pub slot: AdSlot, |
| 69 | + pub accepted_referrers: Vec<Url>, |
| 70 | + pub categories: Vec<String>, |
| 71 | + pub alexa_rank: Option<f64>, |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Debug, Deserialize)] |
| 75 | +#[serde( |
| 76 | + rename_all = "camelCase", |
| 77 | + from = "ad_unit::ShimAdUnitResponse", |
| 78 | + into = "ad_unit::ShimAdUnitResponse" |
| 79 | +)] |
| 80 | +pub struct AdUnitResponse { |
| 81 | + pub unit: AdUnit, |
| 82 | +} |
| 83 | + |
| 84 | +mod ad_unit { |
| 85 | + use crate::{AdUnit, ValidatorId, IPFS}; |
| 86 | + use chrono::{DateTime, Utc}; |
| 87 | + use serde::{Deserialize, Serialize}; |
| 88 | + |
| 89 | + use super::AdUnitResponse; |
| 90 | + |
| 91 | + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 92 | + #[serde(rename_all = "camelCase", from = "Vec<Shim>", into = "Vec<Shim>")] |
| 93 | + pub struct AdUnitsResponse(pub Vec<AdUnit>); |
| 94 | + |
| 95 | + impl From<Vec<Shim>> for AdUnitsResponse { |
| 96 | + fn from(vec_of_shims: Vec<Shim>) -> Self { |
| 97 | + Self(vec_of_shims.into_iter().map(Into::into).collect()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + impl Into<Vec<Shim>> for AdUnitsResponse { |
| 102 | + fn into(self) -> Vec<Shim> { |
| 103 | + self.0.into_iter().map(Into::into).collect() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 108 | + #[serde(rename_all = "camelCase")] |
| 109 | + pub struct ShimAdUnitResponse { |
| 110 | + unit: Shim, |
| 111 | + } |
| 112 | + |
| 113 | + impl From<AdUnitResponse> for ShimAdUnitResponse { |
| 114 | + fn from(response: AdUnitResponse) -> Self { |
| 115 | + Self { |
| 116 | + unit: response.unit.into(), |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + impl From<ShimAdUnitResponse> for AdUnitResponse { |
| 122 | + fn from(shim: ShimAdUnitResponse) -> Self { |
| 123 | + Self { |
| 124 | + unit: shim.unit.into(), |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 130 | + #[serde(rename_all = "camelCase")] |
| 131 | + /// This AdUnit Shim has only one difference with the Validator [`AdUnit`](crate::AdUnit) |
| 132 | + /// The `created` and `modified` timestamps here are in strings (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Timestamp_string), |
| 133 | + /// instead of being millisecond timestamps |
| 134 | + pub struct Shim { |
| 135 | + pub ipfs: IPFS, |
| 136 | + #[serde(rename = "type")] |
| 137 | + pub ad_type: String, |
| 138 | + pub media_url: String, |
| 139 | + pub media_mime: String, |
| 140 | + pub target_url: String, |
| 141 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 142 | + pub min_targeting_score: Option<f64>, |
| 143 | + pub owner: ValidatorId, |
| 144 | + pub created: DateTime<Utc>, |
| 145 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 146 | + pub title: Option<String>, |
| 147 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 148 | + pub description: Option<String>, |
| 149 | + #[serde(default)] |
| 150 | + pub archived: bool, |
| 151 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 152 | + pub modified: Option<DateTime<Utc>>, |
| 153 | + } |
| 154 | + |
| 155 | + impl Into<AdUnit> for Shim { |
| 156 | + fn into(self) -> AdUnit { |
| 157 | + AdUnit { |
| 158 | + ipfs: self.ipfs, |
| 159 | + ad_type: self.ad_type, |
| 160 | + media_url: self.media_url, |
| 161 | + media_mime: self.media_mime, |
| 162 | + target_url: self.target_url, |
| 163 | + min_targeting_score: self.min_targeting_score, |
| 164 | + owner: self.owner, |
| 165 | + created: self.created, |
| 166 | + title: self.title, |
| 167 | + description: self.description, |
| 168 | + archived: self.archived, |
| 169 | + modified: self.modified, |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + impl From<AdUnit> for Shim { |
| 175 | + fn from(ad_unit: AdUnit) -> Self { |
| 176 | + Self { |
| 177 | + ipfs: ad_unit.ipfs, |
| 178 | + ad_type: ad_unit.ad_type, |
| 179 | + media_url: ad_unit.media_url, |
| 180 | + media_mime: ad_unit.media_mime, |
| 181 | + target_url: ad_unit.target_url, |
| 182 | + min_targeting_score: ad_unit.min_targeting_score, |
| 183 | + owner: ad_unit.owner, |
| 184 | + created: ad_unit.created, |
| 185 | + title: ad_unit.title, |
| 186 | + description: ad_unit.description, |
| 187 | + archived: ad_unit.archived, |
| 188 | + modified: ad_unit.modified, |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +mod ad_slot { |
| 195 | + use std::collections::HashMap; |
| 196 | + |
| 197 | + use chrono::{DateTime, Utc}; |
| 198 | + use serde::{Deserialize, Serialize}; |
| 199 | + use url::Url; |
| 200 | + |
| 201 | + use crate::{targeting::Rule, AdSlot, BigNum, ValidatorId}; |
| 202 | + |
| 203 | + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 204 | + #[serde(rename_all = "camelCase")] |
| 205 | + pub struct ShimResponse { |
| 206 | + pub slot: Shim, |
| 207 | + pub accepted_referrers: Vec<Url>, |
| 208 | + pub categories: Vec<String>, |
| 209 | + pub alexa_rank: Option<f64>, |
| 210 | + } |
| 211 | + |
| 212 | + impl From<super::AdSlotResponse> for ShimResponse { |
| 213 | + fn from(response: super::AdSlotResponse) -> Self { |
| 214 | + Self { |
| 215 | + slot: Shim::from(response.slot), |
| 216 | + accepted_referrers: response.accepted_referrers, |
| 217 | + categories: response.categories, |
| 218 | + alexa_rank: response.alexa_rank, |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + impl From<ShimResponse> for super::AdSlotResponse { |
| 224 | + fn from(shim_response: ShimResponse) -> Self { |
| 225 | + Self { |
| 226 | + slot: shim_response.slot.into(), |
| 227 | + accepted_referrers: shim_response.accepted_referrers, |
| 228 | + categories: shim_response.categories, |
| 229 | + alexa_rank: shim_response.alexa_rank, |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 235 | + #[serde(rename_all = "camelCase")] |
| 236 | + /// This AdSlot Shim has only one difference with the Validator `primitives::AdSlot` |
| 237 | + /// The `created` and `modified` timestamps here are in strings (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Timestamp_string), |
| 238 | + /// instead of being millisecond timestamps |
| 239 | + pub struct Shim { |
| 240 | + pub ipfs: String, |
| 241 | + #[serde(rename = "type")] |
| 242 | + pub ad_type: String, |
| 243 | + #[serde(default)] |
| 244 | + pub min_per_impression: Option<HashMap<String, BigNum>>, |
| 245 | + #[serde(default)] |
| 246 | + pub rules: Vec<Rule>, |
| 247 | + #[serde(default)] |
| 248 | + pub fallback_unit: Option<String>, |
| 249 | + pub owner: ValidatorId, |
| 250 | + /// DateTime uses `RFC 3339` by default |
| 251 | + /// This is not the usual `milliseconds timestamp` |
| 252 | + /// as the original |
| 253 | + pub created: DateTime<Utc>, |
| 254 | + #[serde(default)] |
| 255 | + pub title: Option<String>, |
| 256 | + #[serde(default)] |
| 257 | + pub description: Option<String>, |
| 258 | + #[serde(default)] |
| 259 | + pub website: Option<String>, |
| 260 | + #[serde(default)] |
| 261 | + pub archived: bool, |
| 262 | + /// DateTime uses `RFC 3339` by default |
| 263 | + /// This is not the usual `milliseconds timestamp` |
| 264 | + /// as the original |
| 265 | + pub modified: Option<DateTime<Utc>>, |
| 266 | + } |
| 267 | + |
| 268 | + impl From<AdSlot> for Shim { |
| 269 | + fn from(ad_slot: AdSlot) -> Self { |
| 270 | + Self { |
| 271 | + ipfs: ad_slot.ipfs, |
| 272 | + ad_type: ad_slot.ad_type, |
| 273 | + min_per_impression: ad_slot.min_per_impression, |
| 274 | + rules: ad_slot.rules, |
| 275 | + fallback_unit: ad_slot.fallback_unit, |
| 276 | + owner: ad_slot.owner, |
| 277 | + created: ad_slot.created, |
| 278 | + title: ad_slot.title, |
| 279 | + description: ad_slot.description, |
| 280 | + website: ad_slot.website, |
| 281 | + archived: ad_slot.archived, |
| 282 | + modified: ad_slot.modified, |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + impl Into<AdSlot> for Shim { |
| 288 | + fn into(self) -> AdSlot { |
| 289 | + AdSlot { |
| 290 | + ipfs: self.ipfs, |
| 291 | + ad_type: self.ad_type, |
| 292 | + min_per_impression: self.min_per_impression, |
| 293 | + rules: self.rules, |
| 294 | + fallback_unit: self.fallback_unit, |
| 295 | + owner: self.owner, |
| 296 | + created: self.created, |
| 297 | + title: self.title, |
| 298 | + description: self.description, |
| 299 | + website: self.website, |
| 300 | + archived: self.archived, |
| 301 | + modified: self.modified, |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | +} |
0 commit comments