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