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::{make_place, Deserialize, Result};
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
71                _ => <dyn Visitor>::ignore(),
72            })
73        }
74
75        fn deser_default() -> Self {
76            Self {
77                bank_transfer: Deserialize::default(),
78                currency: Deserialize::default(),
79                funding_type: Deserialize::default(),
80                livemode: Deserialize::default(),
81            }
82        }
83
84        fn take_out(&mut self) -> Option<Self::Out> {
85            let (Some(bank_transfer), Some(currency), Some(funding_type), Some(livemode)) =
86                (self.bank_transfer.take(), self.currency, self.funding_type, self.livemode)
87            else {
88                return None;
89            };
90            Some(Self::Out { bank_transfer, currency, funding_type, livemode })
91        }
92    }
93
94    impl<'a> Map for Builder<'a> {
95        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
96            self.builder.key(k)
97        }
98
99        fn finish(&mut self) -> Result<()> {
100            *self.out = self.builder.take_out();
101            Ok(())
102        }
103    }
104
105    impl ObjectDeser for FundingInstructions {
106        type Builder = FundingInstructionsBuilder;
107    }
108
109    impl FromValueOpt for FundingInstructions {
110        fn from_value(v: Value) -> Option<Self> {
111            let Value::Object(obj) = v else {
112                return None;
113            };
114            let mut b = FundingInstructionsBuilder::deser_default();
115            for (k, v) in obj {
116                match k.as_str() {
117                    "bank_transfer" => b.bank_transfer = FromValueOpt::from_value(v),
118                    "currency" => b.currency = FromValueOpt::from_value(v),
119                    "funding_type" => b.funding_type = FromValueOpt::from_value(v),
120                    "livemode" => b.livemode = FromValueOpt::from_value(v),
121
122                    _ => {}
123                }
124            }
125            b.take_out()
126        }
127    }
128};
129#[cfg(feature = "serialize")]
130impl serde::Serialize for FundingInstructions {
131    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
132        use serde::ser::SerializeStruct;
133        let mut s = s.serialize_struct("FundingInstructions", 5)?;
134        s.serialize_field("bank_transfer", &self.bank_transfer)?;
135        s.serialize_field("currency", &self.currency)?;
136        s.serialize_field("funding_type", &self.funding_type)?;
137        s.serialize_field("livemode", &self.livemode)?;
138
139        s.serialize_field("object", "funding_instructions")?;
140        s.end()
141    }
142}
143/// The `funding_type` of the returned instructions
144#[derive(Copy, Clone, Eq, PartialEq)]
145pub enum FundingInstructionsFundingType {
146    BankTransfer,
147}
148impl FundingInstructionsFundingType {
149    pub fn as_str(self) -> &'static str {
150        use FundingInstructionsFundingType::*;
151        match self {
152            BankTransfer => "bank_transfer",
153        }
154    }
155}
156
157impl std::str::FromStr for FundingInstructionsFundingType {
158    type Err = stripe_types::StripeParseError;
159    fn from_str(s: &str) -> Result<Self, Self::Err> {
160        use FundingInstructionsFundingType::*;
161        match s {
162            "bank_transfer" => Ok(BankTransfer),
163            _ => Err(stripe_types::StripeParseError),
164        }
165    }
166}
167impl std::fmt::Display for FundingInstructionsFundingType {
168    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
169        f.write_str(self.as_str())
170    }
171}
172
173impl std::fmt::Debug for FundingInstructionsFundingType {
174    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
175        f.write_str(self.as_str())
176    }
177}
178#[cfg(feature = "serialize")]
179impl serde::Serialize for FundingInstructionsFundingType {
180    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
181    where
182        S: serde::Serializer,
183    {
184        serializer.serialize_str(self.as_str())
185    }
186}
187impl miniserde::Deserialize for FundingInstructionsFundingType {
188    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
189        crate::Place::new(out)
190    }
191}
192
193impl miniserde::de::Visitor for crate::Place<FundingInstructionsFundingType> {
194    fn string(&mut self, s: &str) -> miniserde::Result<()> {
195        use std::str::FromStr;
196        self.out = Some(FundingInstructionsFundingType::from_str(s).map_err(|_| miniserde::Error)?);
197        Ok(())
198    }
199}
200
201stripe_types::impl_from_val_with_from_str!(FundingInstructionsFundingType);
202#[cfg(feature = "deserialize")]
203impl<'de> serde::Deserialize<'de> for FundingInstructionsFundingType {
204    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
205        use std::str::FromStr;
206        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
207        Self::from_str(&s).map_err(|_| {
208            serde::de::Error::custom("Unknown value for FundingInstructionsFundingType")
209        })
210    }
211}