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                _ => <dyn Visitor>::ignore(),
113            })
114        }
115
116        fn deser_default() -> Self {
117            Self {
118                amount: Deserialize::default(),
119                applicability_config: Deserialize::default(),
120                category: Deserialize::default(),
121                created: Deserialize::default(),
122                customer: Deserialize::default(),
123                effective_at: Deserialize::default(),
124                expires_at: Deserialize::default(),
125                id: Deserialize::default(),
126                livemode: Deserialize::default(),
127                metadata: Deserialize::default(),
128                name: Deserialize::default(),
129                priority: Deserialize::default(),
130                test_clock: Deserialize::default(),
131                updated: Deserialize::default(),
132                voided_at: Deserialize::default(),
133            }
134        }
135
136        fn take_out(&mut self) -> Option<Self::Out> {
137            let (
138                Some(amount),
139                Some(applicability_config),
140                Some(category),
141                Some(created),
142                Some(customer),
143                Some(effective_at),
144                Some(expires_at),
145                Some(id),
146                Some(livemode),
147                Some(metadata),
148                Some(name),
149                Some(priority),
150                Some(test_clock),
151                Some(updated),
152                Some(voided_at),
153            ) = (
154                self.amount.take(),
155                self.applicability_config.take(),
156                self.category,
157                self.created,
158                self.customer.take(),
159                self.effective_at,
160                self.expires_at,
161                self.id.take(),
162                self.livemode,
163                self.metadata.take(),
164                self.name.take(),
165                self.priority,
166                self.test_clock.take(),
167                self.updated,
168                self.voided_at,
169            )
170            else {
171                return None;
172            };
173            Some(Self::Out {
174                amount,
175                applicability_config,
176                category,
177                created,
178                customer,
179                effective_at,
180                expires_at,
181                id,
182                livemode,
183                metadata,
184                name,
185                priority,
186                test_clock,
187                updated,
188                voided_at,
189            })
190        }
191    }
192
193    impl Map for Builder<'_> {
194        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
195            self.builder.key(k)
196        }
197
198        fn finish(&mut self) -> Result<()> {
199            *self.out = self.builder.take_out();
200            Ok(())
201        }
202    }
203
204    impl ObjectDeser for BillingCreditGrant {
205        type Builder = BillingCreditGrantBuilder;
206    }
207
208    impl FromValueOpt for BillingCreditGrant {
209        fn from_value(v: Value) -> Option<Self> {
210            let Value::Object(obj) = v else {
211                return None;
212            };
213            let mut b = BillingCreditGrantBuilder::deser_default();
214            for (k, v) in obj {
215                match k.as_str() {
216                    "amount" => b.amount = FromValueOpt::from_value(v),
217                    "applicability_config" => b.applicability_config = FromValueOpt::from_value(v),
218                    "category" => b.category = FromValueOpt::from_value(v),
219                    "created" => b.created = FromValueOpt::from_value(v),
220                    "customer" => b.customer = FromValueOpt::from_value(v),
221                    "effective_at" => b.effective_at = FromValueOpt::from_value(v),
222                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
223                    "id" => b.id = FromValueOpt::from_value(v),
224                    "livemode" => b.livemode = FromValueOpt::from_value(v),
225                    "metadata" => b.metadata = FromValueOpt::from_value(v),
226                    "name" => b.name = FromValueOpt::from_value(v),
227                    "priority" => b.priority = FromValueOpt::from_value(v),
228                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
229                    "updated" => b.updated = FromValueOpt::from_value(v),
230                    "voided_at" => b.voided_at = FromValueOpt::from_value(v),
231                    _ => {}
232                }
233            }
234            b.take_out()
235        }
236    }
237};
238#[cfg(feature = "serialize")]
239impl serde::Serialize for BillingCreditGrant {
240    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
241        use serde::ser::SerializeStruct;
242        let mut s = s.serialize_struct("BillingCreditGrant", 16)?;
243        s.serialize_field("amount", &self.amount)?;
244        s.serialize_field("applicability_config", &self.applicability_config)?;
245        s.serialize_field("category", &self.category)?;
246        s.serialize_field("created", &self.created)?;
247        s.serialize_field("customer", &self.customer)?;
248        s.serialize_field("effective_at", &self.effective_at)?;
249        s.serialize_field("expires_at", &self.expires_at)?;
250        s.serialize_field("id", &self.id)?;
251        s.serialize_field("livemode", &self.livemode)?;
252        s.serialize_field("metadata", &self.metadata)?;
253        s.serialize_field("name", &self.name)?;
254        s.serialize_field("priority", &self.priority)?;
255        s.serialize_field("test_clock", &self.test_clock)?;
256        s.serialize_field("updated", &self.updated)?;
257        s.serialize_field("voided_at", &self.voided_at)?;
258
259        s.serialize_field("object", "billing.credit_grant")?;
260        s.end()
261    }
262}
263impl stripe_types::Object for BillingCreditGrant {
264    type Id = stripe_shared::BillingCreditGrantId;
265    fn id(&self) -> &Self::Id {
266        &self.id
267    }
268
269    fn into_id(self) -> Self::Id {
270        self.id
271    }
272}
273stripe_types::def_id!(BillingCreditGrantId);
274#[derive(Copy, Clone, Eq, PartialEq)]
275pub enum BillingCreditGrantCategory {
276    Paid,
277    Promotional,
278}
279impl BillingCreditGrantCategory {
280    pub fn as_str(self) -> &'static str {
281        use BillingCreditGrantCategory::*;
282        match self {
283            Paid => "paid",
284            Promotional => "promotional",
285        }
286    }
287}
288
289impl std::str::FromStr for BillingCreditGrantCategory {
290    type Err = stripe_types::StripeParseError;
291    fn from_str(s: &str) -> Result<Self, Self::Err> {
292        use BillingCreditGrantCategory::*;
293        match s {
294            "paid" => Ok(Paid),
295            "promotional" => Ok(Promotional),
296            _ => Err(stripe_types::StripeParseError),
297        }
298    }
299}
300impl std::fmt::Display for BillingCreditGrantCategory {
301    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
302        f.write_str(self.as_str())
303    }
304}
305
306impl std::fmt::Debug for BillingCreditGrantCategory {
307    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
308        f.write_str(self.as_str())
309    }
310}
311impl serde::Serialize for BillingCreditGrantCategory {
312    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
313    where
314        S: serde::Serializer,
315    {
316        serializer.serialize_str(self.as_str())
317    }
318}
319impl miniserde::Deserialize for BillingCreditGrantCategory {
320    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
321        crate::Place::new(out)
322    }
323}
324
325impl miniserde::de::Visitor for crate::Place<BillingCreditGrantCategory> {
326    fn string(&mut self, s: &str) -> miniserde::Result<()> {
327        use std::str::FromStr;
328        self.out = Some(BillingCreditGrantCategory::from_str(s).map_err(|_| miniserde::Error)?);
329        Ok(())
330    }
331}
332
333stripe_types::impl_from_val_with_from_str!(BillingCreditGrantCategory);
334#[cfg(feature = "deserialize")]
335impl<'de> serde::Deserialize<'de> for BillingCreditGrantCategory {
336    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
337        use std::str::FromStr;
338        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
339        Self::from_str(&s)
340            .map_err(|_| serde::de::Error::custom("Unknown value for BillingCreditGrantCategory"))
341    }
342}