stripe_shared/
billing_credit_grants_resource_balance_debit.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct BillingCreditGrantsResourceBalanceDebit {
5    pub amount: stripe_shared::BillingCreditGrantsResourceAmount,
6    /// Details of how the billing credits were applied to an invoice.
7    /// Only present if `type` is `credits_applied`.
8    pub credits_applied: Option<stripe_shared::BillingCreditGrantsResourceBalanceCreditsApplied>,
9    /// The type of debit transaction.
10    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
11    pub type_: BillingCreditGrantsResourceBalanceDebitType,
12}
13#[doc(hidden)]
14pub struct BillingCreditGrantsResourceBalanceDebitBuilder {
15    amount: Option<stripe_shared::BillingCreditGrantsResourceAmount>,
16    credits_applied:
17        Option<Option<stripe_shared::BillingCreditGrantsResourceBalanceCreditsApplied>>,
18    type_: Option<BillingCreditGrantsResourceBalanceDebitType>,
19}
20
21#[allow(
22    unused_variables,
23    irrefutable_let_patterns,
24    clippy::let_unit_value,
25    clippy::match_single_binding,
26    clippy::single_match
27)]
28const _: () = {
29    use miniserde::de::{Map, Visitor};
30    use miniserde::json::Value;
31    use miniserde::{Deserialize, Result, make_place};
32    use stripe_types::miniserde_helpers::FromValueOpt;
33    use stripe_types::{MapBuilder, ObjectDeser};
34
35    make_place!(Place);
36
37    impl Deserialize for BillingCreditGrantsResourceBalanceDebit {
38        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
39            Place::new(out)
40        }
41    }
42
43    struct Builder<'a> {
44        out: &'a mut Option<BillingCreditGrantsResourceBalanceDebit>,
45        builder: BillingCreditGrantsResourceBalanceDebitBuilder,
46    }
47
48    impl Visitor for Place<BillingCreditGrantsResourceBalanceDebit> {
49        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
50            Ok(Box::new(Builder {
51                out: &mut self.out,
52                builder: BillingCreditGrantsResourceBalanceDebitBuilder::deser_default(),
53            }))
54        }
55    }
56
57    impl MapBuilder for BillingCreditGrantsResourceBalanceDebitBuilder {
58        type Out = BillingCreditGrantsResourceBalanceDebit;
59        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
60            Ok(match k {
61                "amount" => Deserialize::begin(&mut self.amount),
62                "credits_applied" => Deserialize::begin(&mut self.credits_applied),
63                "type" => Deserialize::begin(&mut self.type_),
64                _ => <dyn Visitor>::ignore(),
65            })
66        }
67
68        fn deser_default() -> Self {
69            Self {
70                amount: Deserialize::default(),
71                credits_applied: Deserialize::default(),
72                type_: Deserialize::default(),
73            }
74        }
75
76        fn take_out(&mut self) -> Option<Self::Out> {
77            let (Some(amount), Some(credits_applied), Some(type_)) =
78                (self.amount.take(), self.credits_applied.take(), self.type_.take())
79            else {
80                return None;
81            };
82            Some(Self::Out { amount, credits_applied, type_ })
83        }
84    }
85
86    impl Map for Builder<'_> {
87        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
88            self.builder.key(k)
89        }
90
91        fn finish(&mut self) -> Result<()> {
92            *self.out = self.builder.take_out();
93            Ok(())
94        }
95    }
96
97    impl ObjectDeser for BillingCreditGrantsResourceBalanceDebit {
98        type Builder = BillingCreditGrantsResourceBalanceDebitBuilder;
99    }
100
101    impl FromValueOpt for BillingCreditGrantsResourceBalanceDebit {
102        fn from_value(v: Value) -> Option<Self> {
103            let Value::Object(obj) = v else {
104                return None;
105            };
106            let mut b = BillingCreditGrantsResourceBalanceDebitBuilder::deser_default();
107            for (k, v) in obj {
108                match k.as_str() {
109                    "amount" => b.amount = FromValueOpt::from_value(v),
110                    "credits_applied" => b.credits_applied = FromValueOpt::from_value(v),
111                    "type" => b.type_ = FromValueOpt::from_value(v),
112                    _ => {}
113                }
114            }
115            b.take_out()
116        }
117    }
118};
119/// The type of debit transaction.
120#[derive(Clone, Eq, PartialEq)]
121#[non_exhaustive]
122pub enum BillingCreditGrantsResourceBalanceDebitType {
123    CreditsApplied,
124    CreditsExpired,
125    CreditsVoided,
126    /// An unrecognized value from Stripe. Should not be used as a request parameter.
127    Unknown(String),
128}
129impl BillingCreditGrantsResourceBalanceDebitType {
130    pub fn as_str(&self) -> &str {
131        use BillingCreditGrantsResourceBalanceDebitType::*;
132        match self {
133            CreditsApplied => "credits_applied",
134            CreditsExpired => "credits_expired",
135            CreditsVoided => "credits_voided",
136            Unknown(v) => v,
137        }
138    }
139}
140
141impl std::str::FromStr for BillingCreditGrantsResourceBalanceDebitType {
142    type Err = std::convert::Infallible;
143    fn from_str(s: &str) -> Result<Self, Self::Err> {
144        use BillingCreditGrantsResourceBalanceDebitType::*;
145        match s {
146            "credits_applied" => Ok(CreditsApplied),
147            "credits_expired" => Ok(CreditsExpired),
148            "credits_voided" => Ok(CreditsVoided),
149            v => {
150                tracing::warn!(
151                    "Unknown value '{}' for enum '{}'",
152                    v,
153                    "BillingCreditGrantsResourceBalanceDebitType"
154                );
155                Ok(Unknown(v.to_owned()))
156            }
157        }
158    }
159}
160impl std::fmt::Display for BillingCreditGrantsResourceBalanceDebitType {
161    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
162        f.write_str(self.as_str())
163    }
164}
165
166impl std::fmt::Debug for BillingCreditGrantsResourceBalanceDebitType {
167    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
168        f.write_str(self.as_str())
169    }
170}
171#[cfg(feature = "serialize")]
172impl serde::Serialize for BillingCreditGrantsResourceBalanceDebitType {
173    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
174    where
175        S: serde::Serializer,
176    {
177        serializer.serialize_str(self.as_str())
178    }
179}
180impl miniserde::Deserialize for BillingCreditGrantsResourceBalanceDebitType {
181    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
182        crate::Place::new(out)
183    }
184}
185
186impl miniserde::de::Visitor for crate::Place<BillingCreditGrantsResourceBalanceDebitType> {
187    fn string(&mut self, s: &str) -> miniserde::Result<()> {
188        use std::str::FromStr;
189        self.out =
190            Some(BillingCreditGrantsResourceBalanceDebitType::from_str(s).expect("infallible"));
191        Ok(())
192    }
193}
194
195stripe_types::impl_from_val_with_from_str!(BillingCreditGrantsResourceBalanceDebitType);
196#[cfg(feature = "deserialize")]
197impl<'de> serde::Deserialize<'de> for BillingCreditGrantsResourceBalanceDebitType {
198    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
199        use std::str::FromStr;
200        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
201        Ok(Self::from_str(&s).expect("infallible"))
202    }
203}