1use serde::Deserialize;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum SubscriptionPeriodUnit {
6 Day,
8 Week,
10 Month,
12 Year,
14 Unknown(String),
16}
17
18impl SubscriptionPeriodUnit {
19 pub fn as_str(&self) -> &str {
21 match self {
22 Self::Day => "day",
23 Self::Week => "week",
24 Self::Month => "month",
25 Self::Year => "year",
26 Self::Unknown(value) => value.as_str(),
27 }
28 }
29
30 pub(crate) fn from_raw(raw: String) -> Self {
31 match raw.as_str() {
32 "day" => Self::Day,
33 "week" => Self::Week,
34 "month" => Self::Month,
35 "year" => Self::Year,
36 _ => Self::Unknown(raw),
37 }
38 }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct SubscriptionPeriod {
44 pub unit: SubscriptionPeriodUnit,
46 pub value: i64,
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub enum SubscriptionOfferType {
53 Introductory,
55 Promotional,
57 WinBack,
59 Unknown(String),
61}
62
63impl SubscriptionOfferType {
64 pub fn as_str(&self) -> &str {
66 match self {
67 Self::Introductory => "introductory",
68 Self::Promotional => "promotional",
69 Self::WinBack => "winBack",
70 Self::Unknown(value) => value.as_str(),
71 }
72 }
73
74 pub(crate) fn from_raw(raw: String) -> Self {
75 match raw.as_str() {
76 "introductory" => Self::Introductory,
77 "promotional" => Self::Promotional,
78 "winBack" => Self::WinBack,
79 _ => Self::Unknown(raw),
80 }
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub enum SubscriptionPaymentMode {
87 PayAsYouGo,
89 PayUpFront,
91 FreeTrial,
93 Unknown(String),
95}
96
97impl SubscriptionPaymentMode {
98 pub fn as_str(&self) -> &str {
100 match self {
101 Self::PayAsYouGo => "payAsYouGo",
102 Self::PayUpFront => "payUpFront",
103 Self::FreeTrial => "freeTrial",
104 Self::Unknown(value) => value.as_str(),
105 }
106 }
107
108 pub(crate) fn from_raw(raw: String) -> Self {
109 match raw.as_str() {
110 "payAsYouGo" => Self::PayAsYouGo,
111 "payUpFront" => Self::PayUpFront,
112 "freeTrial" => Self::FreeTrial,
113 _ => Self::Unknown(raw),
114 }
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Eq)]
119pub struct SubscriptionOffer {
121 pub id: Option<String>,
123 pub offer_type: SubscriptionOfferType,
125 pub price: String,
127 pub display_price: String,
129 pub period: SubscriptionPeriod,
131 pub period_count: i64,
133 pub payment_mode: SubscriptionPaymentMode,
135}
136
137#[derive(Debug, Deserialize)]
138pub(crate) struct SubscriptionPeriodPayload {
139 unit: String,
140 value: i64,
141}
142
143impl SubscriptionPeriodPayload {
144 pub(crate) fn into_subscription_period(self) -> SubscriptionPeriod {
145 SubscriptionPeriod {
146 unit: SubscriptionPeriodUnit::from_raw(self.unit),
147 value: self.value,
148 }
149 }
150}
151
152#[derive(Debug, Deserialize)]
153pub(crate) struct SubscriptionOfferPayload {
154 id: Option<String>,
155 #[serde(rename = "type")]
156 offer_type: String,
157 price: String,
158 #[serde(rename = "displayPrice")]
159 display_price: String,
160 period: SubscriptionPeriodPayload,
161 #[serde(rename = "periodCount")]
162 period_count: i64,
163 #[serde(rename = "paymentMode")]
164 payment_mode: String,
165}
166
167impl SubscriptionOfferPayload {
168 pub(crate) fn into_subscription_offer(self) -> SubscriptionOffer {
169 SubscriptionOffer {
170 id: self.id,
171 offer_type: SubscriptionOfferType::from_raw(self.offer_type),
172 price: self.price,
173 display_price: self.display_price,
174 period: self.period.into_subscription_period(),
175 period_count: self.period_count,
176 payment_mode: SubscriptionPaymentMode::from_raw(self.payment_mode),
177 }
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::*;
184
185 #[test]
186 fn subscription_period_units_round_trip_known_values() {
187 let cases = [
188 ("day", SubscriptionPeriodUnit::Day),
189 ("week", SubscriptionPeriodUnit::Week),
190 ("month", SubscriptionPeriodUnit::Month),
191 ("year", SubscriptionPeriodUnit::Year),
192 ];
193
194 for (raw, unit) in cases {
195 assert_eq!(SubscriptionPeriodUnit::from_raw(raw.to_owned()), unit);
196 assert_eq!(unit.as_str(), raw);
197 }
198 }
199
200 #[test]
201 fn unknown_subscription_period_unit_is_preserved() {
202 let unit = SubscriptionPeriodUnit::from_raw("fortnight".to_owned());
203
204 assert_eq!(unit, SubscriptionPeriodUnit::Unknown("fortnight".into()));
205 assert_eq!(unit.as_str(), "fortnight");
206 }
207
208 #[test]
209 fn subscription_offer_types_round_trip_known_values() {
210 let cases = [
211 ("introductory", SubscriptionOfferType::Introductory),
212 ("promotional", SubscriptionOfferType::Promotional),
213 ("winBack", SubscriptionOfferType::WinBack),
214 ];
215
216 for (raw, offer_type) in cases {
217 assert_eq!(SubscriptionOfferType::from_raw(raw.to_owned()), offer_type);
218 assert_eq!(offer_type.as_str(), raw);
219 }
220 }
221
222 #[test]
223 fn subscription_payment_modes_round_trip_known_values() {
224 let cases = [
225 ("payAsYouGo", SubscriptionPaymentMode::PayAsYouGo),
226 ("payUpFront", SubscriptionPaymentMode::PayUpFront),
227 ("freeTrial", SubscriptionPaymentMode::FreeTrial),
228 ];
229
230 for (raw, payment_mode) in cases {
231 assert_eq!(
232 SubscriptionPaymentMode::from_raw(raw.to_owned()),
233 payment_mode
234 );
235 assert_eq!(payment_mode.as_str(), raw);
236 }
237 }
238
239 #[test]
240 fn subscription_period_payload_converts_to_subscription_period() {
241 let period = SubscriptionPeriodPayload {
242 unit: "month".into(),
243 value: 3,
244 }
245 .into_subscription_period();
246
247 assert_eq!(
248 period,
249 SubscriptionPeriod {
250 unit: SubscriptionPeriodUnit::Month,
251 value: 3,
252 }
253 );
254 }
255
256 #[test]
257 fn subscription_offer_payload_converts_to_subscription_offer() {
258 let offer = SubscriptionOfferPayload {
259 id: Some("intro".into()),
260 offer_type: "introductory".into(),
261 price: "4.99".into(),
262 display_price: "$4.99".into(),
263 period: SubscriptionPeriodPayload {
264 unit: "week".into(),
265 value: 1,
266 },
267 period_count: 2,
268 payment_mode: "freeTrial".into(),
269 }
270 .into_subscription_offer();
271
272 assert_eq!(offer.id.as_deref(), Some("intro"));
273 assert_eq!(offer.offer_type, SubscriptionOfferType::Introductory);
274 assert_eq!(offer.display_price, "$4.99");
275 assert_eq!(
276 offer.period,
277 SubscriptionPeriod {
278 unit: SubscriptionPeriodUnit::Week,
279 value: 1,
280 }
281 );
282 assert_eq!(offer.period_count, 2);
283 assert_eq!(offer.payment_mode, SubscriptionPaymentMode::FreeTrial);
284 }
285}