@@ -35,6 +35,73 @@ pub struct Input {
35
35
pub ad_slot : Option < AdSlot > ,
36
36
}
37
37
38
+ impl Input {
39
+ fn try_get ( & self , key : & str ) -> Result < Value , Error > {
40
+ match key {
41
+ "adView.secondsSinceShow" => self
42
+ . ad_view
43
+ . as_ref ( )
44
+ . map ( |ad_view| Value :: Number ( ad_view. seconds_since_show . into ( ) ) )
45
+ . ok_or ( Error :: UnknownVariable ) ,
46
+ "adView.hasCustomPreferences" => self
47
+ . ad_view
48
+ . as_ref ( )
49
+ . map ( |ad_view| Value :: Bool ( ad_view. has_custom_preferences ) )
50
+ . ok_or ( Error :: UnknownVariable ) ,
51
+ "adSlotId" => Ok ( Value :: String ( self . global . ad_slot_id . clone ( ) ) ) ,
52
+ "adUnitId" => Ok ( Value :: String ( self . global . ad_unit_id . clone ( ) ) ) ,
53
+ "adUnitType" => Ok ( Value :: String ( self . global . ad_unit_type . clone ( ) ) ) ,
54
+ "publisherId" => Ok ( Value :: String ( self . global . publisher_id . clone ( ) ) ) ,
55
+ "advertiserId" => Ok ( Value :: String ( self . global . advertiser_id . clone ( ) ) ) ,
56
+ "country" => Ok ( Value :: String ( self . global . country . clone ( ) ) ) ,
57
+ "eventType" => Ok ( Value :: String ( self . global . event_type . clone ( ) ) ) ,
58
+ "campaignId" => Ok ( Value :: String ( self . global . campiagn_id . clone ( ) ) ) ,
59
+ "campaignTotalSpent" => Ok ( Value :: String ( self . global . campaign_total_spent . clone ( ) ) ) ,
60
+ "campaignSecondsActive" => {
61
+ Ok ( Value :: Number ( self . global . campaign_seconds_active . into ( ) ) )
62
+ }
63
+ "campaignSecondsDuration" => {
64
+ Ok ( Value :: Number ( self . global . campaign_seconds_duration . into ( ) ) )
65
+ }
66
+ "campaignBudget" => Ok ( Value :: BigNum ( self . global . campaign_budget . clone ( ) ) ) ,
67
+ "eventMinPrice" => Ok ( Value :: BigNum ( self . global . event_min_price . clone ( ) ) ) ,
68
+ "eventMaxPrice" => Ok ( Value :: BigNum ( self . global . event_max_price . clone ( ) ) ) ,
69
+ "publisherEarnedFromCampaign" => Ok ( Value :: BigNum (
70
+ self . global . publisher_earned_from_campaign . clone ( ) ,
71
+ ) ) ,
72
+ "secondsSinceEpoch" => Ok ( Value :: Number ( self . global . seconds_since_epoch . into ( ) ) ) ,
73
+ "userAgentOS" => Ok ( Value :: String ( self . global . user_agent_os . clone ( ) ) ) ,
74
+ "userAgentBrowserFamily" => Ok ( Value :: String ( self . global . user_agent_browser_family . clone ( ) ) ) ,
75
+ "adSlot.categories" => self
76
+ . ad_slot
77
+ . as_ref ( )
78
+ . map ( |ad_slot| {
79
+ let array = ad_slot
80
+ . categories
81
+ . iter ( )
82
+ . map ( |string| Value :: String ( string. clone ( ) ) )
83
+ . collect ( ) ;
84
+ Value :: Array ( array)
85
+ } )
86
+ . ok_or ( Error :: UnknownVariable ) ,
87
+ "adSlot.hostname" => self
88
+ . ad_slot
89
+ . as_ref ( )
90
+ . map ( |ad_slot| Value :: String ( ad_slot. hostname . clone ( ) ) )
91
+ . ok_or ( Error :: UnknownVariable ) ,
92
+ "adSlot.alexaRank" => {
93
+ let ad_slot = self . ad_slot . as_ref ( ) . ok_or ( Error :: UnknownVariable ) ?;
94
+
95
+ match serde_json:: Number :: from_f64 ( ad_slot. alexa_rank ) {
96
+ Some ( number) => Ok ( Value :: Number ( number) ) ,
97
+ None => Err ( Error :: TypeError )
98
+ }
99
+ }
100
+ _unknown_field => Err ( Error :: UnknownVariable ) ,
101
+ }
102
+ }
103
+ }
104
+
38
105
#[ derive( Debug , Serialize , Deserialize ) ]
39
106
#[ cfg_attr( test, derive( Default ) ) ]
40
107
#[ serde( rename_all = "camelCase" ) ]
@@ -54,6 +121,7 @@ pub struct Global {
54
121
pub advertiser_id : String ,
55
122
pub country : String ,
56
123
pub event_type : String ,
124
+ pub campiagn_id : String ,
57
125
pub campaign_total_spent : String ,
58
126
pub campaign_seconds_active : u64 ,
59
127
pub campaign_seconds_duration : u64 ,
@@ -76,11 +144,6 @@ pub struct AdSlot {
76
144
pub alexa_rank : f64 ,
77
145
}
78
146
79
- impl TryGet for Input { }
80
- impl TryGet for Global { }
81
- impl TryGet for AdView { }
82
- impl TryGet for AdSlot { }
83
-
84
147
#[ derive( Debug ) ]
85
148
pub struct Output {
86
149
/// Whether to show the ad
@@ -115,8 +178,7 @@ mod test {
115
178
. try_get ( "adView.secondsSinceShow" )
116
179
. expect ( "Should get the ad_view.seconds_since_show field" ) ;
117
180
118
- let expected_number =
119
- serde_json:: from_str :: < serde_json:: Number > ( "10" ) . expect ( "Should create number" ) ;
181
+ let expected_number = serde_json:: Number :: from ( 10 ) ;
120
182
121
183
assert_eq ! ( Value :: Number ( expected_number) , ad_view_seconds_since_show) ;
122
184
0 commit comments