Skip to main content

reydenx/model/
order.rs

1use std::fmt;
2
3use serde::{
4    ser::{Error, SerializeStruct},
5    Deserialize, Serialize,
6};
7
8use super::error::ValueError;
9
10#[derive(Deserialize, Debug)]
11pub enum LaunchMode {
12    Auto,
13    Manual,
14    Delay,
15}
16
17impl Serialize for LaunchMode {
18    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19    where
20        S: serde::Serializer,
21    {
22        match &self {
23            LaunchMode::Auto => serializer.serialize_str("auto"),
24            LaunchMode::Manual => serializer.serialize_str("manual"),
25            LaunchMode::Delay => serializer.serialize_str("delay"),
26        }
27    }
28}
29
30impl fmt::Display for LaunchMode {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match &self {
33            LaunchMode::Auto => write!(f, "auto"),
34            LaunchMode::Manual => write!(f, "manual"),
35            LaunchMode::Delay => write!(f, "delay"),
36        }
37    }
38}
39
40#[derive(Serialize, Deserialize, Debug)]
41pub struct Parameters {
42    pub launch_mode: String,
43    pub work_mode: String,
44    pub delay: bool,
45    pub delay_time: u32,
46    pub even_distribution: bool,
47    pub even_distribution_time: u32,
48}
49
50#[derive(Serialize, Deserialize, Debug)]
51pub struct AvgValues {
52    pub in_settings: f64,
53    pub in_fact: f64,
54}
55
56#[derive(Serialize, Deserialize, Debug)]
57pub struct Average {
58    pub online: AvgValues,
59    pub session_in_seconds: f64,
60}
61
62#[derive(Serialize, Deserialize, Debug)]
63pub struct Statistics {
64    pub active_time_in_seconds: u32,
65    pub views: u32,
66    pub clicks: u32,
67    pub ctr: f64,
68    pub average: Average,
69}
70
71#[derive(Serialize, Deserialize, Debug)]
72pub struct Order {
73    pub id: u32,
74    pub created_at: String,
75    pub updated_at: String,
76    pub uuid: String,
77    pub status: String,
78    pub ordered_view_qty: u32,
79    pub price_per_view: f64,
80    pub is_autostart: bool,
81    pub online_users_limit: u32,
82    pub platform: String,
83    pub content_type: String,
84    pub parameters: Parameters,
85    pub statistics: Option<Statistics>,
86    pub content_classification_labels: Option<Vec<String>>,
87}
88
89#[derive(Serialize, Deserialize, Debug)]
90pub struct OnlineStats {
91    pub created_at: String,
92    pub in_settings: f64,
93    pub in_fact: f64,
94}
95
96#[derive(Serialize, Deserialize, Debug)]
97pub struct DateAndQuantity {
98    pub date: String,
99    pub quantity: i32,
100}
101
102#[derive(Serialize, Deserialize, Debug)]
103pub struct IdAndQuantity {
104    pub id: u32,
105    pub quantity: i32,
106}
107
108#[derive(Serialize, Deserialize, Debug)]
109pub struct SiteStats {
110    pub domain: String,
111    pub views: i32,
112    pub clicks: u32,
113    pub ctr: f64,
114}
115
116#[derive(Serialize, Deserialize, Debug)]
117pub struct Payment {
118    pub id: u32,
119    pub created_at: String,
120    pub updated_at: String,
121    pub payed_at: String,
122    pub amount: u32,
123    pub external_id: String,
124    pub uuid: String,
125    pub receipt: String,
126}
127
128#[derive(Serialize, Deserialize, Debug)]
129pub struct SmoothGain {
130    pub enabled: bool,
131    pub minutes: u32,
132}
133
134#[derive(Serialize, Deserialize, Debug)]
135pub struct TwitchPayload {
136    pub price_id: u32,
137    pub number_of_views: u32,
138    pub number_of_viewers: u32,
139    pub launch_mode: String,
140    pub smooth_gain: SmoothGain,
141    pub delay_time: u32,
142    pub twitch_id: u32,
143    pub fixed_allocation: u32,
144    pub on_overflow: bool,
145}
146
147#[derive(Serialize, Deserialize, Debug)]
148pub struct YouTubePayload {
149    pub price_id: u32,
150    pub number_of_views: u32,
151    pub number_of_viewers: u32,
152    pub launch_mode: String,
153    pub smooth_gain: SmoothGain,
154    pub delay_time: u32,
155    pub channel_url: String,
156    pub fixed_allocation: u32,
157    pub on_overflow: bool,
158}
159
160#[derive(Serialize, Deserialize, Debug)]
161pub struct KickPayload {
162    pub price_id: u32,
163    pub number_of_views: u32,
164    pub number_of_viewers: u32,
165    pub launch_mode: String,
166    pub smooth_gain: SmoothGain,
167    pub delay_time: u32,
168    pub channel_url: String,
169    pub fixed_allocation: u32,
170    pub on_overflow: bool,
171}
172
173#[derive(Serialize, Deserialize, Debug)]
174pub struct Identifiers {
175    pub identifiers: Vec<u32>,
176}
177
178#[derive(Deserialize, Debug)]
179pub struct LaunchParams {
180    pub mode: LaunchMode,
181    pub delay_time: u8,
182}
183
184impl Serialize for LaunchParams {
185    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
186    where
187        S: serde::Serializer,
188    {
189        let mut s = serializer.serialize_struct("LaunchParams", 2)?;
190        match self.mode {
191            LaunchMode::Delay => {
192                if self.delay_time < 5 || self.delay_time > 240 {
193                    return Err(ValueError {
194                        message: String::from(
195                            "The number of minutes for delayed start should be from 5 to 240",
196                        ),
197                    })
198                    .map_err(S::Error::custom);
199                }
200                s.serialize_field("delay_time", &self.delay_time)?;
201            }
202            _ => {
203                s.serialize_field("delay_time", &0)?;
204            }
205        }
206        s.serialize_field("mode", &self.mode)?;
207        s.end()
208    }
209}