stripe_misc/
bank_connections_resource_balance.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct BankConnectionsResourceBalance {
5    /// The time that the external institution calculated this balance.
6    /// Measured in seconds since the Unix epoch.
7    pub as_of: stripe_types::Timestamp,
8    pub cash: Option<stripe_misc::BankConnectionsResourceBalanceApiResourceCashBalance>,
9    pub credit: Option<stripe_misc::BankConnectionsResourceBalanceApiResourceCreditBalance>,
10    /// The balances owed to (or by) the account holder, before subtracting any outbound pending transactions or adding any inbound pending transactions.
11    ///
12    /// Each key is a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
13    ///
14    /// Each value is a integer amount.
15    /// A positive amount indicates money owed to the account holder.
16    /// A negative amount indicates money owed by the account holder.
17    pub current: std::collections::HashMap<String, i64>,
18    /// The `type` of the balance.
19    /// An additional hash is included on the balance with a name matching this value.
20    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
21    pub type_: BankConnectionsResourceBalanceType,
22}
23#[doc(hidden)]
24pub struct BankConnectionsResourceBalanceBuilder {
25    as_of: Option<stripe_types::Timestamp>,
26    cash: Option<Option<stripe_misc::BankConnectionsResourceBalanceApiResourceCashBalance>>,
27    credit: Option<Option<stripe_misc::BankConnectionsResourceBalanceApiResourceCreditBalance>>,
28    current: Option<std::collections::HashMap<String, i64>>,
29    type_: Option<BankConnectionsResourceBalanceType>,
30}
31
32#[allow(
33    unused_variables,
34    irrefutable_let_patterns,
35    clippy::let_unit_value,
36    clippy::match_single_binding,
37    clippy::single_match
38)]
39const _: () = {
40    use miniserde::de::{Map, Visitor};
41    use miniserde::json::Value;
42    use miniserde::{Deserialize, Result, make_place};
43    use stripe_types::miniserde_helpers::FromValueOpt;
44    use stripe_types::{MapBuilder, ObjectDeser};
45
46    make_place!(Place);
47
48    impl Deserialize for BankConnectionsResourceBalance {
49        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
50            Place::new(out)
51        }
52    }
53
54    struct Builder<'a> {
55        out: &'a mut Option<BankConnectionsResourceBalance>,
56        builder: BankConnectionsResourceBalanceBuilder,
57    }
58
59    impl Visitor for Place<BankConnectionsResourceBalance> {
60        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
61            Ok(Box::new(Builder {
62                out: &mut self.out,
63                builder: BankConnectionsResourceBalanceBuilder::deser_default(),
64            }))
65        }
66    }
67
68    impl MapBuilder for BankConnectionsResourceBalanceBuilder {
69        type Out = BankConnectionsResourceBalance;
70        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
71            Ok(match k {
72                "as_of" => Deserialize::begin(&mut self.as_of),
73                "cash" => Deserialize::begin(&mut self.cash),
74                "credit" => Deserialize::begin(&mut self.credit),
75                "current" => Deserialize::begin(&mut self.current),
76                "type" => Deserialize::begin(&mut self.type_),
77                _ => <dyn Visitor>::ignore(),
78            })
79        }
80
81        fn deser_default() -> Self {
82            Self {
83                as_of: Deserialize::default(),
84                cash: Deserialize::default(),
85                credit: Deserialize::default(),
86                current: Deserialize::default(),
87                type_: Deserialize::default(),
88            }
89        }
90
91        fn take_out(&mut self) -> Option<Self::Out> {
92            let (Some(as_of), Some(cash), Some(credit), Some(current), Some(type_)) = (
93                self.as_of,
94                self.cash.take(),
95                self.credit.take(),
96                self.current.take(),
97                self.type_.take(),
98            ) else {
99                return None;
100            };
101            Some(Self::Out { as_of, cash, credit, current, type_ })
102        }
103    }
104
105    impl Map for Builder<'_> {
106        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
107            self.builder.key(k)
108        }
109
110        fn finish(&mut self) -> Result<()> {
111            *self.out = self.builder.take_out();
112            Ok(())
113        }
114    }
115
116    impl ObjectDeser for BankConnectionsResourceBalance {
117        type Builder = BankConnectionsResourceBalanceBuilder;
118    }
119
120    impl FromValueOpt for BankConnectionsResourceBalance {
121        fn from_value(v: Value) -> Option<Self> {
122            let Value::Object(obj) = v else {
123                return None;
124            };
125            let mut b = BankConnectionsResourceBalanceBuilder::deser_default();
126            for (k, v) in obj {
127                match k.as_str() {
128                    "as_of" => b.as_of = FromValueOpt::from_value(v),
129                    "cash" => b.cash = FromValueOpt::from_value(v),
130                    "credit" => b.credit = FromValueOpt::from_value(v),
131                    "current" => b.current = FromValueOpt::from_value(v),
132                    "type" => b.type_ = FromValueOpt::from_value(v),
133                    _ => {}
134                }
135            }
136            b.take_out()
137        }
138    }
139};
140/// The `type` of the balance.
141/// An additional hash is included on the balance with a name matching this value.
142#[derive(Clone, Eq, PartialEq)]
143#[non_exhaustive]
144pub enum BankConnectionsResourceBalanceType {
145    Cash,
146    Credit,
147    /// An unrecognized value from Stripe. Should not be used as a request parameter.
148    Unknown(String),
149}
150impl BankConnectionsResourceBalanceType {
151    pub fn as_str(&self) -> &str {
152        use BankConnectionsResourceBalanceType::*;
153        match self {
154            Cash => "cash",
155            Credit => "credit",
156            Unknown(v) => v,
157        }
158    }
159}
160
161impl std::str::FromStr for BankConnectionsResourceBalanceType {
162    type Err = std::convert::Infallible;
163    fn from_str(s: &str) -> Result<Self, Self::Err> {
164        use BankConnectionsResourceBalanceType::*;
165        match s {
166            "cash" => Ok(Cash),
167            "credit" => Ok(Credit),
168            v => {
169                tracing::warn!(
170                    "Unknown value '{}' for enum '{}'",
171                    v,
172                    "BankConnectionsResourceBalanceType"
173                );
174                Ok(Unknown(v.to_owned()))
175            }
176        }
177    }
178}
179impl std::fmt::Display for BankConnectionsResourceBalanceType {
180    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
181        f.write_str(self.as_str())
182    }
183}
184
185impl std::fmt::Debug for BankConnectionsResourceBalanceType {
186    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
187        f.write_str(self.as_str())
188    }
189}
190#[cfg(feature = "serialize")]
191impl serde::Serialize for BankConnectionsResourceBalanceType {
192    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
193    where
194        S: serde::Serializer,
195    {
196        serializer.serialize_str(self.as_str())
197    }
198}
199impl miniserde::Deserialize for BankConnectionsResourceBalanceType {
200    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
201        crate::Place::new(out)
202    }
203}
204
205impl miniserde::de::Visitor for crate::Place<BankConnectionsResourceBalanceType> {
206    fn string(&mut self, s: &str) -> miniserde::Result<()> {
207        use std::str::FromStr;
208        self.out = Some(BankConnectionsResourceBalanceType::from_str(s).expect("infallible"));
209        Ok(())
210    }
211}
212
213stripe_types::impl_from_val_with_from_str!(BankConnectionsResourceBalanceType);
214#[cfg(feature = "deserialize")]
215impl<'de> serde::Deserialize<'de> for BankConnectionsResourceBalanceType {
216    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
217        use std::str::FromStr;
218        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
219        Ok(Self::from_str(&s).expect("infallible"))
220    }
221}