Skip to main content

stripe_shared/
source_order_item.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 SourceOrderItem {
6    /// The amount (price) for this order item.
7    pub amount: Option<i64>,
8    /// This currency of this order item. Required when `amount` is present.
9    pub currency: Option<stripe_types::Currency>,
10    /// Human-readable description for this order item.
11    pub description: Option<String>,
12    /// The ID of the associated object for this line item.
13    /// Expandable if not null (e.g., expandable to a SKU).
14    pub parent: Option<String>,
15    /// The quantity of this order item.
16    /// When type is `sku`, this is the number of instances of the SKU to be ordered.
17    pub quantity: Option<u64>,
18    /// The type of this order item. Must be `sku`, `tax`, or `shipping`.
19    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
20    pub type_: Option<String>,
21}
22#[cfg(feature = "redact-generated-debug")]
23impl std::fmt::Debug for SourceOrderItem {
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        f.debug_struct("SourceOrderItem").finish_non_exhaustive()
26    }
27}
28#[doc(hidden)]
29pub struct SourceOrderItemBuilder {
30    amount: Option<Option<i64>>,
31    currency: Option<Option<stripe_types::Currency>>,
32    description: Option<Option<String>>,
33    parent: Option<Option<String>>,
34    quantity: Option<Option<u64>>,
35    type_: Option<Option<String>>,
36}
37
38#[allow(
39    unused_variables,
40    irrefutable_let_patterns,
41    clippy::let_unit_value,
42    clippy::match_single_binding,
43    clippy::single_match
44)]
45const _: () = {
46    use miniserde::de::{Map, Visitor};
47    use miniserde::json::Value;
48    use miniserde::{Deserialize, Result, make_place};
49    use stripe_types::miniserde_helpers::FromValueOpt;
50    use stripe_types::{MapBuilder, ObjectDeser};
51
52    make_place!(Place);
53
54    impl Deserialize for SourceOrderItem {
55        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
56            Place::new(out)
57        }
58    }
59
60    struct Builder<'a> {
61        out: &'a mut Option<SourceOrderItem>,
62        builder: SourceOrderItemBuilder,
63    }
64
65    impl Visitor for Place<SourceOrderItem> {
66        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
67            Ok(Box::new(Builder {
68                out: &mut self.out,
69                builder: SourceOrderItemBuilder::deser_default(),
70            }))
71        }
72    }
73
74    impl MapBuilder for SourceOrderItemBuilder {
75        type Out = SourceOrderItem;
76        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
77            Ok(match k {
78                "amount" => Deserialize::begin(&mut self.amount),
79                "currency" => Deserialize::begin(&mut self.currency),
80                "description" => Deserialize::begin(&mut self.description),
81                "parent" => Deserialize::begin(&mut self.parent),
82                "quantity" => Deserialize::begin(&mut self.quantity),
83                "type" => Deserialize::begin(&mut self.type_),
84                _ => <dyn Visitor>::ignore(),
85            })
86        }
87
88        fn deser_default() -> Self {
89            Self {
90                amount: Deserialize::default(),
91                currency: Deserialize::default(),
92                description: Deserialize::default(),
93                parent: Deserialize::default(),
94                quantity: Deserialize::default(),
95                type_: Deserialize::default(),
96            }
97        }
98
99        fn take_out(&mut self) -> Option<Self::Out> {
100            let (
101                Some(amount),
102                Some(currency),
103                Some(description),
104                Some(parent),
105                Some(quantity),
106                Some(type_),
107            ) = (
108                self.amount,
109                self.currency.take(),
110                self.description.take(),
111                self.parent.take(),
112                self.quantity,
113                self.type_.take(),
114            )
115            else {
116                return None;
117            };
118            Some(Self::Out { amount, currency, description, parent, quantity, type_ })
119        }
120    }
121
122    impl Map for Builder<'_> {
123        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
124            self.builder.key(k)
125        }
126
127        fn finish(&mut self) -> Result<()> {
128            *self.out = self.builder.take_out();
129            Ok(())
130        }
131    }
132
133    impl ObjectDeser for SourceOrderItem {
134        type Builder = SourceOrderItemBuilder;
135    }
136
137    impl FromValueOpt for SourceOrderItem {
138        fn from_value(v: Value) -> Option<Self> {
139            let Value::Object(obj) = v else {
140                return None;
141            };
142            let mut b = SourceOrderItemBuilder::deser_default();
143            for (k, v) in obj {
144                match k.as_str() {
145                    "amount" => b.amount = FromValueOpt::from_value(v),
146                    "currency" => b.currency = FromValueOpt::from_value(v),
147                    "description" => b.description = FromValueOpt::from_value(v),
148                    "parent" => b.parent = FromValueOpt::from_value(v),
149                    "quantity" => b.quantity = FromValueOpt::from_value(v),
150                    "type" => b.type_ = FromValueOpt::from_value(v),
151                    _ => {}
152                }
153            }
154            b.take_out()
155        }
156    }
157};