Skip to main content

stripe_shared/
custom_unit_amount.rs

1#[derive(Copy, Clone, Eq, PartialEq)]
2#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
3#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct CustomUnitAmount {
6    /// The maximum unit amount the customer can specify for this item.
7    pub maximum: Option<i64>,
8    /// The minimum unit amount the customer can specify for this item.
9    /// Must be at least the minimum charge amount.
10    pub minimum: Option<i64>,
11    /// The starting unit amount which can be updated by the customer.
12    pub preset: Option<i64>,
13}
14#[cfg(feature = "redact-generated-debug")]
15impl std::fmt::Debug for CustomUnitAmount {
16    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
17        f.debug_struct("CustomUnitAmount").finish_non_exhaustive()
18    }
19}
20#[doc(hidden)]
21pub struct CustomUnitAmountBuilder {
22    maximum: Option<Option<i64>>,
23    minimum: Option<Option<i64>>,
24    preset: Option<Option<i64>>,
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 CustomUnitAmount {
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<CustomUnitAmount>,
51        builder: CustomUnitAmountBuilder,
52    }
53
54    impl Visitor for Place<CustomUnitAmount> {
55        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
56            Ok(Box::new(Builder {
57                out: &mut self.out,
58                builder: CustomUnitAmountBuilder::deser_default(),
59            }))
60        }
61    }
62
63    impl MapBuilder for CustomUnitAmountBuilder {
64        type Out = CustomUnitAmount;
65        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
66            Ok(match k {
67                "maximum" => Deserialize::begin(&mut self.maximum),
68                "minimum" => Deserialize::begin(&mut self.minimum),
69                "preset" => Deserialize::begin(&mut self.preset),
70                _ => <dyn Visitor>::ignore(),
71            })
72        }
73
74        fn deser_default() -> Self {
75            Self { maximum: Some(None), minimum: Some(None), preset: Some(None) }
76        }
77
78        fn take_out(&mut self) -> Option<Self::Out> {
79            let (Some(maximum), Some(minimum), Some(preset)) =
80                (self.maximum, self.minimum, self.preset)
81            else {
82                return None;
83            };
84            Some(Self::Out { maximum, minimum, preset })
85        }
86    }
87
88    impl Map for Builder<'_> {
89        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
90            self.builder.key(k)
91        }
92
93        fn finish(&mut self) -> Result<()> {
94            *self.out = self.builder.take_out();
95            Ok(())
96        }
97    }
98
99    impl ObjectDeser for CustomUnitAmount {
100        type Builder = CustomUnitAmountBuilder;
101    }
102
103    impl FromValueOpt for CustomUnitAmount {
104        fn from_value(v: Value) -> Option<Self> {
105            let Value::Object(obj) = v else {
106                return None;
107            };
108            let mut b = CustomUnitAmountBuilder::deser_default();
109            for (k, v) in obj {
110                match k.as_str() {
111                    "maximum" => b.maximum = FromValueOpt::from_value(v),
112                    "minimum" => b.minimum = FromValueOpt::from_value(v),
113                    "preset" => b.preset = FromValueOpt::from_value(v),
114                    _ => {}
115                }
116            }
117            b.take_out()
118        }
119    }
120};