stripe_shared/
transform_quantity.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct TransformQuantity {
5    /// Divide usage by this number.
6    pub divide_by: i64,
7    /// After division, either round the result `up` or `down`.
8    pub round: TransformQuantityRound,
9}
10#[doc(hidden)]
11pub struct TransformQuantityBuilder {
12    divide_by: Option<i64>,
13    round: Option<TransformQuantityRound>,
14}
15
16#[allow(
17    unused_variables,
18    irrefutable_let_patterns,
19    clippy::let_unit_value,
20    clippy::match_single_binding,
21    clippy::single_match
22)]
23const _: () = {
24    use miniserde::de::{Map, Visitor};
25    use miniserde::json::Value;
26    use miniserde::{Deserialize, Result, make_place};
27    use stripe_types::miniserde_helpers::FromValueOpt;
28    use stripe_types::{MapBuilder, ObjectDeser};
29
30    make_place!(Place);
31
32    impl Deserialize for TransformQuantity {
33        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
34            Place::new(out)
35        }
36    }
37
38    struct Builder<'a> {
39        out: &'a mut Option<TransformQuantity>,
40        builder: TransformQuantityBuilder,
41    }
42
43    impl Visitor for Place<TransformQuantity> {
44        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
45            Ok(Box::new(Builder {
46                out: &mut self.out,
47                builder: TransformQuantityBuilder::deser_default(),
48            }))
49        }
50    }
51
52    impl MapBuilder for TransformQuantityBuilder {
53        type Out = TransformQuantity;
54        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
55            Ok(match k {
56                "divide_by" => Deserialize::begin(&mut self.divide_by),
57                "round" => Deserialize::begin(&mut self.round),
58                _ => <dyn Visitor>::ignore(),
59            })
60        }
61
62        fn deser_default() -> Self {
63            Self { divide_by: Deserialize::default(), round: Deserialize::default() }
64        }
65
66        fn take_out(&mut self) -> Option<Self::Out> {
67            let (Some(divide_by), Some(round)) = (self.divide_by, self.round.take()) else {
68                return None;
69            };
70            Some(Self::Out { divide_by, round })
71        }
72    }
73
74    impl Map for Builder<'_> {
75        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
76            self.builder.key(k)
77        }
78
79        fn finish(&mut self) -> Result<()> {
80            *self.out = self.builder.take_out();
81            Ok(())
82        }
83    }
84
85    impl ObjectDeser for TransformQuantity {
86        type Builder = TransformQuantityBuilder;
87    }
88
89    impl FromValueOpt for TransformQuantity {
90        fn from_value(v: Value) -> Option<Self> {
91            let Value::Object(obj) = v else {
92                return None;
93            };
94            let mut b = TransformQuantityBuilder::deser_default();
95            for (k, v) in obj {
96                match k.as_str() {
97                    "divide_by" => b.divide_by = FromValueOpt::from_value(v),
98                    "round" => b.round = FromValueOpt::from_value(v),
99                    _ => {}
100                }
101            }
102            b.take_out()
103        }
104    }
105};
106/// After division, either round the result `up` or `down`.
107#[derive(Clone, Eq, PartialEq)]
108#[non_exhaustive]
109pub enum TransformQuantityRound {
110    Down,
111    Up,
112    /// An unrecognized value from Stripe. Should not be used as a request parameter.
113    Unknown(String),
114}
115impl TransformQuantityRound {
116    pub fn as_str(&self) -> &str {
117        use TransformQuantityRound::*;
118        match self {
119            Down => "down",
120            Up => "up",
121            Unknown(v) => v,
122        }
123    }
124}
125
126impl std::str::FromStr for TransformQuantityRound {
127    type Err = std::convert::Infallible;
128    fn from_str(s: &str) -> Result<Self, Self::Err> {
129        use TransformQuantityRound::*;
130        match s {
131            "down" => Ok(Down),
132            "up" => Ok(Up),
133            v => {
134                tracing::warn!("Unknown value '{}' for enum '{}'", v, "TransformQuantityRound");
135                Ok(Unknown(v.to_owned()))
136            }
137        }
138    }
139}
140impl std::fmt::Display for TransformQuantityRound {
141    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
142        f.write_str(self.as_str())
143    }
144}
145
146impl std::fmt::Debug for TransformQuantityRound {
147    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
148        f.write_str(self.as_str())
149    }
150}
151#[cfg(feature = "serialize")]
152impl serde::Serialize for TransformQuantityRound {
153    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
154    where
155        S: serde::Serializer,
156    {
157        serializer.serialize_str(self.as_str())
158    }
159}
160impl miniserde::Deserialize for TransformQuantityRound {
161    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
162        crate::Place::new(out)
163    }
164}
165
166impl miniserde::de::Visitor for crate::Place<TransformQuantityRound> {
167    fn string(&mut self, s: &str) -> miniserde::Result<()> {
168        use std::str::FromStr;
169        self.out = Some(TransformQuantityRound::from_str(s).expect("infallible"));
170        Ok(())
171    }
172}
173
174stripe_types::impl_from_val_with_from_str!(TransformQuantityRound);
175#[cfg(feature = "deserialize")]
176impl<'de> serde::Deserialize<'de> for TransformQuantityRound {
177    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
178        use std::str::FromStr;
179        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
180        Ok(Self::from_str(&s).expect("infallible"))
181    }
182}