stripe_shared/
plan_tier.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct PlanTier {
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 PlanTierBuilder {
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::{make_place, Deserialize, Result};
36    use stripe_types::miniserde_helpers::FromValueOpt;
37    use stripe_types::{MapBuilder, ObjectDeser};
38
39    make_place!(Place);
40
41    impl Deserialize for PlanTier {
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<PlanTier>,
49        builder: PlanTierBuilder,
50    }
51
52    impl Visitor for Place<PlanTier> {
53        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
54            Ok(Box::new(Builder { out: &mut self.out, builder: PlanTierBuilder::deser_default() }))
55        }
56    }
57
58    impl MapBuilder for PlanTierBuilder {
59        type Out = PlanTier;
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
68                _ => <dyn Visitor>::ignore(),
69            })
70        }
71
72        fn deser_default() -> Self {
73            Self {
74                flat_amount: Deserialize::default(),
75                flat_amount_decimal: Deserialize::default(),
76                unit_amount: Deserialize::default(),
77                unit_amount_decimal: Deserialize::default(),
78                up_to: Deserialize::default(),
79            }
80        }
81
82        fn take_out(&mut self) -> Option<Self::Out> {
83            let (
84                Some(flat_amount),
85                Some(flat_amount_decimal),
86                Some(unit_amount),
87                Some(unit_amount_decimal),
88                Some(up_to),
89            ) = (
90                self.flat_amount,
91                self.flat_amount_decimal.take(),
92                self.unit_amount,
93                self.unit_amount_decimal.take(),
94                self.up_to,
95            )
96            else {
97                return None;
98            };
99            Some(Self::Out {
100                flat_amount,
101                flat_amount_decimal,
102                unit_amount,
103                unit_amount_decimal,
104                up_to,
105            })
106        }
107    }
108
109    impl Map for Builder<'_> {
110        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
111            self.builder.key(k)
112        }
113
114        fn finish(&mut self) -> Result<()> {
115            *self.out = self.builder.take_out();
116            Ok(())
117        }
118    }
119
120    impl ObjectDeser for PlanTier {
121        type Builder = PlanTierBuilder;
122    }
123
124    impl FromValueOpt for PlanTier {
125        fn from_value(v: Value) -> Option<Self> {
126            let Value::Object(obj) = v else {
127                return None;
128            };
129            let mut b = PlanTierBuilder::deser_default();
130            for (k, v) in obj {
131                match k.as_str() {
132                    "flat_amount" => b.flat_amount = FromValueOpt::from_value(v),
133                    "flat_amount_decimal" => b.flat_amount_decimal = FromValueOpt::from_value(v),
134                    "unit_amount" => b.unit_amount = FromValueOpt::from_value(v),
135                    "unit_amount_decimal" => b.unit_amount_decimal = FromValueOpt::from_value(v),
136                    "up_to" => b.up_to = FromValueOpt::from_value(v),
137
138                    _ => {}
139                }
140            }
141            b.take_out()
142        }
143    }
144};