stripe_shared/
fee.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct Fee {
5    /// Amount of the fee, in cents.
6    pub amount: i64,
7    /// ID of the Connect application that earned the fee.
8    pub application: Option<String>,
9    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
10    /// Must be a [supported currency](https://stripe.com/docs/currencies).
11    pub currency: stripe_types::Currency,
12    /// An arbitrary string attached to the object. Often useful for displaying to users.
13    pub description: Option<String>,
14    /// Type of the fee, one of: `application_fee`, `payment_method_passthrough_fee`, `stripe_fee` or `tax`.
15    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
16    pub type_: String,
17}
18#[doc(hidden)]
19pub struct FeeBuilder {
20    amount: Option<i64>,
21    application: Option<Option<String>>,
22    currency: Option<stripe_types::Currency>,
23    description: Option<Option<String>>,
24    type_: Option<String>,
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 Fee {
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<Fee>,
51        builder: FeeBuilder,
52    }
53
54    impl Visitor for Place<Fee> {
55        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
56            Ok(Box::new(Builder { out: &mut self.out, builder: FeeBuilder::deser_default() }))
57        }
58    }
59
60    impl MapBuilder for FeeBuilder {
61        type Out = Fee;
62        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
63            Ok(match k {
64                "amount" => Deserialize::begin(&mut self.amount),
65                "application" => Deserialize::begin(&mut self.application),
66                "currency" => Deserialize::begin(&mut self.currency),
67                "description" => Deserialize::begin(&mut self.description),
68                "type" => Deserialize::begin(&mut self.type_),
69                _ => <dyn Visitor>::ignore(),
70            })
71        }
72
73        fn deser_default() -> Self {
74            Self {
75                amount: Deserialize::default(),
76                application: Deserialize::default(),
77                currency: Deserialize::default(),
78                description: Deserialize::default(),
79                type_: Deserialize::default(),
80            }
81        }
82
83        fn take_out(&mut self) -> Option<Self::Out> {
84            let (Some(amount), Some(application), Some(currency), Some(description), Some(type_)) = (
85                self.amount,
86                self.application.take(),
87                self.currency.take(),
88                self.description.take(),
89                self.type_.take(),
90            ) else {
91                return None;
92            };
93            Some(Self::Out { amount, application, currency, description, type_ })
94        }
95    }
96
97    impl Map for Builder<'_> {
98        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
99            self.builder.key(k)
100        }
101
102        fn finish(&mut self) -> Result<()> {
103            *self.out = self.builder.take_out();
104            Ok(())
105        }
106    }
107
108    impl ObjectDeser for Fee {
109        type Builder = FeeBuilder;
110    }
111
112    impl FromValueOpt for Fee {
113        fn from_value(v: Value) -> Option<Self> {
114            let Value::Object(obj) = v else {
115                return None;
116            };
117            let mut b = FeeBuilder::deser_default();
118            for (k, v) in obj {
119                match k.as_str() {
120                    "amount" => b.amount = FromValueOpt::from_value(v),
121                    "application" => b.application = FromValueOpt::from_value(v),
122                    "currency" => b.currency = FromValueOpt::from_value(v),
123                    "description" => b.description = FromValueOpt::from_value(v),
124                    "type" => b.type_ = FromValueOpt::from_value(v),
125                    _ => {}
126                }
127            }
128            b.take_out()
129        }
130    }
131};