stripe_shared/
checkout_session_item.rs

1/// A line item.
2#[derive(Clone, Debug)]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CheckoutSessionItem {
5    /// Total discount amount applied. If no discounts were applied, defaults to 0.
6    pub amount_discount: i64,
7    /// Total before any discounts or taxes are applied.
8    pub amount_subtotal: i64,
9    /// Total tax amount applied. If no tax was applied, defaults to 0.
10    pub amount_tax: i64,
11    /// Total after discounts and taxes.
12    pub amount_total: i64,
13    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
14    /// Must be a [supported currency](https://stripe.com/docs/currencies).
15    pub currency: stripe_types::Currency,
16    /// An arbitrary string attached to the object.
17    /// Often useful for displaying to users.
18    /// Defaults to product name.
19    pub description: Option<String>,
20    /// The discounts applied to the line item.
21    pub discounts: Option<Vec<stripe_shared::LineItemsDiscountAmount>>,
22    /// Unique identifier for the object.
23    pub id: stripe_shared::CheckoutSessionItemId,
24    /// The price used to generate the line item.
25    pub price: Option<stripe_shared::Price>,
26    /// The quantity of products being purchased.
27    pub quantity: Option<u64>,
28    /// The taxes applied to the line item.
29    pub taxes: Option<Vec<stripe_shared::LineItemsTaxAmount>>,
30}
31#[doc(hidden)]
32pub struct CheckoutSessionItemBuilder {
33    amount_discount: Option<i64>,
34    amount_subtotal: Option<i64>,
35    amount_tax: Option<i64>,
36    amount_total: Option<i64>,
37    currency: Option<stripe_types::Currency>,
38    description: Option<Option<String>>,
39    discounts: Option<Option<Vec<stripe_shared::LineItemsDiscountAmount>>>,
40    id: Option<stripe_shared::CheckoutSessionItemId>,
41    price: Option<Option<stripe_shared::Price>>,
42    quantity: Option<Option<u64>>,
43    taxes: Option<Option<Vec<stripe_shared::LineItemsTaxAmount>>>,
44}
45
46#[allow(
47    unused_variables,
48    irrefutable_let_patterns,
49    clippy::let_unit_value,
50    clippy::match_single_binding,
51    clippy::single_match
52)]
53const _: () = {
54    use miniserde::de::{Map, Visitor};
55    use miniserde::json::Value;
56    use miniserde::{Deserialize, Result, make_place};
57    use stripe_types::miniserde_helpers::FromValueOpt;
58    use stripe_types::{MapBuilder, ObjectDeser};
59
60    make_place!(Place);
61
62    impl Deserialize for CheckoutSessionItem {
63        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
64            Place::new(out)
65        }
66    }
67
68    struct Builder<'a> {
69        out: &'a mut Option<CheckoutSessionItem>,
70        builder: CheckoutSessionItemBuilder,
71    }
72
73    impl Visitor for Place<CheckoutSessionItem> {
74        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
75            Ok(Box::new(Builder {
76                out: &mut self.out,
77                builder: CheckoutSessionItemBuilder::deser_default(),
78            }))
79        }
80    }
81
82    impl MapBuilder for CheckoutSessionItemBuilder {
83        type Out = CheckoutSessionItem;
84        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
85            Ok(match k {
86                "amount_discount" => Deserialize::begin(&mut self.amount_discount),
87                "amount_subtotal" => Deserialize::begin(&mut self.amount_subtotal),
88                "amount_tax" => Deserialize::begin(&mut self.amount_tax),
89                "amount_total" => Deserialize::begin(&mut self.amount_total),
90                "currency" => Deserialize::begin(&mut self.currency),
91                "description" => Deserialize::begin(&mut self.description),
92                "discounts" => Deserialize::begin(&mut self.discounts),
93                "id" => Deserialize::begin(&mut self.id),
94                "price" => Deserialize::begin(&mut self.price),
95                "quantity" => Deserialize::begin(&mut self.quantity),
96                "taxes" => Deserialize::begin(&mut self.taxes),
97                _ => <dyn Visitor>::ignore(),
98            })
99        }
100
101        fn deser_default() -> Self {
102            Self {
103                amount_discount: Deserialize::default(),
104                amount_subtotal: Deserialize::default(),
105                amount_tax: Deserialize::default(),
106                amount_total: Deserialize::default(),
107                currency: Deserialize::default(),
108                description: Deserialize::default(),
109                discounts: Deserialize::default(),
110                id: Deserialize::default(),
111                price: Deserialize::default(),
112                quantity: Deserialize::default(),
113                taxes: Deserialize::default(),
114            }
115        }
116
117        fn take_out(&mut self) -> Option<Self::Out> {
118            let (
119                Some(amount_discount),
120                Some(amount_subtotal),
121                Some(amount_tax),
122                Some(amount_total),
123                Some(currency),
124                Some(description),
125                Some(discounts),
126                Some(id),
127                Some(price),
128                Some(quantity),
129                Some(taxes),
130            ) = (
131                self.amount_discount,
132                self.amount_subtotal,
133                self.amount_tax,
134                self.amount_total,
135                self.currency.take(),
136                self.description.take(),
137                self.discounts.take(),
138                self.id.take(),
139                self.price.take(),
140                self.quantity,
141                self.taxes.take(),
142            )
143            else {
144                return None;
145            };
146            Some(Self::Out {
147                amount_discount,
148                amount_subtotal,
149                amount_tax,
150                amount_total,
151                currency,
152                description,
153                discounts,
154                id,
155                price,
156                quantity,
157                taxes,
158            })
159        }
160    }
161
162    impl Map for Builder<'_> {
163        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
164            self.builder.key(k)
165        }
166
167        fn finish(&mut self) -> Result<()> {
168            *self.out = self.builder.take_out();
169            Ok(())
170        }
171    }
172
173    impl ObjectDeser for CheckoutSessionItem {
174        type Builder = CheckoutSessionItemBuilder;
175    }
176
177    impl FromValueOpt for CheckoutSessionItem {
178        fn from_value(v: Value) -> Option<Self> {
179            let Value::Object(obj) = v else {
180                return None;
181            };
182            let mut b = CheckoutSessionItemBuilder::deser_default();
183            for (k, v) in obj {
184                match k.as_str() {
185                    "amount_discount" => b.amount_discount = FromValueOpt::from_value(v),
186                    "amount_subtotal" => b.amount_subtotal = FromValueOpt::from_value(v),
187                    "amount_tax" => b.amount_tax = FromValueOpt::from_value(v),
188                    "amount_total" => b.amount_total = FromValueOpt::from_value(v),
189                    "currency" => b.currency = FromValueOpt::from_value(v),
190                    "description" => b.description = FromValueOpt::from_value(v),
191                    "discounts" => b.discounts = FromValueOpt::from_value(v),
192                    "id" => b.id = FromValueOpt::from_value(v),
193                    "price" => b.price = FromValueOpt::from_value(v),
194                    "quantity" => b.quantity = FromValueOpt::from_value(v),
195                    "taxes" => b.taxes = FromValueOpt::from_value(v),
196                    _ => {}
197                }
198            }
199            b.take_out()
200        }
201    }
202};
203#[cfg(feature = "serialize")]
204impl serde::Serialize for CheckoutSessionItem {
205    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
206        use serde::ser::SerializeStruct;
207        let mut s = s.serialize_struct("CheckoutSessionItem", 12)?;
208        s.serialize_field("amount_discount", &self.amount_discount)?;
209        s.serialize_field("amount_subtotal", &self.amount_subtotal)?;
210        s.serialize_field("amount_tax", &self.amount_tax)?;
211        s.serialize_field("amount_total", &self.amount_total)?;
212        s.serialize_field("currency", &self.currency)?;
213        s.serialize_field("description", &self.description)?;
214        s.serialize_field("discounts", &self.discounts)?;
215        s.serialize_field("id", &self.id)?;
216        s.serialize_field("price", &self.price)?;
217        s.serialize_field("quantity", &self.quantity)?;
218        s.serialize_field("taxes", &self.taxes)?;
219
220        s.serialize_field("object", "item")?;
221        s.end()
222    }
223}
224impl stripe_types::Object for CheckoutSessionItem {
225    type Id = stripe_shared::CheckoutSessionItemId;
226    fn id(&self) -> &Self::Id {
227        &self.id
228    }
229
230    fn into_id(self) -> Self::Id {
231        self.id
232    }
233}
234stripe_types::def_id!(CheckoutSessionItemId);