stripe_shared/
funding_instructions.rs

1/// Each customer has a [`balance`](https://stripe.com/docs/api/customers/object#customer_object-balance) that is.
2/// automatically applied to future invoices and payments using the `customer_balance` payment method.
3/// Customers can fund this balance by initiating a bank transfer to any account in the
4/// `financial_addresses` field.
5/// Related guide: [Customer balance funding instructions](https://stripe.com/docs/payments/customer-balance/funding-instructions).
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct FundingInstructions {
9    pub bank_transfer: stripe_shared::FundingInstructionsBankTransfer,
10    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
11    /// Must be a [supported currency](https://stripe.com/docs/currencies).
12    pub currency: stripe_types::Currency,
13    /// The `funding_type` of the returned instructions
14    pub funding_type: FundingInstructionsFundingType,
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}
18#[doc(hidden)]
19pub struct FundingInstructionsBuilder {
20    bank_transfer: Option<stripe_shared::FundingInstructionsBankTransfer>,
21    currency: Option<stripe_types::Currency>,
22    funding_type: Option<FundingInstructionsFundingType>,
23    livemode: Option<bool>,
24}
25
26#[allow(
27    unused_variables,
28    irrefutable_let_patterns,
29    clippy::let_unit_value,
30    clippy::match_single_binding,
31    clippy::single_match
32)]
33const _: () = {
34    use miniserde::de::{Map, Visitor};
35    use miniserde::json::Value;
36    use miniserde::{Deserialize, Result, make_place};
37    use stripe_types::miniserde_helpers::FromValueOpt;
38    use stripe_types::{MapBuilder, ObjectDeser};
39
40    make_place!(Place);
41
42    impl Deserialize for FundingInstructions {
43        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
44            Place::new(out)
45        }
46    }
47
48    struct Builder<'a> {
49        out: &'a mut Option<FundingInstructions>,
50        builder: FundingInstructionsBuilder,
51    }
52
53    impl Visitor for Place<FundingInstructions> {
54        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
55            Ok(Box::new(Builder {
56                out: &mut self.out,
57                builder: FundingInstructionsBuilder::deser_default(),
58            }))
59        }
60    }
61
62    impl MapBuilder for FundingInstructionsBuilder {
63        type Out = FundingInstructions;
64        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
65            Ok(match k {
66                "bank_transfer" => Deserialize::begin(&mut self.bank_transfer),
67                "currency" => Deserialize::begin(&mut self.currency),
68                "funding_type" => Deserialize::begin(&mut self.funding_type),
69                "livemode" => Deserialize::begin(&mut self.livemode),
70                _ => <dyn Visitor>::ignore(),
71            })
72        }
73
74        fn deser_default() -> Self {
75            Self {
76                bank_transfer: Deserialize::default(),
77                currency: Deserialize::default(),
78                funding_type: Deserialize::default(),
79                livemode: Deserialize::default(),
80            }
81        }
82
83        fn take_out(&mut self) -> Option<Self::Out> {
84            let (Some(bank_transfer), Some(currency), Some(funding_type), Some(livemode)) = (
85                self.bank_transfer.take(),
86                self.currency.take(),
87                self.funding_type.take(),
88                self.livemode,
89            ) else {
90                return None;
91            };
92            Some(Self::Out { bank_transfer, currency, funding_type, livemode })
93        }
94    }
95
96    impl Map for Builder<'_> {
97        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
98            self.builder.key(k)
99        }
100
101        fn finish(&mut self) -> Result<()> {
102            *self.out = self.builder.take_out();
103            Ok(())
104        }
105    }
106
107    impl ObjectDeser for FundingInstructions {
108        type Builder = FundingInstructionsBuilder;
109    }
110
111    impl FromValueOpt for FundingInstructions {
112        fn from_value(v: Value) -> Option<Self> {
113            let Value::Object(obj) = v else {
114                return None;
115            };
116            let mut b = FundingInstructionsBuilder::deser_default();
117            for (k, v) in obj {
118                match k.as_str() {
119                    "bank_transfer" => b.bank_transfer = FromValueOpt::from_value(v),
120                    "currency" => b.currency = FromValueOpt::from_value(v),
121                    "funding_type" => b.funding_type = FromValueOpt::from_value(v),
122                    "livemode" => b.livemode = FromValueOpt::from_value(v),
123                    _ => {}
124                }
125            }
126            b.take_out()
127        }
128    }
129};
130#[cfg(feature = "serialize")]
131impl serde::Serialize for FundingInstructions {
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("FundingInstructions", 5)?;
135        s.serialize_field("bank_transfer", &self.bank_transfer)?;
136        s.serialize_field("currency", &self.currency)?;
137        s.serialize_field("funding_type", &self.funding_type)?;
138        s.serialize_field("livemode", &self.livemode)?;
139
140        s.serialize_field("object", "funding_instructions")?;
141        s.end()
142    }
143}
144/// The `funding_type` of the returned instructions
145#[derive(Clone, Eq, PartialEq)]
146#[non_exhaustive]
147pub enum FundingInstructionsFundingType {
148    BankTransfer,
149    /// An unrecognized value from Stripe. Should not be used as a request parameter.
150    Unknown(String),
151}
152impl FundingInstructionsFundingType {
153    pub fn as_str(&self) -> &str {
154        use FundingInstructionsFundingType::*;
155        match self {
156            BankTransfer => "bank_transfer",
157            Unknown(v) => v,
158        }
159    }
160}
161
162impl std::str::FromStr for FundingInstructionsFundingType {
163    type Err = std::convert::Infallible;
164    fn from_str(s: &str) -> Result<Self, Self::Err> {
165        use FundingInstructionsFundingType::*;
166        match s {
167            "bank_transfer" => Ok(BankTransfer),
168            v => {
169                tracing::warn!(
170                    "Unknown value '{}' for enum '{}'",
171                    v,
172                    "FundingInstructionsFundingType"
173                );
174                Ok(Unknown(v.to_owned()))
175            }
176        }
177    }
178}
179impl std::fmt::Display for FundingInstructionsFundingType {
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 FundingInstructionsFundingType {
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 FundingInstructionsFundingType {
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 FundingInstructionsFundingType {
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<FundingInstructionsFundingType> {
206    fn string(&mut self, s: &str) -> miniserde::Result<()> {
207        use std::str::FromStr;
208        self.out = Some(FundingInstructionsFundingType::from_str(s).expect("infallible"));
209        Ok(())
210    }
211}
212
213stripe_types::impl_from_val_with_from_str!(FundingInstructionsFundingType);
214#[cfg(feature = "deserialize")]
215impl<'de> serde::Deserialize<'de> for FundingInstructionsFundingType {
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}