stripe_shared/
billing_credit_grant.rs

1/// A credit grant is an API resource that documents the allocation of some billing credits to a customer.
2///
3/// Related guide: [Billing credits](https://docs.stripe.com/billing/subscriptions/usage-based/billing-credits).
4#[derive(Clone, Debug)]
5#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
6pub struct BillingCreditGrant {
7    pub amount: stripe_shared::BillingCreditGrantsResourceAmount,
8    pub applicability_config: stripe_shared::BillingCreditGrantsResourceApplicabilityConfig,
9    /// The category of this credit grant.
10    /// This is for tracking purposes and isn't displayed to the customer.
11    pub category: stripe_shared::BillingCreditGrantCategory,
12    /// Time at which the object was created. Measured in seconds since the Unix epoch.
13    pub created: stripe_types::Timestamp,
14    /// ID of the customer receiving the billing credits.
15    pub customer: stripe_types::Expandable<stripe_shared::Customer>,
16    /// ID of the account representing the customer receiving the billing credits
17    pub customer_account: Option<String>,
18    /// The time when the billing credits become effective-when they're eligible for use.
19    pub effective_at: Option<stripe_types::Timestamp>,
20    /// The time when the billing credits expire. If not present, the billing credits don't expire.
21    pub expires_at: Option<stripe_types::Timestamp>,
22    /// Unique identifier for the object.
23    pub id: stripe_shared::BillingCreditGrantId,
24    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
25    pub livemode: bool,
26    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
27    /// This can be useful for storing additional information about the object in a structured format.
28    pub metadata: std::collections::HashMap<String, String>,
29    /// A descriptive name shown in dashboard.
30    pub name: Option<String>,
31    /// The priority for applying this credit grant. The highest priority is 0 and the lowest is 100.
32    pub priority: Option<i64>,
33    /// ID of the test clock this credit grant belongs to.
34    pub test_clock: Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>,
35    /// Time at which the object was last updated. Measured in seconds since the Unix epoch.
36    pub updated: stripe_types::Timestamp,
37    /// The time when this credit grant was voided. If not present, the credit grant hasn't been voided.
38    pub voided_at: Option<stripe_types::Timestamp>,
39}
40#[doc(hidden)]
41pub struct BillingCreditGrantBuilder {
42    amount: Option<stripe_shared::BillingCreditGrantsResourceAmount>,
43    applicability_config: Option<stripe_shared::BillingCreditGrantsResourceApplicabilityConfig>,
44    category: Option<stripe_shared::BillingCreditGrantCategory>,
45    created: Option<stripe_types::Timestamp>,
46    customer: Option<stripe_types::Expandable<stripe_shared::Customer>>,
47    customer_account: Option<Option<String>>,
48    effective_at: Option<Option<stripe_types::Timestamp>>,
49    expires_at: Option<Option<stripe_types::Timestamp>>,
50    id: Option<stripe_shared::BillingCreditGrantId>,
51    livemode: Option<bool>,
52    metadata: Option<std::collections::HashMap<String, String>>,
53    name: Option<Option<String>>,
54    priority: Option<Option<i64>>,
55    test_clock: Option<Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>>,
56    updated: Option<stripe_types::Timestamp>,
57    voided_at: Option<Option<stripe_types::Timestamp>>,
58}
59
60#[allow(
61    unused_variables,
62    irrefutable_let_patterns,
63    clippy::let_unit_value,
64    clippy::match_single_binding,
65    clippy::single_match
66)]
67const _: () = {
68    use miniserde::de::{Map, Visitor};
69    use miniserde::json::Value;
70    use miniserde::{Deserialize, Result, make_place};
71    use stripe_types::miniserde_helpers::FromValueOpt;
72    use stripe_types::{MapBuilder, ObjectDeser};
73
74    make_place!(Place);
75
76    impl Deserialize for BillingCreditGrant {
77        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
78            Place::new(out)
79        }
80    }
81
82    struct Builder<'a> {
83        out: &'a mut Option<BillingCreditGrant>,
84        builder: BillingCreditGrantBuilder,
85    }
86
87    impl Visitor for Place<BillingCreditGrant> {
88        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
89            Ok(Box::new(Builder {
90                out: &mut self.out,
91                builder: BillingCreditGrantBuilder::deser_default(),
92            }))
93        }
94    }
95
96    impl MapBuilder for BillingCreditGrantBuilder {
97        type Out = BillingCreditGrant;
98        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
99            Ok(match k {
100                "amount" => Deserialize::begin(&mut self.amount),
101                "applicability_config" => Deserialize::begin(&mut self.applicability_config),
102                "category" => Deserialize::begin(&mut self.category),
103                "created" => Deserialize::begin(&mut self.created),
104                "customer" => Deserialize::begin(&mut self.customer),
105                "customer_account" => Deserialize::begin(&mut self.customer_account),
106                "effective_at" => Deserialize::begin(&mut self.effective_at),
107                "expires_at" => Deserialize::begin(&mut self.expires_at),
108                "id" => Deserialize::begin(&mut self.id),
109                "livemode" => Deserialize::begin(&mut self.livemode),
110                "metadata" => Deserialize::begin(&mut self.metadata),
111                "name" => Deserialize::begin(&mut self.name),
112                "priority" => Deserialize::begin(&mut self.priority),
113                "test_clock" => Deserialize::begin(&mut self.test_clock),
114                "updated" => Deserialize::begin(&mut self.updated),
115                "voided_at" => Deserialize::begin(&mut self.voided_at),
116                _ => <dyn Visitor>::ignore(),
117            })
118        }
119
120        fn deser_default() -> Self {
121            Self {
122                amount: Deserialize::default(),
123                applicability_config: Deserialize::default(),
124                category: Deserialize::default(),
125                created: Deserialize::default(),
126                customer: Deserialize::default(),
127                customer_account: Deserialize::default(),
128                effective_at: Deserialize::default(),
129                expires_at: Deserialize::default(),
130                id: Deserialize::default(),
131                livemode: Deserialize::default(),
132                metadata: Deserialize::default(),
133                name: Deserialize::default(),
134                priority: Deserialize::default(),
135                test_clock: Deserialize::default(),
136                updated: Deserialize::default(),
137                voided_at: Deserialize::default(),
138            }
139        }
140
141        fn take_out(&mut self) -> Option<Self::Out> {
142            let (
143                Some(amount),
144                Some(applicability_config),
145                Some(category),
146                Some(created),
147                Some(customer),
148                Some(customer_account),
149                Some(effective_at),
150                Some(expires_at),
151                Some(id),
152                Some(livemode),
153                Some(metadata),
154                Some(name),
155                Some(priority),
156                Some(test_clock),
157                Some(updated),
158                Some(voided_at),
159            ) = (
160                self.amount.take(),
161                self.applicability_config.take(),
162                self.category.take(),
163                self.created,
164                self.customer.take(),
165                self.customer_account.take(),
166                self.effective_at,
167                self.expires_at,
168                self.id.take(),
169                self.livemode,
170                self.metadata.take(),
171                self.name.take(),
172                self.priority,
173                self.test_clock.take(),
174                self.updated,
175                self.voided_at,
176            )
177            else {
178                return None;
179            };
180            Some(Self::Out {
181                amount,
182                applicability_config,
183                category,
184                created,
185                customer,
186                customer_account,
187                effective_at,
188                expires_at,
189                id,
190                livemode,
191                metadata,
192                name,
193                priority,
194                test_clock,
195                updated,
196                voided_at,
197            })
198        }
199    }
200
201    impl Map for Builder<'_> {
202        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
203            self.builder.key(k)
204        }
205
206        fn finish(&mut self) -> Result<()> {
207            *self.out = self.builder.take_out();
208            Ok(())
209        }
210    }
211
212    impl ObjectDeser for BillingCreditGrant {
213        type Builder = BillingCreditGrantBuilder;
214    }
215
216    impl FromValueOpt for BillingCreditGrant {
217        fn from_value(v: Value) -> Option<Self> {
218            let Value::Object(obj) = v else {
219                return None;
220            };
221            let mut b = BillingCreditGrantBuilder::deser_default();
222            for (k, v) in obj {
223                match k.as_str() {
224                    "amount" => b.amount = FromValueOpt::from_value(v),
225                    "applicability_config" => b.applicability_config = FromValueOpt::from_value(v),
226                    "category" => b.category = FromValueOpt::from_value(v),
227                    "created" => b.created = FromValueOpt::from_value(v),
228                    "customer" => b.customer = FromValueOpt::from_value(v),
229                    "customer_account" => b.customer_account = FromValueOpt::from_value(v),
230                    "effective_at" => b.effective_at = FromValueOpt::from_value(v),
231                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
232                    "id" => b.id = FromValueOpt::from_value(v),
233                    "livemode" => b.livemode = FromValueOpt::from_value(v),
234                    "metadata" => b.metadata = FromValueOpt::from_value(v),
235                    "name" => b.name = FromValueOpt::from_value(v),
236                    "priority" => b.priority = FromValueOpt::from_value(v),
237                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
238                    "updated" => b.updated = FromValueOpt::from_value(v),
239                    "voided_at" => b.voided_at = FromValueOpt::from_value(v),
240                    _ => {}
241                }
242            }
243            b.take_out()
244        }
245    }
246};
247#[cfg(feature = "serialize")]
248impl serde::Serialize for BillingCreditGrant {
249    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
250        use serde::ser::SerializeStruct;
251        let mut s = s.serialize_struct("BillingCreditGrant", 17)?;
252        s.serialize_field("amount", &self.amount)?;
253        s.serialize_field("applicability_config", &self.applicability_config)?;
254        s.serialize_field("category", &self.category)?;
255        s.serialize_field("created", &self.created)?;
256        s.serialize_field("customer", &self.customer)?;
257        s.serialize_field("customer_account", &self.customer_account)?;
258        s.serialize_field("effective_at", &self.effective_at)?;
259        s.serialize_field("expires_at", &self.expires_at)?;
260        s.serialize_field("id", &self.id)?;
261        s.serialize_field("livemode", &self.livemode)?;
262        s.serialize_field("metadata", &self.metadata)?;
263        s.serialize_field("name", &self.name)?;
264        s.serialize_field("priority", &self.priority)?;
265        s.serialize_field("test_clock", &self.test_clock)?;
266        s.serialize_field("updated", &self.updated)?;
267        s.serialize_field("voided_at", &self.voided_at)?;
268
269        s.serialize_field("object", "billing.credit_grant")?;
270        s.end()
271    }
272}
273impl stripe_types::Object for BillingCreditGrant {
274    type Id = stripe_shared::BillingCreditGrantId;
275    fn id(&self) -> &Self::Id {
276        &self.id
277    }
278
279    fn into_id(self) -> Self::Id {
280        self.id
281    }
282}
283stripe_types::def_id!(BillingCreditGrantId);
284#[derive(Clone, Eq, PartialEq)]
285#[non_exhaustive]
286pub enum BillingCreditGrantCategory {
287    Paid,
288    Promotional,
289    /// An unrecognized value from Stripe. Should not be used as a request parameter.
290    Unknown(String),
291}
292impl BillingCreditGrantCategory {
293    pub fn as_str(&self) -> &str {
294        use BillingCreditGrantCategory::*;
295        match self {
296            Paid => "paid",
297            Promotional => "promotional",
298            Unknown(v) => v,
299        }
300    }
301}
302
303impl std::str::FromStr for BillingCreditGrantCategory {
304    type Err = std::convert::Infallible;
305    fn from_str(s: &str) -> Result<Self, Self::Err> {
306        use BillingCreditGrantCategory::*;
307        match s {
308            "paid" => Ok(Paid),
309            "promotional" => Ok(Promotional),
310            v => {
311                tracing::warn!("Unknown value '{}' for enum '{}'", v, "BillingCreditGrantCategory");
312                Ok(Unknown(v.to_owned()))
313            }
314        }
315    }
316}
317impl std::fmt::Display for BillingCreditGrantCategory {
318    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
319        f.write_str(self.as_str())
320    }
321}
322
323impl std::fmt::Debug for BillingCreditGrantCategory {
324    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
325        f.write_str(self.as_str())
326    }
327}
328impl serde::Serialize for BillingCreditGrantCategory {
329    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
330    where
331        S: serde::Serializer,
332    {
333        serializer.serialize_str(self.as_str())
334    }
335}
336impl miniserde::Deserialize for BillingCreditGrantCategory {
337    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
338        crate::Place::new(out)
339    }
340}
341
342impl miniserde::de::Visitor for crate::Place<BillingCreditGrantCategory> {
343    fn string(&mut self, s: &str) -> miniserde::Result<()> {
344        use std::str::FromStr;
345        self.out = Some(BillingCreditGrantCategory::from_str(s).expect("infallible"));
346        Ok(())
347    }
348}
349
350stripe_types::impl_from_val_with_from_str!(BillingCreditGrantCategory);
351#[cfg(feature = "deserialize")]
352impl<'de> serde::Deserialize<'de> for BillingCreditGrantCategory {
353    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
354        use std::str::FromStr;
355        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
356        Ok(Self::from_str(&s).expect("infallible"))
357    }
358}