Skip to main content

stripe_shared/
billing_credit_balance_transaction.rs

1/// A credit balance transaction is a resource representing a transaction (either a credit or a debit) against an existing credit grant.
2#[derive(Clone)]
3#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct BillingCreditBalanceTransaction {
6    /// Time at which the object was created. Measured in seconds since the Unix epoch.
7    pub created: stripe_types::Timestamp,
8    /// Credit details for this credit balance transaction. Only present if type is `credit`.
9    pub credit: Option<stripe_shared::BillingCreditGrantsResourceBalanceCredit>,
10    /// The credit grant associated with this credit balance transaction.
11    pub credit_grant: stripe_types::Expandable<stripe_shared::BillingCreditGrant>,
12    /// Debit details for this credit balance transaction. Only present if type is `debit`.
13    pub debit: Option<stripe_shared::BillingCreditGrantsResourceBalanceDebit>,
14    /// The effective time of this credit balance transaction.
15    pub effective_at: stripe_types::Timestamp,
16    /// Unique identifier for the object.
17    pub id: stripe_shared::BillingCreditBalanceTransactionId,
18    /// If the object exists in live mode, the value is `true`.
19    /// If the object exists in test mode, the value is `false`.
20    pub livemode: bool,
21    /// ID of the test clock this credit balance transaction belongs to.
22    pub test_clock: Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>,
23    /// The type of credit balance transaction (credit or debit).
24    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
25    pub type_: Option<BillingCreditBalanceTransactionType>,
26}
27#[cfg(feature = "redact-generated-debug")]
28impl std::fmt::Debug for BillingCreditBalanceTransaction {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        f.debug_struct("BillingCreditBalanceTransaction").finish_non_exhaustive()
31    }
32}
33#[doc(hidden)]
34pub struct BillingCreditBalanceTransactionBuilder {
35    created: Option<stripe_types::Timestamp>,
36    credit: Option<Option<stripe_shared::BillingCreditGrantsResourceBalanceCredit>>,
37    credit_grant: Option<stripe_types::Expandable<stripe_shared::BillingCreditGrant>>,
38    debit: Option<Option<stripe_shared::BillingCreditGrantsResourceBalanceDebit>>,
39    effective_at: Option<stripe_types::Timestamp>,
40    id: Option<stripe_shared::BillingCreditBalanceTransactionId>,
41    livemode: Option<bool>,
42    test_clock: Option<Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>>,
43    type_: Option<Option<BillingCreditBalanceTransactionType>>,
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 BillingCreditBalanceTransaction {
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<BillingCreditBalanceTransaction>,
70        builder: BillingCreditBalanceTransactionBuilder,
71    }
72
73    impl Visitor for Place<BillingCreditBalanceTransaction> {
74        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
75            Ok(Box::new(Builder {
76                out: &mut self.out,
77                builder: BillingCreditBalanceTransactionBuilder::deser_default(),
78            }))
79        }
80    }
81
82    impl MapBuilder for BillingCreditBalanceTransactionBuilder {
83        type Out = BillingCreditBalanceTransaction;
84        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
85            Ok(match k {
86                "created" => Deserialize::begin(&mut self.created),
87                "credit" => Deserialize::begin(&mut self.credit),
88                "credit_grant" => Deserialize::begin(&mut self.credit_grant),
89                "debit" => Deserialize::begin(&mut self.debit),
90                "effective_at" => Deserialize::begin(&mut self.effective_at),
91                "id" => Deserialize::begin(&mut self.id),
92                "livemode" => Deserialize::begin(&mut self.livemode),
93                "test_clock" => Deserialize::begin(&mut self.test_clock),
94                "type" => Deserialize::begin(&mut self.type_),
95                _ => <dyn Visitor>::ignore(),
96            })
97        }
98
99        fn deser_default() -> Self {
100            Self {
101                created: None,
102                credit: Some(None),
103                credit_grant: None,
104                debit: Some(None),
105                effective_at: None,
106                id: None,
107                livemode: None,
108                test_clock: Some(None),
109                type_: Some(None),
110            }
111        }
112
113        fn take_out(&mut self) -> Option<Self::Out> {
114            let (
115                Some(created),
116                Some(credit),
117                Some(credit_grant),
118                Some(debit),
119                Some(effective_at),
120                Some(id),
121                Some(livemode),
122                Some(test_clock),
123                Some(type_),
124            ) = (
125                self.created,
126                self.credit.take(),
127                self.credit_grant.take(),
128                self.debit.take(),
129                self.effective_at,
130                self.id.take(),
131                self.livemode,
132                self.test_clock.take(),
133                self.type_.take(),
134            )
135            else {
136                return None;
137            };
138            Some(Self::Out {
139                created,
140                credit,
141                credit_grant,
142                debit,
143                effective_at,
144                id,
145                livemode,
146                test_clock,
147                type_,
148            })
149        }
150    }
151
152    impl Map for Builder<'_> {
153        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
154            self.builder.key(k)
155        }
156
157        fn finish(&mut self) -> Result<()> {
158            *self.out = self.builder.take_out();
159            Ok(())
160        }
161    }
162
163    impl ObjectDeser for BillingCreditBalanceTransaction {
164        type Builder = BillingCreditBalanceTransactionBuilder;
165    }
166
167    impl FromValueOpt for BillingCreditBalanceTransaction {
168        fn from_value(v: Value) -> Option<Self> {
169            let Value::Object(obj) = v else {
170                return None;
171            };
172            let mut b = BillingCreditBalanceTransactionBuilder::deser_default();
173            for (k, v) in obj {
174                match k.as_str() {
175                    "created" => b.created = FromValueOpt::from_value(v),
176                    "credit" => b.credit = FromValueOpt::from_value(v),
177                    "credit_grant" => b.credit_grant = FromValueOpt::from_value(v),
178                    "debit" => b.debit = FromValueOpt::from_value(v),
179                    "effective_at" => b.effective_at = FromValueOpt::from_value(v),
180                    "id" => b.id = FromValueOpt::from_value(v),
181                    "livemode" => b.livemode = FromValueOpt::from_value(v),
182                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
183                    "type" => b.type_ = FromValueOpt::from_value(v),
184                    _ => {}
185                }
186            }
187            b.take_out()
188        }
189    }
190};
191#[cfg(feature = "serialize")]
192impl serde::Serialize for BillingCreditBalanceTransaction {
193    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
194        use serde::ser::SerializeStruct;
195        let mut s = s.serialize_struct("BillingCreditBalanceTransaction", 10)?;
196        s.serialize_field("created", &self.created)?;
197        s.serialize_field("credit", &self.credit)?;
198        s.serialize_field("credit_grant", &self.credit_grant)?;
199        s.serialize_field("debit", &self.debit)?;
200        s.serialize_field("effective_at", &self.effective_at)?;
201        s.serialize_field("id", &self.id)?;
202        s.serialize_field("livemode", &self.livemode)?;
203        s.serialize_field("test_clock", &self.test_clock)?;
204        s.serialize_field("type", &self.type_)?;
205
206        s.serialize_field("object", "billing.credit_balance_transaction")?;
207        s.end()
208    }
209}
210/// The type of credit balance transaction (credit or debit).
211#[derive(Clone, Eq, PartialEq)]
212#[non_exhaustive]
213pub enum BillingCreditBalanceTransactionType {
214    Credit,
215    Debit,
216    /// An unrecognized value from Stripe. Should not be used as a request parameter.
217    Unknown(String),
218}
219impl BillingCreditBalanceTransactionType {
220    pub fn as_str(&self) -> &str {
221        use BillingCreditBalanceTransactionType::*;
222        match self {
223            Credit => "credit",
224            Debit => "debit",
225            Unknown(v) => v,
226        }
227    }
228}
229
230impl std::str::FromStr for BillingCreditBalanceTransactionType {
231    type Err = std::convert::Infallible;
232    fn from_str(s: &str) -> Result<Self, Self::Err> {
233        use BillingCreditBalanceTransactionType::*;
234        match s {
235            "credit" => Ok(Credit),
236            "debit" => Ok(Debit),
237            v => {
238                tracing::warn!(
239                    "Unknown value '{}' for enum '{}'",
240                    v,
241                    "BillingCreditBalanceTransactionType"
242                );
243                Ok(Unknown(v.to_owned()))
244            }
245        }
246    }
247}
248impl std::fmt::Display for BillingCreditBalanceTransactionType {
249    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
250        f.write_str(self.as_str())
251    }
252}
253
254#[cfg(not(feature = "redact-generated-debug"))]
255impl std::fmt::Debug for BillingCreditBalanceTransactionType {
256    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
257        f.write_str(self.as_str())
258    }
259}
260#[cfg(feature = "redact-generated-debug")]
261impl std::fmt::Debug for BillingCreditBalanceTransactionType {
262    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
263        f.debug_struct(stringify!(BillingCreditBalanceTransactionType)).finish_non_exhaustive()
264    }
265}
266#[cfg(feature = "serialize")]
267impl serde::Serialize for BillingCreditBalanceTransactionType {
268    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
269    where
270        S: serde::Serializer,
271    {
272        serializer.serialize_str(self.as_str())
273    }
274}
275impl miniserde::Deserialize for BillingCreditBalanceTransactionType {
276    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
277        crate::Place::new(out)
278    }
279}
280
281impl miniserde::de::Visitor for crate::Place<BillingCreditBalanceTransactionType> {
282    fn string(&mut self, s: &str) -> miniserde::Result<()> {
283        use std::str::FromStr;
284        self.out = Some(BillingCreditBalanceTransactionType::from_str(s).expect("infallible"));
285        Ok(())
286    }
287}
288
289stripe_types::impl_from_val_with_from_str!(BillingCreditBalanceTransactionType);
290#[cfg(feature = "deserialize")]
291impl<'de> serde::Deserialize<'de> for BillingCreditBalanceTransactionType {
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        Ok(Self::from_str(&s).expect("infallible"))
296    }
297}
298impl stripe_types::Object for BillingCreditBalanceTransaction {
299    type Id = stripe_shared::BillingCreditBalanceTransactionId;
300    fn id(&self) -> &Self::Id {
301        &self.id
302    }
303
304    fn into_id(self) -> Self::Id {
305        self.id
306    }
307}
308stripe_types::def_id!(BillingCreditBalanceTransactionId);