Skip to main content

stripe_shared/
source_order.rs

1#[derive(Clone, Eq, PartialEq)]
2#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
3#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct SourceOrder {
6    /// A positive integer in the smallest currency unit (that is, 100 cents for $1.00, or 1 for ¥1, Japanese Yen being a zero-decimal currency) representing the total amount for the order.
7    pub amount: i64,
8    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
9    /// Must be a [supported currency](https://stripe.com/docs/currencies).
10    pub currency: stripe_types::Currency,
11    /// The email address of the customer placing the order.
12    pub email: Option<String>,
13    /// List of items constituting the order.
14    pub items: Option<Vec<stripe_shared::SourceOrderItem>>,
15    pub shipping: Option<stripe_shared::Shipping>,
16}
17#[cfg(feature = "redact-generated-debug")]
18impl std::fmt::Debug for SourceOrder {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        f.debug_struct("SourceOrder").finish_non_exhaustive()
21    }
22}
23#[doc(hidden)]
24pub struct SourceOrderBuilder {
25    amount: Option<i64>,
26    currency: Option<stripe_types::Currency>,
27    email: Option<Option<String>>,
28    items: Option<Option<Vec<stripe_shared::SourceOrderItem>>>,
29    shipping: Option<Option<stripe_shared::Shipping>>,
30}
31
32#[allow(
33    unused_variables,
34    irrefutable_let_patterns,
35    clippy::let_unit_value,
36    clippy::match_single_binding,
37    clippy::single_match
38)]
39const _: () = {
40    use miniserde::de::{Map, Visitor};
41    use miniserde::json::Value;
42    use miniserde::{Deserialize, Result, make_place};
43    use stripe_types::miniserde_helpers::FromValueOpt;
44    use stripe_types::{MapBuilder, ObjectDeser};
45
46    make_place!(Place);
47
48    impl Deserialize for SourceOrder {
49        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
50            Place::new(out)
51        }
52    }
53
54    struct Builder<'a> {
55        out: &'a mut Option<SourceOrder>,
56        builder: SourceOrderBuilder,
57    }
58
59    impl Visitor for Place<SourceOrder> {
60        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
61            Ok(Box::new(Builder {
62                out: &mut self.out,
63                builder: SourceOrderBuilder::deser_default(),
64            }))
65        }
66    }
67
68    impl MapBuilder for SourceOrderBuilder {
69        type Out = SourceOrder;
70        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
71            Ok(match k {
72                "amount" => Deserialize::begin(&mut self.amount),
73                "currency" => Deserialize::begin(&mut self.currency),
74                "email" => Deserialize::begin(&mut self.email),
75                "items" => Deserialize::begin(&mut self.items),
76                "shipping" => Deserialize::begin(&mut self.shipping),
77                _ => <dyn Visitor>::ignore(),
78            })
79        }
80
81        fn deser_default() -> Self {
82            Self {
83                amount: Deserialize::default(),
84                currency: Deserialize::default(),
85                email: Deserialize::default(),
86                items: Deserialize::default(),
87                shipping: Deserialize::default(),
88            }
89        }
90
91        fn take_out(&mut self) -> Option<Self::Out> {
92            let (Some(amount), Some(currency), Some(email), Some(items), Some(shipping)) = (
93                self.amount,
94                self.currency.take(),
95                self.email.take(),
96                self.items.take(),
97                self.shipping.take(),
98            ) else {
99                return None;
100            };
101            Some(Self::Out { amount, currency, email, items, shipping })
102        }
103    }
104
105    impl Map for Builder<'_> {
106        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
107            self.builder.key(k)
108        }
109
110        fn finish(&mut self) -> Result<()> {
111            *self.out = self.builder.take_out();
112            Ok(())
113        }
114    }
115
116    impl ObjectDeser for SourceOrder {
117        type Builder = SourceOrderBuilder;
118    }
119
120    impl FromValueOpt for SourceOrder {
121        fn from_value(v: Value) -> Option<Self> {
122            let Value::Object(obj) = v else {
123                return None;
124            };
125            let mut b = SourceOrderBuilder::deser_default();
126            for (k, v) in obj {
127                match k.as_str() {
128                    "amount" => b.amount = FromValueOpt::from_value(v),
129                    "currency" => b.currency = FromValueOpt::from_value(v),
130                    "email" => b.email = FromValueOpt::from_value(v),
131                    "items" => b.items = FromValueOpt::from_value(v),
132                    "shipping" => b.shipping = FromValueOpt::from_value(v),
133                    _ => {}
134                }
135            }
136            b.take_out()
137        }
138    }
139};