stripe_shared/
price_tier.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct PriceTier {
5    /// Price for the entire tier.
6    pub flat_amount: Option<i64>,
7    /// Same as `flat_amount`, but contains a decimal value with at most 12 decimal places.
8    pub flat_amount_decimal: Option<String>,
9    /// Per unit price for units relevant to the tier.
10    pub unit_amount: Option<i64>,
11    /// Same as `unit_amount`, but contains a decimal value with at most 12 decimal places.
12    pub unit_amount_decimal: Option<String>,
13    /// Up to and including to this quantity will be contained in the tier.
14    pub up_to: Option<i64>,
15}
16#[doc(hidden)]
17pub struct PriceTierBuilder {
18    flat_amount: Option<Option<i64>>,
19    flat_amount_decimal: Option<Option<String>>,
20    unit_amount: Option<Option<i64>>,
21    unit_amount_decimal: Option<Option<String>>,
22    up_to: Option<Option<i64>>,
23}
24
25#[allow(
26    unused_variables,
27    irrefutable_let_patterns,
28    clippy::let_unit_value,
29    clippy::match_single_binding,
30    clippy::single_match
31)]
32const _: () = {
33    use miniserde::de::{Map, Visitor};
34    use miniserde::json::Value;
35    use miniserde::{Deserialize, Result, make_place};
36    use stripe_types::miniserde_helpers::FromValueOpt;
37    use stripe_types::{MapBuilder, ObjectDeser};
38
39    make_place!(Place);
40
41    impl Deserialize for PriceTier {
42        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
43            Place::new(out)
44        }
45    }
46
47    struct Builder<'a> {
48        out: &'a mut Option<PriceTier>,
49        builder: PriceTierBuilder,
50    }
51
52    impl Visitor for Place<PriceTier> {
53        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
54            Ok(Box::new(Builder { out: &mut self.out, builder: PriceTierBuilder::deser_default() }))
55        }
56    }
57
58    impl MapBuilder for PriceTierBuilder {
59        type Out = PriceTier;
60        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
61            Ok(match k {
62                "flat_amount" => Deserialize::begin(&mut self.flat_amount),
63                "flat_amount_decimal" => Deserialize::begin(&mut self.flat_amount_decimal),
64                "unit_amount" => Deserialize::begin(&mut self.unit_amount),
65                "unit_amount_decimal" => Deserialize::begin(&mut self.unit_amount_decimal),
66                "up_to" => Deserialize::begin(&mut self.up_to),
67                _ => <dyn Visitor>::ignore(),
68            })
69        }
70
71        fn deser_default() -> Self {
72            Self {
73                flat_amount: Deserialize::default(),
74                flat_amount_decimal: Deserialize::default(),
75                unit_amount: Deserialize::default(),
76                unit_amount_decimal: Deserialize::default(),
77                up_to: Deserialize::default(),
78            }
79        }
80
81        fn take_out(&mut self) -> Option<Self::Out> {
82            let (
83                Some(flat_amount),
84                Some(flat_amount_decimal),
85                Some(unit_amount),
86                Some(unit_amount_decimal),
87                Some(up_to),
88            ) = (
89                self.flat_amount,
90                self.flat_amount_decimal.take(),
91                self.unit_amount,
92                self.unit_amount_decimal.take(),
93                self.up_to,
94            )
95            else {
96                return None;
97            };
98            Some(Self::Out {
99                flat_amount,
100                flat_amount_decimal,
101                unit_amount,
102                unit_amount_decimal,
103                up_to,
104            })
105        }
106    }
107
108    impl Map for Builder<'_> {
109        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
110            self.builder.key(k)
111        }
112
113        fn finish(&mut self) -> Result<()> {
114            *self.out = self.builder.take_out();
115            Ok(())
116        }
117    }
118
119    impl ObjectDeser for PriceTier {
120        type Builder = PriceTierBuilder;
121    }
122
123    impl FromValueOpt for PriceTier {
124        fn from_value(v: Value) -> Option<Self> {
125            let Value::Object(obj) = v else {
126                return None;
127            };
128            let mut b = PriceTierBuilder::deser_default();
129            for (k, v) in obj {
130                match k.as_str() {
131                    "flat_amount" => b.flat_amount = FromValueOpt::from_value(v),
132                    "flat_amount_decimal" => b.flat_amount_decimal = FromValueOpt::from_value(v),
133                    "unit_amount" => b.unit_amount = FromValueOpt::from_value(v),
134                    "unit_amount_decimal" => b.unit_amount_decimal = FromValueOpt::from_value(v),
135                    "up_to" => b.up_to = FromValueOpt::from_value(v),
136                    _ => {}
137                }
138            }
139            b.take_out()
140        }
141    }
142};