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://stripe.com/docs/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    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
16    pub livemode: bool,
17    pub settings: stripe_shared::CustomerBalanceCustomerBalanceSettings,
18}
19#[doc(hidden)]
20pub struct CashBalanceBuilder {
21    available: Option<Option<std::collections::HashMap<String, i64>>>,
22    customer: Option<String>,
23    livemode: Option<bool>,
24    settings: Option<stripe_shared::CustomerBalanceCustomerBalanceSettings>,
25}
26
27#[allow(
28    unused_variables,
29    irrefutable_let_patterns,
30    clippy::let_unit_value,
31    clippy::match_single_binding,
32    clippy::single_match
33)]
34const _: () = {
35    use miniserde::de::{Map, Visitor};
36    use miniserde::json::Value;
37    use miniserde::{make_place, Deserialize, Result};
38    use stripe_types::miniserde_helpers::FromValueOpt;
39    use stripe_types::{MapBuilder, ObjectDeser};
40
41    make_place!(Place);
42
43    impl Deserialize for CashBalance {
44        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
45            Place::new(out)
46        }
47    }
48
49    struct Builder<'a> {
50        out: &'a mut Option<CashBalance>,
51        builder: CashBalanceBuilder,
52    }
53
54    impl Visitor for Place<CashBalance> {
55        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
56            Ok(Box::new(Builder {
57                out: &mut self.out,
58                builder: CashBalanceBuilder::deser_default(),
59            }))
60        }
61    }
62
63    impl MapBuilder for CashBalanceBuilder {
64        type Out = CashBalance;
65        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
66            Ok(match k {
67                "available" => Deserialize::begin(&mut self.available),
68                "customer" => Deserialize::begin(&mut self.customer),
69                "livemode" => Deserialize::begin(&mut self.livemode),
70                "settings" => Deserialize::begin(&mut self.settings),
71
72                _ => <dyn Visitor>::ignore(),
73            })
74        }
75
76        fn deser_default() -> Self {
77            Self {
78                available: Deserialize::default(),
79                customer: Deserialize::default(),
80                livemode: Deserialize::default(),
81                settings: Deserialize::default(),
82            }
83        }
84
85        fn take_out(&mut self) -> Option<Self::Out> {
86            let (Some(available), Some(customer), Some(livemode), Some(settings)) =
87                (self.available.take(), self.customer.take(), self.livemode, self.settings)
88            else {
89                return None;
90            };
91            Some(Self::Out { available, customer, livemode, settings })
92        }
93    }
94
95    impl Map for Builder<'_> {
96        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
97            self.builder.key(k)
98        }
99
100        fn finish(&mut self) -> Result<()> {
101            *self.out = self.builder.take_out();
102            Ok(())
103        }
104    }
105
106    impl ObjectDeser for CashBalance {
107        type Builder = CashBalanceBuilder;
108    }
109
110    impl FromValueOpt for CashBalance {
111        fn from_value(v: Value) -> Option<Self> {
112            let Value::Object(obj) = v else {
113                return None;
114            };
115            let mut b = CashBalanceBuilder::deser_default();
116            for (k, v) in obj {
117                match k.as_str() {
118                    "available" => b.available = FromValueOpt::from_value(v),
119                    "customer" => b.customer = FromValueOpt::from_value(v),
120                    "livemode" => b.livemode = FromValueOpt::from_value(v),
121                    "settings" => b.settings = FromValueOpt::from_value(v),
122
123                    _ => {}
124                }
125            }
126            b.take_out()
127        }
128    }
129};
130#[cfg(feature = "serialize")]
131impl serde::Serialize for CashBalance {
132    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
133        use serde::ser::SerializeStruct;
134        let mut s = s.serialize_struct("CashBalance", 5)?;
135        s.serialize_field("available", &self.available)?;
136        s.serialize_field("customer", &self.customer)?;
137        s.serialize_field("livemode", &self.livemode)?;
138        s.serialize_field("settings", &self.settings)?;
139
140        s.serialize_field("object", "cash_balance")?;
141        s.end()
142    }
143}