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