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
98                _ => <dyn Visitor>::ignore(),
99            })
100        }
101
102        fn deser_default() -> Self {
103            Self {
104                amount: Deserialize::default(),
105                amount_tax: Deserialize::default(),
106                id: Deserialize::default(),
107                livemode: Deserialize::default(),
108                metadata: Deserialize::default(),
109                product: Deserialize::default(),
110                quantity: Deserialize::default(),
111                reference: Deserialize::default(),
112                tax_behavior: Deserialize::default(),
113                tax_breakdown: Deserialize::default(),
114                tax_code: Deserialize::default(),
115            }
116        }
117
118        fn take_out(&mut self) -> Option<Self::Out> {
119            let (
120                Some(amount),
121                Some(amount_tax),
122                Some(id),
123                Some(livemode),
124                Some(metadata),
125                Some(product),
126                Some(quantity),
127                Some(reference),
128                Some(tax_behavior),
129                Some(tax_breakdown),
130                Some(tax_code),
131            ) = (
132                self.amount,
133                self.amount_tax,
134                self.id.take(),
135                self.livemode,
136                self.metadata.take(),
137                self.product.take(),
138                self.quantity,
139                self.reference.take(),
140                self.tax_behavior,
141                self.tax_breakdown.take(),
142                self.tax_code.take(),
143            )
144            else {
145                return None;
146            };
147            Some(Self::Out {
148                amount,
149                amount_tax,
150                id,
151                livemode,
152                metadata,
153                product,
154                quantity,
155                reference,
156                tax_behavior,
157                tax_breakdown,
158                tax_code,
159            })
160        }
161    }
162
163    impl Map for Builder<'_> {
164        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
165            self.builder.key(k)
166        }
167
168        fn finish(&mut self) -> Result<()> {
169            *self.out = self.builder.take_out();
170            Ok(())
171        }
172    }
173
174    impl ObjectDeser for TaxCalculationLineItem {
175        type Builder = TaxCalculationLineItemBuilder;
176    }
177
178    impl FromValueOpt for TaxCalculationLineItem {
179        fn from_value(v: Value) -> Option<Self> {
180            let Value::Object(obj) = v else {
181                return None;
182            };
183            let mut b = TaxCalculationLineItemBuilder::deser_default();
184            for (k, v) in obj {
185                match k.as_str() {
186                    "amount" => b.amount = FromValueOpt::from_value(v),
187                    "amount_tax" => b.amount_tax = FromValueOpt::from_value(v),
188                    "id" => b.id = FromValueOpt::from_value(v),
189                    "livemode" => b.livemode = FromValueOpt::from_value(v),
190                    "metadata" => b.metadata = FromValueOpt::from_value(v),
191                    "product" => b.product = FromValueOpt::from_value(v),
192                    "quantity" => b.quantity = FromValueOpt::from_value(v),
193                    "reference" => b.reference = FromValueOpt::from_value(v),
194                    "tax_behavior" => b.tax_behavior = FromValueOpt::from_value(v),
195                    "tax_breakdown" => b.tax_breakdown = FromValueOpt::from_value(v),
196                    "tax_code" => b.tax_code = FromValueOpt::from_value(v),
197
198                    _ => {}
199                }
200            }
201            b.take_out()
202        }
203    }
204};
205#[cfg(feature = "serialize")]
206impl serde::Serialize for TaxCalculationLineItem {
207    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
208        use serde::ser::SerializeStruct;
209        let mut s = s.serialize_struct("TaxCalculationLineItem", 12)?;
210        s.serialize_field("amount", &self.amount)?;
211        s.serialize_field("amount_tax", &self.amount_tax)?;
212        s.serialize_field("id", &self.id)?;
213        s.serialize_field("livemode", &self.livemode)?;
214        s.serialize_field("metadata", &self.metadata)?;
215        s.serialize_field("product", &self.product)?;
216        s.serialize_field("quantity", &self.quantity)?;
217        s.serialize_field("reference", &self.reference)?;
218        s.serialize_field("tax_behavior", &self.tax_behavior)?;
219        s.serialize_field("tax_breakdown", &self.tax_breakdown)?;
220        s.serialize_field("tax_code", &self.tax_code)?;
221
222        s.serialize_field("object", "tax.calculation_line_item")?;
223        s.end()
224    }
225}
226/// Specifies whether the `amount` includes taxes.
227/// If `tax_behavior=inclusive`, then the amount includes taxes.
228#[derive(Copy, Clone, Eq, PartialEq)]
229pub enum TaxCalculationLineItemTaxBehavior {
230    Exclusive,
231    Inclusive,
232}
233impl TaxCalculationLineItemTaxBehavior {
234    pub fn as_str(self) -> &'static str {
235        use TaxCalculationLineItemTaxBehavior::*;
236        match self {
237            Exclusive => "exclusive",
238            Inclusive => "inclusive",
239        }
240    }
241}
242
243impl std::str::FromStr for TaxCalculationLineItemTaxBehavior {
244    type Err = stripe_types::StripeParseError;
245    fn from_str(s: &str) -> Result<Self, Self::Err> {
246        use TaxCalculationLineItemTaxBehavior::*;
247        match s {
248            "exclusive" => Ok(Exclusive),
249            "inclusive" => Ok(Inclusive),
250            _ => Err(stripe_types::StripeParseError),
251        }
252    }
253}
254impl std::fmt::Display for TaxCalculationLineItemTaxBehavior {
255    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
256        f.write_str(self.as_str())
257    }
258}
259
260impl std::fmt::Debug for TaxCalculationLineItemTaxBehavior {
261    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
262        f.write_str(self.as_str())
263    }
264}
265#[cfg(feature = "serialize")]
266impl serde::Serialize for TaxCalculationLineItemTaxBehavior {
267    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
268    where
269        S: serde::Serializer,
270    {
271        serializer.serialize_str(self.as_str())
272    }
273}
274impl miniserde::Deserialize for TaxCalculationLineItemTaxBehavior {
275    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
276        crate::Place::new(out)
277    }
278}
279
280impl miniserde::de::Visitor for crate::Place<TaxCalculationLineItemTaxBehavior> {
281    fn string(&mut self, s: &str) -> miniserde::Result<()> {
282        use std::str::FromStr;
283        self.out =
284            Some(TaxCalculationLineItemTaxBehavior::from_str(s).map_err(|_| miniserde::Error)?);
285        Ok(())
286    }
287}
288
289stripe_types::impl_from_val_with_from_str!(TaxCalculationLineItemTaxBehavior);
290#[cfg(feature = "deserialize")]
291impl<'de> serde::Deserialize<'de> for TaxCalculationLineItemTaxBehavior {
292    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
293        use std::str::FromStr;
294        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
295        Self::from_str(&s).map_err(|_| {
296            serde::de::Error::custom("Unknown value for TaxCalculationLineItemTaxBehavior")
297        })
298    }
299}
300impl stripe_types::Object for TaxCalculationLineItem {
301    type Id = stripe_misc::TaxCalculationLineItemId;
302    fn id(&self) -> &Self::Id {
303        &self.id
304    }
305
306    fn into_id(self) -> Self::Id {
307        self.id
308    }
309}
310stripe_types::def_id!(TaxCalculationLineItemId);