Skip to main content

stripe_shared/
credit_note_line_item.rs

1/// The credit note line item object
2///
3/// For more details see <<https://stripe.com/docs/api/credit_notes/line_item>>.
4#[derive(Clone)]
5#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
6#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
7pub struct CreditNoteLineItem {
8    /// The integer amount in cents (or local equivalent) representing the gross amount being credited for this line item, excluding (exclusive) tax and discounts.
9    pub amount: i64,
10    /// Description of the item being credited.
11    pub description: Option<String>,
12    /// The integer amount in cents (or local equivalent) representing the discount being credited for this line item.
13    pub discount_amount: i64,
14    /// The amount of discount calculated per discount for this line item
15    pub discount_amounts: Vec<stripe_shared::DiscountsResourceDiscountAmount>,
16    /// Unique identifier for the object.
17    pub id: stripe_shared::CreditNoteLineItemId,
18    /// ID of the invoice line item being credited
19    pub invoice_line_item: Option<String>,
20    /// If the object exists in live mode, the value is `true`.
21    /// If the object exists in test mode, the value is `false`.
22    pub livemode: bool,
23    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
24    /// This can be useful for storing additional information about the object in a structured format.
25    pub metadata: Option<std::collections::HashMap<String, String>>,
26    /// The pretax credit amounts (ex: discount, credit grants, etc) for this line item.
27    pub pretax_credit_amounts: Vec<stripe_shared::CreditNotesPretaxCreditAmount>,
28    /// The number of units of product being credited.
29    pub quantity: Option<u64>,
30    /// The tax rates which apply to the line item.
31    pub tax_rates: Vec<stripe_shared::TaxRate>,
32    /// The tax information of the line item.
33    pub taxes: Option<Vec<stripe_shared::BillingBillResourceInvoicingTaxesTax>>,
34    /// The type of the credit note line item, one of `invoice_line_item` or `custom_line_item`.
35    /// When the type is `invoice_line_item` there is an additional `invoice_line_item` property on the resource the value of which is the id of the credited line item on the invoice.
36    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
37    pub type_: CreditNoteLineItemType,
38    /// The cost of each unit of product being credited.
39    pub unit_amount: Option<i64>,
40    /// Same as `unit_amount`, but contains a decimal value with at most 12 decimal places.
41    pub unit_amount_decimal: Option<String>,
42}
43#[cfg(feature = "redact-generated-debug")]
44impl std::fmt::Debug for CreditNoteLineItem {
45    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46        f.debug_struct("CreditNoteLineItem").finish_non_exhaustive()
47    }
48}
49#[doc(hidden)]
50pub struct CreditNoteLineItemBuilder {
51    amount: Option<i64>,
52    description: Option<Option<String>>,
53    discount_amount: Option<i64>,
54    discount_amounts: Option<Vec<stripe_shared::DiscountsResourceDiscountAmount>>,
55    id: Option<stripe_shared::CreditNoteLineItemId>,
56    invoice_line_item: Option<Option<String>>,
57    livemode: Option<bool>,
58    metadata: Option<Option<std::collections::HashMap<String, String>>>,
59    pretax_credit_amounts: Option<Vec<stripe_shared::CreditNotesPretaxCreditAmount>>,
60    quantity: Option<Option<u64>>,
61    tax_rates: Option<Vec<stripe_shared::TaxRate>>,
62    taxes: Option<Option<Vec<stripe_shared::BillingBillResourceInvoicingTaxesTax>>>,
63    type_: Option<CreditNoteLineItemType>,
64    unit_amount: Option<Option<i64>>,
65    unit_amount_decimal: Option<Option<String>>,
66}
67
68#[allow(
69    unused_variables,
70    irrefutable_let_patterns,
71    clippy::let_unit_value,
72    clippy::match_single_binding,
73    clippy::single_match
74)]
75const _: () = {
76    use miniserde::de::{Map, Visitor};
77    use miniserde::json::Value;
78    use miniserde::{Deserialize, Result, make_place};
79    use stripe_types::miniserde_helpers::FromValueOpt;
80    use stripe_types::{MapBuilder, ObjectDeser};
81
82    make_place!(Place);
83
84    impl Deserialize for CreditNoteLineItem {
85        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
86            Place::new(out)
87        }
88    }
89
90    struct Builder<'a> {
91        out: &'a mut Option<CreditNoteLineItem>,
92        builder: CreditNoteLineItemBuilder,
93    }
94
95    impl Visitor for Place<CreditNoteLineItem> {
96        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
97            Ok(Box::new(Builder {
98                out: &mut self.out,
99                builder: CreditNoteLineItemBuilder::deser_default(),
100            }))
101        }
102    }
103
104    impl MapBuilder for CreditNoteLineItemBuilder {
105        type Out = CreditNoteLineItem;
106        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
107            Ok(match k {
108                "amount" => Deserialize::begin(&mut self.amount),
109                "description" => Deserialize::begin(&mut self.description),
110                "discount_amount" => Deserialize::begin(&mut self.discount_amount),
111                "discount_amounts" => Deserialize::begin(&mut self.discount_amounts),
112                "id" => Deserialize::begin(&mut self.id),
113                "invoice_line_item" => Deserialize::begin(&mut self.invoice_line_item),
114                "livemode" => Deserialize::begin(&mut self.livemode),
115                "metadata" => Deserialize::begin(&mut self.metadata),
116                "pretax_credit_amounts" => Deserialize::begin(&mut self.pretax_credit_amounts),
117                "quantity" => Deserialize::begin(&mut self.quantity),
118                "tax_rates" => Deserialize::begin(&mut self.tax_rates),
119                "taxes" => Deserialize::begin(&mut self.taxes),
120                "type" => Deserialize::begin(&mut self.type_),
121                "unit_amount" => Deserialize::begin(&mut self.unit_amount),
122                "unit_amount_decimal" => Deserialize::begin(&mut self.unit_amount_decimal),
123                _ => <dyn Visitor>::ignore(),
124            })
125        }
126
127        fn deser_default() -> Self {
128            Self {
129                amount: None,
130                description: Some(None),
131                discount_amount: None,
132                discount_amounts: None,
133                id: None,
134                invoice_line_item: Some(None),
135                livemode: None,
136                metadata: Some(None),
137                pretax_credit_amounts: None,
138                quantity: Some(None),
139                tax_rates: None,
140                taxes: Some(None),
141                type_: None,
142                unit_amount: Some(None),
143                unit_amount_decimal: Some(None),
144            }
145        }
146
147        fn take_out(&mut self) -> Option<Self::Out> {
148            let (
149                Some(amount),
150                Some(description),
151                Some(discount_amount),
152                Some(discount_amounts),
153                Some(id),
154                Some(invoice_line_item),
155                Some(livemode),
156                Some(metadata),
157                Some(pretax_credit_amounts),
158                Some(quantity),
159                Some(tax_rates),
160                Some(taxes),
161                Some(type_),
162                Some(unit_amount),
163                Some(unit_amount_decimal),
164            ) = (
165                self.amount,
166                self.description.take(),
167                self.discount_amount,
168                self.discount_amounts.take(),
169                self.id.take(),
170                self.invoice_line_item.take(),
171                self.livemode,
172                self.metadata.take(),
173                self.pretax_credit_amounts.take(),
174                self.quantity,
175                self.tax_rates.take(),
176                self.taxes.take(),
177                self.type_.take(),
178                self.unit_amount,
179                self.unit_amount_decimal.take(),
180            )
181            else {
182                return None;
183            };
184            Some(Self::Out {
185                amount,
186                description,
187                discount_amount,
188                discount_amounts,
189                id,
190                invoice_line_item,
191                livemode,
192                metadata,
193                pretax_credit_amounts,
194                quantity,
195                tax_rates,
196                taxes,
197                type_,
198                unit_amount,
199                unit_amount_decimal,
200            })
201        }
202    }
203
204    impl Map for Builder<'_> {
205        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
206            self.builder.key(k)
207        }
208
209        fn finish(&mut self) -> Result<()> {
210            *self.out = self.builder.take_out();
211            Ok(())
212        }
213    }
214
215    impl ObjectDeser for CreditNoteLineItem {
216        type Builder = CreditNoteLineItemBuilder;
217    }
218
219    impl FromValueOpt for CreditNoteLineItem {
220        fn from_value(v: Value) -> Option<Self> {
221            let Value::Object(obj) = v else {
222                return None;
223            };
224            let mut b = CreditNoteLineItemBuilder::deser_default();
225            for (k, v) in obj {
226                match k.as_str() {
227                    "amount" => b.amount = FromValueOpt::from_value(v),
228                    "description" => b.description = FromValueOpt::from_value(v),
229                    "discount_amount" => b.discount_amount = FromValueOpt::from_value(v),
230                    "discount_amounts" => b.discount_amounts = FromValueOpt::from_value(v),
231                    "id" => b.id = FromValueOpt::from_value(v),
232                    "invoice_line_item" => b.invoice_line_item = FromValueOpt::from_value(v),
233                    "livemode" => b.livemode = FromValueOpt::from_value(v),
234                    "metadata" => b.metadata = FromValueOpt::from_value(v),
235                    "pretax_credit_amounts" => {
236                        b.pretax_credit_amounts = FromValueOpt::from_value(v)
237                    }
238                    "quantity" => b.quantity = FromValueOpt::from_value(v),
239                    "tax_rates" => b.tax_rates = FromValueOpt::from_value(v),
240                    "taxes" => b.taxes = FromValueOpt::from_value(v),
241                    "type" => b.type_ = FromValueOpt::from_value(v),
242                    "unit_amount" => b.unit_amount = FromValueOpt::from_value(v),
243                    "unit_amount_decimal" => b.unit_amount_decimal = FromValueOpt::from_value(v),
244                    _ => {}
245                }
246            }
247            b.take_out()
248        }
249    }
250};
251#[cfg(feature = "serialize")]
252impl serde::Serialize for CreditNoteLineItem {
253    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
254        use serde::ser::SerializeStruct;
255        let mut s = s.serialize_struct("CreditNoteLineItem", 16)?;
256        s.serialize_field("amount", &self.amount)?;
257        s.serialize_field("description", &self.description)?;
258        s.serialize_field("discount_amount", &self.discount_amount)?;
259        s.serialize_field("discount_amounts", &self.discount_amounts)?;
260        s.serialize_field("id", &self.id)?;
261        s.serialize_field("invoice_line_item", &self.invoice_line_item)?;
262        s.serialize_field("livemode", &self.livemode)?;
263        s.serialize_field("metadata", &self.metadata)?;
264        s.serialize_field("pretax_credit_amounts", &self.pretax_credit_amounts)?;
265        s.serialize_field("quantity", &self.quantity)?;
266        s.serialize_field("tax_rates", &self.tax_rates)?;
267        s.serialize_field("taxes", &self.taxes)?;
268        s.serialize_field("type", &self.type_)?;
269        s.serialize_field("unit_amount", &self.unit_amount)?;
270        s.serialize_field("unit_amount_decimal", &self.unit_amount_decimal)?;
271
272        s.serialize_field("object", "credit_note_line_item")?;
273        s.end()
274    }
275}
276/// The type of the credit note line item, one of `invoice_line_item` or `custom_line_item`.
277/// When the type is `invoice_line_item` there is an additional `invoice_line_item` property on the resource the value of which is the id of the credited line item on the invoice.
278#[derive(Clone, Eq, PartialEq)]
279#[non_exhaustive]
280pub enum CreditNoteLineItemType {
281    CustomLineItem,
282    InvoiceLineItem,
283    /// An unrecognized value from Stripe. Should not be used as a request parameter.
284    Unknown(String),
285}
286impl CreditNoteLineItemType {
287    pub fn as_str(&self) -> &str {
288        use CreditNoteLineItemType::*;
289        match self {
290            CustomLineItem => "custom_line_item",
291            InvoiceLineItem => "invoice_line_item",
292            Unknown(v) => v,
293        }
294    }
295}
296
297impl std::str::FromStr for CreditNoteLineItemType {
298    type Err = std::convert::Infallible;
299    fn from_str(s: &str) -> Result<Self, Self::Err> {
300        use CreditNoteLineItemType::*;
301        match s {
302            "custom_line_item" => Ok(CustomLineItem),
303            "invoice_line_item" => Ok(InvoiceLineItem),
304            v => {
305                tracing::warn!("Unknown value '{}' for enum '{}'", v, "CreditNoteLineItemType");
306                Ok(Unknown(v.to_owned()))
307            }
308        }
309    }
310}
311impl std::fmt::Display for CreditNoteLineItemType {
312    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
313        f.write_str(self.as_str())
314    }
315}
316
317#[cfg(not(feature = "redact-generated-debug"))]
318impl std::fmt::Debug for CreditNoteLineItemType {
319    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
320        f.write_str(self.as_str())
321    }
322}
323#[cfg(feature = "redact-generated-debug")]
324impl std::fmt::Debug for CreditNoteLineItemType {
325    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
326        f.debug_struct(stringify!(CreditNoteLineItemType)).finish_non_exhaustive()
327    }
328}
329#[cfg(feature = "serialize")]
330impl serde::Serialize for CreditNoteLineItemType {
331    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
332    where
333        S: serde::Serializer,
334    {
335        serializer.serialize_str(self.as_str())
336    }
337}
338impl miniserde::Deserialize for CreditNoteLineItemType {
339    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
340        crate::Place::new(out)
341    }
342}
343
344impl miniserde::de::Visitor for crate::Place<CreditNoteLineItemType> {
345    fn string(&mut self, s: &str) -> miniserde::Result<()> {
346        use std::str::FromStr;
347        self.out = Some(CreditNoteLineItemType::from_str(s).expect("infallible"));
348        Ok(())
349    }
350}
351
352stripe_types::impl_from_val_with_from_str!(CreditNoteLineItemType);
353#[cfg(feature = "deserialize")]
354impl<'de> serde::Deserialize<'de> for CreditNoteLineItemType {
355    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
356        use std::str::FromStr;
357        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
358        Ok(Self::from_str(&s).expect("infallible"))
359    }
360}
361impl stripe_types::Object for CreditNoteLineItem {
362    type Id = stripe_shared::CreditNoteLineItemId;
363    fn id(&self) -> &Self::Id {
364        &self.id
365    }
366
367    fn into_id(self) -> Self::Id {
368        self.id
369    }
370}
371stripe_types::def_id!(CreditNoteLineItemId);