rust-woocommerce 0.1.3

Framework for woocommerce
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use serde_with::skip_serializing_none;

use crate::{DiscountType, MetaData};
#[skip_serializing_none]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct CreateCoupon {
    code: String,
    discount_type: DiscountType,
    amount: String,
    description: Option<String>,
    date_expires: Option<String>,
    date_expires_gmt: Option<String>,
    individual_use: bool,
    product_ids: Option<Vec<i32>>,
    excluded_product_ids: Option<Vec<i32>>,
    usage_limit: Option<i32>,
    usage_limit_per_user: Option<i32>,
    limit_usage_to_x_items: Option<i32>,
    free_shipping: bool,
    product_categories: Option<Vec<i32>>,
    excluded_product_categories: Option<Vec<i32>>,
    exclude_sale_items: bool,
    minimum_amount: Option<String>,
    maximum_amount: Option<String>,
    email_restrictions: Option<Vec<String>>,
    meta_data: Option<Vec<MetaData>>,
}
impl CreateCoupon {
    pub fn new(
        code: impl Into<String>,
        discount_type: DiscountType,
        amount: impl Into<String>,
    ) -> Self {
        Self {
            code: code.into(),
            discount_type,
            amount: amount.into(),
            ..Default::default()
        }
    }
    pub fn builder() -> CreateCouponBuilder<NoCode, NoDiscountType, NoAmount> {
        CreateCouponBuilder::default()
    }
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct CreateCouponBuilder<C, D, A> {
    code: C,
    discount_type: D,
    amount: A,
    description: Option<String>,
    date_expires: Option<String>,
    date_expires_gmt: Option<String>,
    individual_use: bool,
    product_ids: Option<Vec<i32>>,
    excluded_product_ids: Option<Vec<i32>>,
    usage_limit: Option<i32>,
    usage_limit_per_user: Option<i32>,
    limit_usage_to_x_items: Option<i32>,
    free_shipping: bool,
    product_categories: Option<Vec<i32>>,
    excluded_product_categories: Option<Vec<i32>>,
    exclude_sale_items: bool,
    minimum_amount: Option<String>,
    maximum_amount: Option<String>,
    email_restrictions: Option<Vec<String>>,
    meta_data: Option<Vec<MetaData>>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct NoCode;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct NoDiscountType;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct NoAmount;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct WithCode(String);
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct WithDiscountType(DiscountType);
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct WithAmount(String);
impl<C, D, A> CreateCouponBuilder<C, D, A> {
    /// Coupon code.
    pub fn code(self, code: impl Into<String>) -> CreateCouponBuilder<WithCode, D, A> {
        CreateCouponBuilder {
            code: WithCode(code.into()),
            discount_type: self.discount_type,
            amount: self.amount,
            description: self.description,
            date_expires: self.date_expires,
            date_expires_gmt: self.date_expires_gmt,
            individual_use: self.individual_use,
            product_ids: self.product_ids,
            excluded_product_ids: self.excluded_product_ids,
            usage_limit: self.usage_limit,
            usage_limit_per_user: self.usage_limit_per_user,
            limit_usage_to_x_items: self.limit_usage_to_x_items,
            free_shipping: self.free_shipping,
            product_categories: self.product_categories,
            excluded_product_categories: self.excluded_product_categories,
            exclude_sale_items: self.exclude_sale_items,
            minimum_amount: self.minimum_amount,
            maximum_amount: self.maximum_amount,
            email_restrictions: self.email_restrictions,
            meta_data: self.meta_data,
        }
    }
    /// Determines the type of discount that will be applied.
    pub fn discount_type(
        self,
        discount_type: DiscountType,
    ) -> CreateCouponBuilder<C, WithDiscountType, A> {
        CreateCouponBuilder {
            code: self.code,
            discount_type: WithDiscountType(discount_type),
            amount: self.amount,
            description: self.description,
            date_expires: self.date_expires,
            date_expires_gmt: self.date_expires_gmt,
            individual_use: self.individual_use,
            product_ids: self.product_ids,
            excluded_product_ids: self.excluded_product_ids,
            usage_limit: self.usage_limit,
            usage_limit_per_user: self.usage_limit_per_user,
            limit_usage_to_x_items: self.limit_usage_to_x_items,
            free_shipping: self.free_shipping,
            product_categories: self.product_categories,
            excluded_product_categories: self.excluded_product_categories,
            exclude_sale_items: self.exclude_sale_items,
            minimum_amount: self.minimum_amount,
            maximum_amount: self.maximum_amount,
            email_restrictions: self.email_restrictions,
            meta_data: self.meta_data,
        }
    }
    /// The amount of discount. Should always be numeric, even if setting a percentage.
    pub fn amount(self, amount: impl Into<String>) -> CreateCouponBuilder<C, D, WithAmount> {
        CreateCouponBuilder {
            code: self.code,
            discount_type: self.discount_type,
            amount: WithAmount(amount.into()),
            description: self.description,
            date_expires: self.date_expires,
            date_expires_gmt: self.date_expires_gmt,
            individual_use: self.individual_use,
            product_ids: self.product_ids,
            excluded_product_ids: self.excluded_product_ids,
            usage_limit: self.usage_limit,
            usage_limit_per_user: self.usage_limit_per_user,
            limit_usage_to_x_items: self.limit_usage_to_x_items,
            free_shipping: self.free_shipping,
            product_categories: self.product_categories,
            excluded_product_categories: self.excluded_product_categories,
            exclude_sale_items: self.exclude_sale_items,
            minimum_amount: self.minimum_amount,
            maximum_amount: self.maximum_amount,
            email_restrictions: self.email_restrictions,
            meta_data: self.meta_data,
        }
    }
    /// Coupon description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// The date the coupon expires, in the site's timezone. Format: YEAR-MONTH-DAY. Example: "2024-01-31"
    pub fn date_expires(mut self, date_expires: impl Into<String>) -> Self {
        let _ = self.date_expires.insert(date_expires.into());
        self
    }
    /// The date the coupon expires, as GMT.Format: YEAR-MONTH-DAY. Example: "2024-01-31"
    pub fn date_expires_gmt(mut self, date_expires_gmt: impl Into<String>) -> Self {
        let _ = self.date_expires_gmt.insert(date_expires_gmt.into());
        self
    }
    /// If true, the coupon can only be used individually. Other applied coupons will be removed from the cart. Default is false.
    pub fn individual_use(mut self) -> Self {
        self.individual_use = true;
        self
    }
    /// List of product IDs the coupon can be used on.
    pub fn product_id(mut self, product_id: i32) -> Self {
        self.product_ids.get_or_insert(vec![]).push(product_id);
        self
    }
    /// List of product IDs the coupon cannot be used on.
    pub fn excluded_product_id(mut self, excluded_product_id: i32) -> Self {
        self.excluded_product_ids
            .get_or_insert(vec![])
            .push(excluded_product_id);
        self
    }
    /// How many times the coupon can be used in total.
    pub fn usage_limit(mut self, usage_limit: i32) -> Self {
        let _ = self.usage_limit.insert(usage_limit);
        self
    }
    /// How many times the coupon can be used per customer.
    pub fn usage_limit_per_user(mut self, usage_limit_per_user: i32) -> Self {
        let _ = self.usage_limit_per_user.insert(usage_limit_per_user);
        self
    }
    /// Max number of items in the cart the coupon can be applied to.
    pub fn limit_usage_to_x_items(mut self, limit_usage_to_x_items: i32) -> Self {
        let _ = self.limit_usage_to_x_items.insert(limit_usage_to_x_items);
        self
    }
    /// If true and if the free shipping method requires a coupon, this coupon will enable free shipping. Default is false.
    pub fn free_shipping(mut self) -> Self {
        self.free_shipping = true;
        self
    }
    /// List of category IDs the coupon applies to.
    pub fn product_category(mut self, category_id: i32) -> Self {
        self.product_categories
            .get_or_insert(vec![])
            .push(category_id);
        self
    }
    /// List of category IDs the coupon does not apply to.
    pub fn excluded_product_category(mut self, exclude_product_category_id: i32) -> Self {
        self.excluded_product_categories
            .get_or_insert(vec![])
            .push(exclude_product_category_id);
        self
    }
    /// If true, this coupon will not be applied to items that have sale prices. Default is false.
    pub fn exclude_sale_items(mut self) -> Self {
        self.exclude_sale_items = true;
        self
    }
    /// Minimum order amount that needs to be in the cart before coupon applies.
    pub fn minimum_amount(mut self, minimum_amount: impl Into<String>) -> Self {
        let _ = self.minimum_amount.insert(minimum_amount.into());
        self
    }
    /// Maximum order amount allowed when using the coupon.
    pub fn maximum_amount(mut self, maximum_amount: impl Into<String>) -> Self {
        let _ = self.maximum_amount.insert(maximum_amount.into());
        self
    }
    /// List of email addresses that can use this coupon.
    pub fn email_restriction(mut self, email: impl Into<String>) -> Self {
        self.email_restrictions
            .get_or_insert(vec![])
            .push(email.into());
        self
    }
    /// Meta data.
    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
}
impl CreateCouponBuilder<WithCode, WithDiscountType, WithAmount> {
    pub fn build(self) -> CreateCoupon {
        CreateCoupon {
            code: self.code.0,
            discount_type: self.discount_type.0,
            amount: self.amount.0,
            description: self.description,
            date_expires: self.date_expires,
            date_expires_gmt: self.date_expires_gmt,
            individual_use: self.individual_use,
            product_ids: self.product_ids,
            excluded_product_ids: self.excluded_product_ids,
            usage_limit: self.usage_limit,
            usage_limit_per_user: self.usage_limit_per_user,
            limit_usage_to_x_items: self.limit_usage_to_x_items,
            free_shipping: self.free_shipping,
            product_categories: self.product_categories,
            excluded_product_categories: self.excluded_product_categories,
            exclude_sale_items: self.exclude_sale_items,
            minimum_amount: self.minimum_amount,
            maximum_amount: self.maximum_amount,
            email_restrictions: self.email_restrictions,
            meta_data: self.meta_data,
        }
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct UpdateCoupon {
    id: Option<i32>,
    code: Option<String>,
    discount_type: Option<DiscountType>,
    amount: Option<String>,
    description: Option<String>,
    date_expires: Option<String>,
    date_expires_gmt: Option<String>,
    individual_use: Option<bool>,
    product_ids: Option<Vec<i32>>,
    excluded_product_ids: Option<Vec<i32>>,
    usage_limit: Option<i32>,
    usage_limit_per_user: Option<i32>,
    limit_usage_to_x_items: Option<i32>,
    free_shipping: Option<bool>,
    product_categories: Option<Vec<i32>>,
    excluded_product_categories: Option<Vec<i32>>,
    exclude_sale_items: Option<bool>,
    minimum_amount: Option<String>,
    maximum_amount: Option<String>,
    email_restrictions: Option<Vec<String>>,
    meta_data: Option<Vec<MetaData>>,
}
impl UpdateCoupon {
    pub fn builder() -> UpdateCouponBuilder {
        UpdateCouponBuilder::default()
    }
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct UpdateCouponBuilder {
    id: Option<i32>,
    code: Option<String>,
    discount_type: Option<DiscountType>,
    amount: Option<String>,
    description: Option<String>,
    date_expires: Option<String>,
    date_expires_gmt: Option<String>,
    individual_use: Option<bool>,
    product_ids: Option<Vec<i32>>,
    excluded_product_ids: Option<Vec<i32>>,
    usage_limit: Option<i32>,
    usage_limit_per_user: Option<i32>,
    limit_usage_to_x_items: Option<i32>,
    free_shipping: Option<bool>,
    product_categories: Option<Vec<i32>>,
    excluded_product_categories: Option<Vec<i32>>,
    exclude_sale_items: Option<bool>,
    minimum_amount: Option<String>,
    maximum_amount: Option<String>,
    email_restrictions: Option<Vec<String>>,
    meta_data: Option<Vec<MetaData>>,
}
impl UpdateCouponBuilder {
    /// Unique identifier for the object.
    pub fn id(&mut self, id: i32) -> &mut Self {
        let _ = self.id.insert(id);
        self
    }
    /// Coupon code.
    pub fn code(&mut self, code: impl Into<String>) -> &mut Self {
        let _ = self.code.insert(code.into());
        self
    }
    /// Determines the type of discount that will be applied.
    pub fn discount_type(&mut self, discount_type: DiscountType) -> &mut Self {
        let _ = self.discount_type.insert(discount_type);
        self
    }
    /// The amount of discount. Should always be numeric, even if setting a percentage.
    pub fn amount(&mut self, amount: impl Into<String>) -> &mut Self {
        let _ = self.amount.insert(amount.into());
        self
    }
    /// Coupon description.
    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// The date the coupon expires, in the site's timezone. Format: YEAR-MONTH-DAY. Example: "2024-01-31"
    pub fn date_expires(&mut self, date_expires: impl Into<String>) -> &mut Self {
        let _ = self.date_expires.insert(date_expires.into());
        self
    }
    /// The date the coupon expires, as GMT.Format: YEAR-MONTH-DAY. Example: "2024-01-31"
    pub fn date_expires_gmt(&mut self, date_expires_gmt: impl Into<String>) -> &mut Self {
        let _ = self.date_expires_gmt.insert(date_expires_gmt.into());
        self
    }
    /// If true, the coupon can only be used individually. Other applied coupons will be removed from the cart. Default is false.
    pub fn individual_use(&mut self, individual_use: bool) -> &mut Self {
        let _ = self.individual_use.insert(individual_use);
        self
    }
    /// List of product IDs the coupon can be used on.
    pub fn product_id(&mut self, product_id: i32) -> &mut Self {
        self.product_ids.get_or_insert(vec![]).push(product_id);
        self
    }
    /// List of product IDs the coupon cannot be used on.
    pub fn excluded_product_id(&mut self, excluded_product_id: i32) -> &mut Self {
        self.excluded_product_ids
            .get_or_insert(vec![])
            .push(excluded_product_id);
        self
    }
    /// How many times the coupon can be used in total.
    pub fn usage_limit(&mut self, usage_limit: i32) -> &mut Self {
        let _ = self.usage_limit.insert(usage_limit);
        self
    }
    /// How many times the coupon can be used per customer.
    pub fn usage_limit_per_user(&mut self, usage_limit_per_user: i32) -> &mut Self {
        let _ = self.usage_limit_per_user.insert(usage_limit_per_user);
        self
    }
    /// Max number of items in the cart the coupon can be applied to.
    pub fn limit_usage_to_x_items(&mut self, limit_usage_to_x_items: i32) -> &mut Self {
        let _ = self.limit_usage_to_x_items.insert(limit_usage_to_x_items);
        self
    }
    /// If true and if the free shipping method requires a coupon, this coupon will enable free shipping. Default is false.
    pub fn free_shipping(&mut self, free_shipping: bool) -> &mut Self {
        let _ = self.free_shipping.insert(free_shipping);
        self
    }
    /// List of category IDs the coupon applies to.
    pub fn product_category(&mut self, category_id: i32) -> &mut Self {
        self.product_categories
            .get_or_insert(vec![])
            .push(category_id);
        self
    }
    /// List of category IDs the coupon does not apply to.
    pub fn excluded_product_category(&mut self, exclude_product_category_id: i32) -> &mut Self {
        self.excluded_product_categories
            .get_or_insert(vec![])
            .push(exclude_product_category_id);
        self
    }
    /// If true, this coupon will not be applied to items that have sale prices. Default is false.
    pub fn exclude_sale_items(&mut self, exclude_sale_items: bool) -> &mut Self {
        let _ = self.exclude_sale_items.insert(exclude_sale_items);
        self
    }
    /// Minimum order amount that needs to be in the cart before coupon applies.
    pub fn minimum_amount(&mut self, minimum_amount: impl Into<String>) -> &mut Self {
        let _ = self.minimum_amount.insert(minimum_amount.into());
        self
    }
    /// Maximum order amount allowed when using the coupon.
    pub fn maximum_amount(&mut self, maximum_amount: impl Into<String>) -> &mut Self {
        let _ = self.maximum_amount.insert(maximum_amount.into());
        self
    }
    /// List of email addresses that can use this coupon.
    pub fn email_restriction(&mut self, email: impl Into<String>) -> &mut Self {
        self.email_restrictions
            .get_or_insert(vec![])
            .push(email.into());
        self
    }
    /// Meta data.
    pub fn meta_data(&mut self, key: impl Into<String>, value: impl serde::Serialize) -> &mut Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
    pub fn build(&self) -> UpdateCoupon {
        UpdateCoupon {
            id: self.id,
            code: self.code.clone(),
            discount_type: self.discount_type.clone(),
            amount: self.amount.clone(),
            description: self.description.clone(),
            date_expires: self.date_expires.clone(),
            date_expires_gmt: self.date_expires_gmt.clone(),
            individual_use: self.individual_use,
            product_ids: self.product_ids.clone(),
            excluded_product_ids: self.excluded_product_ids.clone(),
            usage_limit: self.usage_limit,
            usage_limit_per_user: self.usage_limit_per_user,
            limit_usage_to_x_items: self.limit_usage_to_x_items,
            free_shipping: self.free_shipping,
            product_categories: self.product_categories.clone(),
            excluded_product_categories: self.excluded_product_categories.clone(),
            exclude_sale_items: self.exclude_sale_items,
            minimum_amount: self.minimum_amount.clone(),
            maximum_amount: self.maximum_amount.clone(),
            email_restrictions: self.email_restrictions.clone(),
            meta_data: self.meta_data.clone(),
        }
    }
}