stripe_shared/
custom_unit_amount.rs

1#[derive(Copy, Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CustomUnitAmount {
5    /// The maximum unit amount the customer can specify for this item.
6    pub maximum: Option<i64>,
7    /// The minimum unit amount the customer can specify for this item.
8    /// Must be at least the minimum charge amount.
9    pub minimum: Option<i64>,
10    /// The starting unit amount which can be updated by the customer.
11    pub preset: Option<i64>,
12}
13#[doc(hidden)]
14pub struct CustomUnitAmountBuilder {
15    maximum: Option<Option<i64>>,
16    minimum: Option<Option<i64>>,
17    preset: Option<Option<i64>>,
18}
19
20#[allow(
21    unused_variables,
22    irrefutable_let_patterns,
23    clippy::let_unit_value,
24    clippy::match_single_binding,
25    clippy::single_match
26)]
27const _: () = {
28    use miniserde::de::{Map, Visitor};
29    use miniserde::json::Value;
30    use miniserde::{make_place, Deserialize, Result};
31    use stripe_types::miniserde_helpers::FromValueOpt;
32    use stripe_types::{MapBuilder, ObjectDeser};
33
34    make_place!(Place);
35
36    impl Deserialize for CustomUnitAmount {
37        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
38            Place::new(out)
39        }
40    }
41
42    struct Builder<'a> {
43        out: &'a mut Option<CustomUnitAmount>,
44        builder: CustomUnitAmountBuilder,
45    }
46
47    impl Visitor for Place<CustomUnitAmount> {
48        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
49            Ok(Box::new(Builder {
50                out: &mut self.out,
51                builder: CustomUnitAmountBuilder::deser_default(),
52            }))
53        }
54    }
55
56    impl MapBuilder for CustomUnitAmountBuilder {
57        type Out = CustomUnitAmount;
58        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
59            Ok(match k {
60                "maximum" => Deserialize::begin(&mut self.maximum),
61                "minimum" => Deserialize::begin(&mut self.minimum),
62                "preset" => Deserialize::begin(&mut self.preset),
63
64                _ => <dyn Visitor>::ignore(),
65            })
66        }
67
68        fn deser_default() -> Self {
69            Self {
70                maximum: Deserialize::default(),
71                minimum: Deserialize::default(),
72                preset: Deserialize::default(),
73            }
74        }
75
76        fn take_out(&mut self) -> Option<Self::Out> {
77            let (Some(maximum), Some(minimum), Some(preset)) =
78                (self.maximum, self.minimum, self.preset)
79            else {
80                return None;
81            };
82            Some(Self::Out { maximum, minimum, preset })
83        }
84    }
85
86    impl Map for Builder<'_> {
87        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
88            self.builder.key(k)
89        }
90
91        fn finish(&mut self) -> Result<()> {
92            *self.out = self.builder.take_out();
93            Ok(())
94        }
95    }
96
97    impl ObjectDeser for CustomUnitAmount {
98        type Builder = CustomUnitAmountBuilder;
99    }
100
101    impl FromValueOpt for CustomUnitAmount {
102        fn from_value(v: Value) -> Option<Self> {
103            let Value::Object(obj) = v else {
104                return None;
105            };
106            let mut b = CustomUnitAmountBuilder::deser_default();
107            for (k, v) in obj {
108                match k.as_str() {
109                    "maximum" => b.maximum = FromValueOpt::from_value(v),
110                    "minimum" => b.minimum = FromValueOpt::from_value(v),
111                    "preset" => b.preset = FromValueOpt::from_value(v),
112
113                    _ => {}
114                }
115            }
116            b.take_out()
117        }
118    }
119};