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::{make_place, Deserialize, Result};
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
70                _ => <dyn Visitor>::ignore(),
71            })
72        }
73
74        fn deser_default() -> Self {
75            Self {
76                amount: Deserialize::default(),
77                application: Deserialize::default(),
78                currency: Deserialize::default(),
79                description: Deserialize::default(),
80                type_: Deserialize::default(),
81            }
82        }
83
84        fn take_out(&mut self) -> Option<Self::Out> {
85            let (Some(amount), Some(application), Some(currency), Some(description), Some(type_)) = (
86                self.amount,
87                self.application.take(),
88                self.currency,
89                self.description.take(),
90                self.type_.take(),
91            ) else {
92                return None;
93            };
94            Some(Self::Out { amount, application, currency, description, type_ })
95        }
96    }
97
98    impl<'a> Map for Builder<'a> {
99        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
100            self.builder.key(k)
101        }
102
103        fn finish(&mut self) -> Result<()> {
104            *self.out = self.builder.take_out();
105            Ok(())
106        }
107    }
108
109    impl ObjectDeser for Fee {
110        type Builder = FeeBuilder;
111    }
112
113    impl FromValueOpt for Fee {
114        fn from_value(v: Value) -> Option<Self> {
115            let Value::Object(obj) = v else {
116                return None;
117            };
118            let mut b = FeeBuilder::deser_default();
119            for (k, v) in obj {
120                match k.as_str() {
121                    "amount" => b.amount = FromValueOpt::from_value(v),
122                    "application" => b.application = FromValueOpt::from_value(v),
123                    "currency" => b.currency = FromValueOpt::from_value(v),
124                    "description" => b.description = FromValueOpt::from_value(v),
125                    "type" => b.type_ = FromValueOpt::from_value(v),
126
127                    _ => {}
128                }
129            }
130            b.take_out()
131        }
132    }
133};