stripe_shared/
currency_option.rs1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CurrencyOption {
5 pub custom_unit_amount: Option<stripe_shared::CustomUnitAmount>,
7 pub tax_behavior: Option<CurrencyOptionTaxBehavior>,
12 pub tiers: Option<Vec<stripe_shared::PriceTier>>,
16 pub unit_amount: Option<i64>,
19 pub unit_amount_decimal: Option<String>,
22}
23#[doc(hidden)]
24pub struct CurrencyOptionBuilder {
25 custom_unit_amount: Option<Option<stripe_shared::CustomUnitAmount>>,
26 tax_behavior: Option<Option<CurrencyOptionTaxBehavior>>,
27 tiers: Option<Option<Vec<stripe_shared::PriceTier>>>,
28 unit_amount: Option<Option<i64>>,
29 unit_amount_decimal: Option<Option<String>>,
30}
31
32#[allow(
33 unused_variables,
34 irrefutable_let_patterns,
35 clippy::let_unit_value,
36 clippy::match_single_binding,
37 clippy::single_match
38)]
39const _: () = {
40 use miniserde::de::{Map, Visitor};
41 use miniserde::json::Value;
42 use miniserde::{Deserialize, Result, make_place};
43 use stripe_types::miniserde_helpers::FromValueOpt;
44 use stripe_types::{MapBuilder, ObjectDeser};
45
46 make_place!(Place);
47
48 impl Deserialize for CurrencyOption {
49 fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
50 Place::new(out)
51 }
52 }
53
54 struct Builder<'a> {
55 out: &'a mut Option<CurrencyOption>,
56 builder: CurrencyOptionBuilder,
57 }
58
59 impl Visitor for Place<CurrencyOption> {
60 fn map(&mut self) -> Result<Box<dyn Map + '_>> {
61 Ok(Box::new(Builder {
62 out: &mut self.out,
63 builder: CurrencyOptionBuilder::deser_default(),
64 }))
65 }
66 }
67
68 impl MapBuilder for CurrencyOptionBuilder {
69 type Out = CurrencyOption;
70 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
71 Ok(match k {
72 "custom_unit_amount" => Deserialize::begin(&mut self.custom_unit_amount),
73 "tax_behavior" => Deserialize::begin(&mut self.tax_behavior),
74 "tiers" => Deserialize::begin(&mut self.tiers),
75 "unit_amount" => Deserialize::begin(&mut self.unit_amount),
76 "unit_amount_decimal" => Deserialize::begin(&mut self.unit_amount_decimal),
77 _ => <dyn Visitor>::ignore(),
78 })
79 }
80
81 fn deser_default() -> Self {
82 Self {
83 custom_unit_amount: Deserialize::default(),
84 tax_behavior: Deserialize::default(),
85 tiers: Deserialize::default(),
86 unit_amount: Deserialize::default(),
87 unit_amount_decimal: Deserialize::default(),
88 }
89 }
90
91 fn take_out(&mut self) -> Option<Self::Out> {
92 let (
93 Some(custom_unit_amount),
94 Some(tax_behavior),
95 Some(tiers),
96 Some(unit_amount),
97 Some(unit_amount_decimal),
98 ) = (
99 self.custom_unit_amount,
100 self.tax_behavior.take(),
101 self.tiers.take(),
102 self.unit_amount,
103 self.unit_amount_decimal.take(),
104 )
105 else {
106 return None;
107 };
108 Some(Self::Out {
109 custom_unit_amount,
110 tax_behavior,
111 tiers,
112 unit_amount,
113 unit_amount_decimal,
114 })
115 }
116 }
117
118 impl Map for Builder<'_> {
119 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
120 self.builder.key(k)
121 }
122
123 fn finish(&mut self) -> Result<()> {
124 *self.out = self.builder.take_out();
125 Ok(())
126 }
127 }
128
129 impl ObjectDeser for CurrencyOption {
130 type Builder = CurrencyOptionBuilder;
131 }
132
133 impl FromValueOpt for CurrencyOption {
134 fn from_value(v: Value) -> Option<Self> {
135 let Value::Object(obj) = v else {
136 return None;
137 };
138 let mut b = CurrencyOptionBuilder::deser_default();
139 for (k, v) in obj {
140 match k.as_str() {
141 "custom_unit_amount" => b.custom_unit_amount = FromValueOpt::from_value(v),
142 "tax_behavior" => b.tax_behavior = FromValueOpt::from_value(v),
143 "tiers" => b.tiers = FromValueOpt::from_value(v),
144 "unit_amount" => b.unit_amount = FromValueOpt::from_value(v),
145 "unit_amount_decimal" => b.unit_amount_decimal = FromValueOpt::from_value(v),
146 _ => {}
147 }
148 }
149 b.take_out()
150 }
151 }
152};
153#[derive(Clone, Eq, PartialEq)]
158#[non_exhaustive]
159pub enum CurrencyOptionTaxBehavior {
160 Exclusive,
161 Inclusive,
162 Unspecified,
163 Unknown(String),
165}
166impl CurrencyOptionTaxBehavior {
167 pub fn as_str(&self) -> &str {
168 use CurrencyOptionTaxBehavior::*;
169 match self {
170 Exclusive => "exclusive",
171 Inclusive => "inclusive",
172 Unspecified => "unspecified",
173 Unknown(v) => v,
174 }
175 }
176}
177
178impl std::str::FromStr for CurrencyOptionTaxBehavior {
179 type Err = std::convert::Infallible;
180 fn from_str(s: &str) -> Result<Self, Self::Err> {
181 use CurrencyOptionTaxBehavior::*;
182 match s {
183 "exclusive" => Ok(Exclusive),
184 "inclusive" => Ok(Inclusive),
185 "unspecified" => Ok(Unspecified),
186 v => {
187 tracing::warn!("Unknown value '{}' for enum '{}'", v, "CurrencyOptionTaxBehavior");
188 Ok(Unknown(v.to_owned()))
189 }
190 }
191 }
192}
193impl std::fmt::Display for CurrencyOptionTaxBehavior {
194 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
195 f.write_str(self.as_str())
196 }
197}
198
199impl std::fmt::Debug for CurrencyOptionTaxBehavior {
200 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
201 f.write_str(self.as_str())
202 }
203}
204#[cfg(feature = "serialize")]
205impl serde::Serialize for CurrencyOptionTaxBehavior {
206 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
207 where
208 S: serde::Serializer,
209 {
210 serializer.serialize_str(self.as_str())
211 }
212}
213impl miniserde::Deserialize for CurrencyOptionTaxBehavior {
214 fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
215 crate::Place::new(out)
216 }
217}
218
219impl miniserde::de::Visitor for crate::Place<CurrencyOptionTaxBehavior> {
220 fn string(&mut self, s: &str) -> miniserde::Result<()> {
221 use std::str::FromStr;
222 self.out = Some(CurrencyOptionTaxBehavior::from_str(s).expect("infallible"));
223 Ok(())
224 }
225}
226
227stripe_types::impl_from_val_with_from_str!(CurrencyOptionTaxBehavior);
228#[cfg(feature = "deserialize")]
229impl<'de> serde::Deserialize<'de> for CurrencyOptionTaxBehavior {
230 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
231 use std::str::FromStr;
232 let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
233 Ok(Self::from_str(&s).expect("infallible"))
234 }
235}