stripe_misc/
tax_calculation_line_item.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
3pub struct TaxCalculationLineItem {
4    /// The line item amount in the [smallest currency unit](https://stripe.com/docs/currencies#zero-decimal).
5    /// If `tax_behavior=inclusive`, then this amount includes taxes.
6    /// Otherwise, taxes were calculated on top of this amount.
7    pub amount: i64,
8    /// The amount of tax calculated for this line item, in the [smallest currency unit](https://stripe.com/docs/currencies#zero-decimal).
9    pub amount_tax: i64,
10    /// Unique identifier for the object.
11    pub id: stripe_misc::TaxCalculationLineItemId,
12    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
13    pub livemode: bool,
14    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
15    /// This can be useful for storing additional information about the object in a structured format.
16    pub metadata: Option<std::collections::HashMap<String, String>>,
17    /// The ID of an existing [Product](https://stripe.com/docs/api/products/object).
18    pub product: Option<String>,
19    /// The number of units of the item being purchased. For reversals, this is the quantity reversed.
20    pub quantity: u64,
21    /// A custom identifier for this line item.
22    pub reference: String,
23    /// Specifies whether the `amount` includes taxes.
24    /// If `tax_behavior=inclusive`, then the amount includes taxes.
25    pub tax_behavior: TaxCalculationLineItemTaxBehavior,
26    /// Detailed account of taxes relevant to this line item.
27    pub tax_breakdown: Option<Vec<stripe_misc::TaxProductResourceLineItemTaxBreakdown>>,
28    /// The [tax code](https://stripe.com/docs/tax/tax-categories) ID used for this resource.
29    pub tax_code: String,
30}
31#[doc(hidden)]
32pub struct TaxCalculationLineItemBuilder {
33    amount: Option<i64>,
34    amount_tax: Option<i64>,
35    id: Option<stripe_misc::TaxCalculationLineItemId>,
36    livemode: Option<bool>,
37    metadata: Option<Option<std::collections::HashMap<String, String>>>,
38    product: Option<Option<String>>,
39    quantity: Option<u64>,
40    reference: Option<String>,
41    tax_behavior: Option<TaxCalculationLineItemTaxBehavior>,
42    tax_breakdown: Option<Option<Vec<stripe_misc::TaxProductResourceLineItemTaxBreakdown>>>,
43    tax_code: Option<String>,
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 TaxCalculationLineItem {
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<TaxCalculationLineItem>,
70        builder: TaxCalculationLineItemBuilder,
71    }
72
73    impl Visitor for Place<TaxCalculationLineItem> {
74        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
75            Ok(Box::new(Builder {
76                out: &mut self.out,
77                builder: TaxCalculationLineItemBuilder::deser_default(),
78            }))
79        }
80    }
81
82    impl MapBuilder for TaxCalculationLineItemBuilder {
83        type Out = TaxCalculationLineItem;
84        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
85            Ok(match k {
86                "amount" => Deserialize::begin(&mut self.amount),
87                "amount_tax" => Deserialize::begin(&mut self.amount_tax),
88                "id" => Deserialize::begin(&mut self.id),
89                "livemode" => Deserialize::begin(&mut self.livemode),
90                "metadata" => Deserialize::begin(&mut self.metadata),
91                "product" => Deserialize::begin(&mut self.product),
92                "quantity" => Deserialize::begin(&mut self.quantity),
93                "reference" => Deserialize::begin(&mut self.reference),
94                "tax_behavior" => Deserialize::begin(&mut self.tax_behavior),
95                "tax_breakdown" => Deserialize::begin(&mut self.tax_breakdown),
96                "tax_code" => Deserialize::begin(&mut self.tax_code),
97                _ => <dyn Visitor>::ignore(),
98            })
99        }
100
101        fn deser_default() -> Self {
102            Self {
103                amount: Deserialize::default(),
104                amount_tax: Deserialize::default(),
105                id: Deserialize::default(),
106                livemode: Deserialize::default(),
107                metadata: Deserialize::default(),
108                product: Deserialize::default(),
109                quantity: Deserialize::default(),
110                reference: Deserialize::default(),
111                tax_behavior: Deserialize::default(),
112                tax_breakdown: Deserialize::default(),
113                tax_code: Deserialize::default(),
114            }
115        }
116
117        fn take_out(&mut self) -> Option<Self::Out> {
118            let (
119                Some(amount),
120                Some(amount_tax),
121                Some(id),
122                Some(livemode),
123                Some(metadata),
124                Some(product),
125                Some(quantity),
126                Some(reference),
127                Some(tax_behavior),
128                Some(tax_breakdown),
129                Some(tax_code),
130            ) = (
131                self.amount,
132                self.amount_tax,
133                self.id.take(),
134                self.livemode,
135                self.metadata.take(),
136                self.product.take(),
137                self.quantity,
138                self.reference.take(),
139                self.tax_behavior,
140                self.tax_breakdown.take(),
141                self.tax_code.take(),
142            )
143            else {
144                return None;
145            };
146            Some(Self::Out {
147                amount,
148                amount_tax,
149                id,
150                livemode,
151                metadata,
152                product,
153                quantity,
154                reference,
155                tax_behavior,
156                tax_breakdown,
157                tax_code,
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 TaxCalculationLineItem {
174        type Builder = TaxCalculationLineItemBuilder;
175    }
176
177    impl FromValueOpt for TaxCalculationLineItem {
178        fn from_value(v: Value) -> Option<Self> {
179            let Value::Object(obj) = v else {
180                return None;
181            };
182            let mut b = TaxCalculationLineItemBuilder::deser_default();
183            for (k, v) in obj {
184                match k.as_str() {
185                    "amount" => b.amount = FromValueOpt::from_value(v),
186                    "amount_tax" => b.amount_tax = FromValueOpt::from_value(v),
187                    "id" => b.id = FromValueOpt::from_value(v),
188                    "livemode" => b.livemode = FromValueOpt::from_value(v),
189                    "metadata" => b.metadata = FromValueOpt::from_value(v),
190                    "product" => b.product = FromValueOpt::from_value(v),
191                    "quantity" => b.quantity = FromValueOpt::from_value(v),
192                    "reference" => b.reference = FromValueOpt::from_value(v),
193                    "tax_behavior" => b.tax_behavior = FromValueOpt::from_value(v),
194                    "tax_breakdown" => b.tax_breakdown = FromValueOpt::from_value(v),
195                    "tax_code" => b.tax_code = FromValueOpt::from_value(v),
196                    _ => {}
197                }
198            }
199            b.take_out()
200        }
201    }
202};
203#[cfg(feature = "serialize")]
204impl serde::Serialize for TaxCalculationLineItem {
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("TaxCalculationLineItem", 12)?;
208        s.serialize_field("amount", &self.amount)?;
209        s.serialize_field("amount_tax", &self.amount_tax)?;
210        s.serialize_field("id", &self.id)?;
211        s.serialize_field("livemode", &self.livemode)?;
212        s.serialize_field("metadata", &self.metadata)?;
213        s.serialize_field("product", &self.product)?;
214        s.serialize_field("quantity", &self.quantity)?;
215        s.serialize_field("reference", &self.reference)?;
216        s.serialize_field("tax_behavior", &self.tax_behavior)?;
217        s.serialize_field("tax_breakdown", &self.tax_breakdown)?;
218        s.serialize_field("tax_code", &self.tax_code)?;
219
220        s.serialize_field("object", "tax.calculation_line_item")?;
221        s.end()
222    }
223}
224/// Specifies whether the `amount` includes taxes.
225/// If `tax_behavior=inclusive`, then the amount includes taxes.
226#[derive(Copy, Clone, Eq, PartialEq)]
227pub enum TaxCalculationLineItemTaxBehavior {
228    Exclusive,
229    Inclusive,
230}
231impl TaxCalculationLineItemTaxBehavior {
232    pub fn as_str(self) -> &'static str {
233        use TaxCalculationLineItemTaxBehavior::*;
234        match self {
235            Exclusive => "exclusive",
236            Inclusive => "inclusive",
237        }
238    }
239}
240
241impl std::str::FromStr for TaxCalculationLineItemTaxBehavior {
242    type Err = stripe_types::StripeParseError;
243    fn from_str(s: &str) -> Result<Self, Self::Err> {
244        use TaxCalculationLineItemTaxBehavior::*;
245        match s {
246            "exclusive" => Ok(Exclusive),
247            "inclusive" => Ok(Inclusive),
248            _ => Err(stripe_types::StripeParseError),
249        }
250    }
251}
252impl std::fmt::Display for TaxCalculationLineItemTaxBehavior {
253    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
254        f.write_str(self.as_str())
255    }
256}
257
258impl std::fmt::Debug for TaxCalculationLineItemTaxBehavior {
259    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
260        f.write_str(self.as_str())
261    }
262}
263#[cfg(feature = "serialize")]
264impl serde::Serialize for TaxCalculationLineItemTaxBehavior {
265    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
266    where
267        S: serde::Serializer,
268    {
269        serializer.serialize_str(self.as_str())
270    }
271}
272impl miniserde::Deserialize for TaxCalculationLineItemTaxBehavior {
273    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
274        crate::Place::new(out)
275    }
276}
277
278impl miniserde::de::Visitor for crate::Place<TaxCalculationLineItemTaxBehavior> {
279    fn string(&mut self, s: &str) -> miniserde::Result<()> {
280        use std::str::FromStr;
281        self.out =
282            Some(TaxCalculationLineItemTaxBehavior::from_str(s).map_err(|_| miniserde::Error)?);
283        Ok(())
284    }
285}
286
287stripe_types::impl_from_val_with_from_str!(TaxCalculationLineItemTaxBehavior);
288#[cfg(feature = "deserialize")]
289impl<'de> serde::Deserialize<'de> for TaxCalculationLineItemTaxBehavior {
290    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
291        use std::str::FromStr;
292        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
293        Self::from_str(&s).map_err(|_| {
294            serde::de::Error::custom("Unknown value for TaxCalculationLineItemTaxBehavior")
295        })
296    }
297}
298impl stripe_types::Object for TaxCalculationLineItem {
299    type Id = stripe_misc::TaxCalculationLineItemId;
300    fn id(&self) -> &Self::Id {
301        &self.id
302    }
303
304    fn into_id(self) -> Self::Id {
305        self.id
306    }
307}
308stripe_types::def_id!(TaxCalculationLineItemId);