Skip to main content

stripe_shared/
transfer_data.rs

1#[derive(Clone)]
2#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
3#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct TransferData {
6    /// The amount transferred to the destination account.
7    /// This transfer will occur automatically after the payment succeeds.
8    /// If no amount is specified, by default the entire payment amount is transferred to the destination account.
9    /// The amount must be less than or equal to the [amount](https://docs.stripe.com/api/payment_intents/object#payment_intent_object-amount), and must be a positive integer.
10    ///  representing how much to transfer in the smallest currency unit (e.g., 100 cents to charge $1.00).
11    pub amount: Option<i64>,
12    /// The account (if any) that the payment is attributed to for tax reporting, and where funds from the payment are transferred to after payment success.
13    pub destination: stripe_types::Expandable<stripe_shared::Account>,
14}
15#[cfg(feature = "redact-generated-debug")]
16impl std::fmt::Debug for TransferData {
17    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18        f.debug_struct("TransferData").finish_non_exhaustive()
19    }
20}
21#[doc(hidden)]
22pub struct TransferDataBuilder {
23    amount: Option<Option<i64>>,
24    destination: Option<stripe_types::Expandable<stripe_shared::Account>>,
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::{Deserialize, Result, make_place};
38    use stripe_types::miniserde_helpers::FromValueOpt;
39    use stripe_types::{MapBuilder, ObjectDeser};
40
41    make_place!(Place);
42
43    impl Deserialize for TransferData {
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<TransferData>,
51        builder: TransferDataBuilder,
52    }
53
54    impl Visitor for Place<TransferData> {
55        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
56            Ok(Box::new(Builder {
57                out: &mut self.out,
58                builder: TransferDataBuilder::deser_default(),
59            }))
60        }
61    }
62
63    impl MapBuilder for TransferDataBuilder {
64        type Out = TransferData;
65        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
66            Ok(match k {
67                "amount" => Deserialize::begin(&mut self.amount),
68                "destination" => Deserialize::begin(&mut self.destination),
69                _ => <dyn Visitor>::ignore(),
70            })
71        }
72
73        fn deser_default() -> Self {
74            Self { amount: Some(None), destination: None }
75        }
76
77        fn take_out(&mut self) -> Option<Self::Out> {
78            let (Some(amount), Some(destination)) = (self.amount, self.destination.take()) else {
79                return None;
80            };
81            Some(Self::Out { amount, destination })
82        }
83    }
84
85    impl Map for Builder<'_> {
86        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
87            self.builder.key(k)
88        }
89
90        fn finish(&mut self) -> Result<()> {
91            *self.out = self.builder.take_out();
92            Ok(())
93        }
94    }
95
96    impl ObjectDeser for TransferData {
97        type Builder = TransferDataBuilder;
98    }
99
100    impl FromValueOpt for TransferData {
101        fn from_value(v: Value) -> Option<Self> {
102            let Value::Object(obj) = v else {
103                return None;
104            };
105            let mut b = TransferDataBuilder::deser_default();
106            for (k, v) in obj {
107                match k.as_str() {
108                    "amount" => b.amount = FromValueOpt::from_value(v),
109                    "destination" => b.destination = FromValueOpt::from_value(v),
110                    _ => {}
111                }
112            }
113            b.take_out()
114        }
115    }
116};