stripe_shared/
cash_balance.rs

1/// A customer's `Cash balance` represents real funds.
2/// Customers can add funds to their cash balance by sending a bank transfer.
3/// These funds can be used for payment and can eventually be paid out to your bank account.
4///
5/// For more details see <<https://stripe.com/docs/api/cash_balance/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct CashBalance {
9    /// A hash of all cash balances available to this customer.
10    /// You cannot delete a customer with any cash balances, even if the balance is 0.
11    /// Amounts are represented in the [smallest currency unit](https://docs.stripe.com/currencies#zero-decimal).
12    pub available: Option<std::collections::HashMap<String, i64>>,
13    /// The ID of the customer whose cash balance this object represents.
14    pub customer: String,
15    /// The ID of an Account representing a customer whose cash balance this object represents.
16    pub customer_account: Option<String>,
17    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
18    pub livemode: bool,
19    pub settings: stripe_shared::CustomerBalanceCustomerBalanceSettings,
20}
21#[doc(hidden)]
22pub struct CashBalanceBuilder {
23    available: Option<Option<std::collections::HashMap<String, i64>>>,
24    customer: Option<String>,
25    customer_account: Option<Option<String>>,
26    livemode: Option<bool>,
27    settings: Option<stripe_shared::CustomerBalanceCustomerBalanceSettings>,
28}
29
30#[allow(
31    unused_variables,
32    irrefutable_let_patterns,
33    clippy::let_unit_value,
34    clippy::match_single_binding,
35    clippy::single_match
36)]
37const _: () = {
38    use miniserde::de::{Map, Visitor};
39    use miniserde::json::Value;
40    use miniserde::{Deserialize, Result, make_place};
41    use stripe_types::miniserde_helpers::FromValueOpt;
42    use stripe_types::{MapBuilder, ObjectDeser};
43
44    make_place!(Place);
45
46    impl Deserialize for CashBalance {
47        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
48            Place::new(out)
49        }
50    }
51
52    struct Builder<'a> {
53        out: &'a mut Option<CashBalance>,
54        builder: CashBalanceBuilder,
55    }
56
57    impl Visitor for Place<CashBalance> {
58        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
59            Ok(Box::new(Builder {
60                out: &mut self.out,
61                builder: CashBalanceBuilder::deser_default(),
62            }))
63        }
64    }
65
66    impl MapBuilder for CashBalanceBuilder {
67        type Out = CashBalance;
68        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
69            Ok(match k {
70                "available" => Deserialize::begin(&mut self.available),
71                "customer" => Deserialize::begin(&mut self.customer),
72                "customer_account" => Deserialize::begin(&mut self.customer_account),
73                "livemode" => Deserialize::begin(&mut self.livemode),
74                "settings" => Deserialize::begin(&mut self.settings),
75                _ => <dyn Visitor>::ignore(),
76            })
77        }
78
79        fn deser_default() -> Self {
80            Self {
81                available: Deserialize::default(),
82                customer: Deserialize::default(),
83                customer_account: Deserialize::default(),
84                livemode: Deserialize::default(),
85                settings: Deserialize::default(),
86            }
87        }
88
89        fn take_out(&mut self) -> Option<Self::Out> {
90            let (
91                Some(available),
92                Some(customer),
93                Some(customer_account),
94                Some(livemode),
95                Some(settings),
96            ) = (
97                self.available.take(),
98                self.customer.take(),
99                self.customer_account.take(),
100                self.livemode,
101                self.settings.take(),
102            )
103            else {
104                return None;
105            };
106            Some(Self::Out { available, customer, customer_account, livemode, settings })
107        }
108    }
109
110    impl Map for Builder<'_> {
111        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
112            self.builder.key(k)
113        }
114
115        fn finish(&mut self) -> Result<()> {
116            *self.out = self.builder.take_out();
117            Ok(())
118        }
119    }
120
121    impl ObjectDeser for CashBalance {
122        type Builder = CashBalanceBuilder;
123    }
124
125    impl FromValueOpt for CashBalance {
126        fn from_value(v: Value) -> Option<Self> {
127            let Value::Object(obj) = v else {
128                return None;
129            };
130            let mut b = CashBalanceBuilder::deser_default();
131            for (k, v) in obj {
132                match k.as_str() {
133                    "available" => b.available = FromValueOpt::from_value(v),
134                    "customer" => b.customer = FromValueOpt::from_value(v),
135                    "customer_account" => b.customer_account = FromValueOpt::from_value(v),
136                    "livemode" => b.livemode = FromValueOpt::from_value(v),
137                    "settings" => b.settings = FromValueOpt::from_value(v),
138                    _ => {}
139                }
140            }
141            b.take_out()
142        }
143    }
144};
145#[cfg(feature = "serialize")]
146impl serde::Serialize for CashBalance {
147    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
148        use serde::ser::SerializeStruct;
149        let mut s = s.serialize_struct("CashBalance", 6)?;
150        s.serialize_field("available", &self.available)?;
151        s.serialize_field("customer", &self.customer)?;
152        s.serialize_field("customer_account", &self.customer_account)?;
153        s.serialize_field("livemode", &self.livemode)?;
154        s.serialize_field("settings", &self.settings)?;
155
156        s.serialize_field("object", "cash_balance")?;
157        s.end()
158    }
159}