stripe_shared/
currency_option.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CurrencyOption {
5    /// When set, provides configuration for the amount to be adjusted by the customer during Checkout Sessions and Payment Links.
6    pub custom_unit_amount: Option<stripe_shared::CustomUnitAmount>,
7    /// Only required if a [default tax behavior](https://stripe.com/docs/tax/products-prices-tax-categories-tax-behavior#setting-a-default-tax-behavior-(recommended)) was not provided in the Stripe Tax settings.
8    /// Specifies whether the price is considered inclusive of taxes or exclusive of taxes.
9    /// One of `inclusive`, `exclusive`, or `unspecified`.
10    /// Once specified as either `inclusive` or `exclusive`, it cannot be changed.
11    pub tax_behavior: Option<CurrencyOptionTaxBehavior>,
12    /// Each element represents a pricing tier.
13    /// This parameter requires `billing_scheme` to be set to `tiered`.
14    /// See also the documentation for `billing_scheme`.
15    pub tiers: Option<Vec<stripe_shared::PriceTier>>,
16    /// The unit amount in cents (or local equivalent) to be charged, represented as a whole integer if possible.
17    /// Only set if `billing_scheme=per_unit`.
18    pub unit_amount: Option<i64>,
19    /// The unit amount in cents (or local equivalent) to be charged, represented as a decimal string with at most 12 decimal places.
20    /// Only set if `billing_scheme=per_unit`.
21    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::{make_place, Deserialize, Result};
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
78                _ => <dyn Visitor>::ignore(),
79            })
80        }
81
82        fn deser_default() -> Self {
83            Self {
84                custom_unit_amount: Deserialize::default(),
85                tax_behavior: Deserialize::default(),
86                tiers: Deserialize::default(),
87                unit_amount: Deserialize::default(),
88                unit_amount_decimal: Deserialize::default(),
89            }
90        }
91
92        fn take_out(&mut self) -> Option<Self::Out> {
93            let (
94                Some(custom_unit_amount),
95                Some(tax_behavior),
96                Some(tiers),
97                Some(unit_amount),
98                Some(unit_amount_decimal),
99            ) = (
100                self.custom_unit_amount,
101                self.tax_behavior,
102                self.tiers.take(),
103                self.unit_amount,
104                self.unit_amount_decimal.take(),
105            )
106            else {
107                return None;
108            };
109            Some(Self::Out {
110                custom_unit_amount,
111                tax_behavior,
112                tiers,
113                unit_amount,
114                unit_amount_decimal,
115            })
116        }
117    }
118
119    impl<'a> Map for Builder<'a> {
120        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
121            self.builder.key(k)
122        }
123
124        fn finish(&mut self) -> Result<()> {
125            *self.out = self.builder.take_out();
126            Ok(())
127        }
128    }
129
130    impl ObjectDeser for CurrencyOption {
131        type Builder = CurrencyOptionBuilder;
132    }
133
134    impl FromValueOpt for CurrencyOption {
135        fn from_value(v: Value) -> Option<Self> {
136            let Value::Object(obj) = v else {
137                return None;
138            };
139            let mut b = CurrencyOptionBuilder::deser_default();
140            for (k, v) in obj {
141                match k.as_str() {
142                    "custom_unit_amount" => b.custom_unit_amount = FromValueOpt::from_value(v),
143                    "tax_behavior" => b.tax_behavior = FromValueOpt::from_value(v),
144                    "tiers" => b.tiers = FromValueOpt::from_value(v),
145                    "unit_amount" => b.unit_amount = FromValueOpt::from_value(v),
146                    "unit_amount_decimal" => b.unit_amount_decimal = FromValueOpt::from_value(v),
147
148                    _ => {}
149                }
150            }
151            b.take_out()
152        }
153    }
154};
155/// Only required if a [default tax behavior](https://stripe.com/docs/tax/products-prices-tax-categories-tax-behavior#setting-a-default-tax-behavior-(recommended)) was not provided in the Stripe Tax settings.
156/// Specifies whether the price is considered inclusive of taxes or exclusive of taxes.
157/// One of `inclusive`, `exclusive`, or `unspecified`.
158/// Once specified as either `inclusive` or `exclusive`, it cannot be changed.
159#[derive(Copy, Clone, Eq, PartialEq)]
160pub enum CurrencyOptionTaxBehavior {
161    Exclusive,
162    Inclusive,
163    Unspecified,
164}
165impl CurrencyOptionTaxBehavior {
166    pub fn as_str(self) -> &'static str {
167        use CurrencyOptionTaxBehavior::*;
168        match self {
169            Exclusive => "exclusive",
170            Inclusive => "inclusive",
171            Unspecified => "unspecified",
172        }
173    }
174}
175
176impl std::str::FromStr for CurrencyOptionTaxBehavior {
177    type Err = stripe_types::StripeParseError;
178    fn from_str(s: &str) -> Result<Self, Self::Err> {
179        use CurrencyOptionTaxBehavior::*;
180        match s {
181            "exclusive" => Ok(Exclusive),
182            "inclusive" => Ok(Inclusive),
183            "unspecified" => Ok(Unspecified),
184            _ => Err(stripe_types::StripeParseError),
185        }
186    }
187}
188impl std::fmt::Display for CurrencyOptionTaxBehavior {
189    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
190        f.write_str(self.as_str())
191    }
192}
193
194impl std::fmt::Debug for CurrencyOptionTaxBehavior {
195    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
196        f.write_str(self.as_str())
197    }
198}
199#[cfg(feature = "serialize")]
200impl serde::Serialize for CurrencyOptionTaxBehavior {
201    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
202    where
203        S: serde::Serializer,
204    {
205        serializer.serialize_str(self.as_str())
206    }
207}
208impl miniserde::Deserialize for CurrencyOptionTaxBehavior {
209    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
210        crate::Place::new(out)
211    }
212}
213
214impl miniserde::de::Visitor for crate::Place<CurrencyOptionTaxBehavior> {
215    fn string(&mut self, s: &str) -> miniserde::Result<()> {
216        use std::str::FromStr;
217        self.out = Some(CurrencyOptionTaxBehavior::from_str(s).map_err(|_| miniserde::Error)?);
218        Ok(())
219    }
220}
221
222stripe_types::impl_from_val_with_from_str!(CurrencyOptionTaxBehavior);
223#[cfg(feature = "deserialize")]
224impl<'de> serde::Deserialize<'de> for CurrencyOptionTaxBehavior {
225    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
226        use std::str::FromStr;
227        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
228        Self::from_str(&s)
229            .map_err(|_| serde::de::Error::custom("Unknown value for CurrencyOptionTaxBehavior"))
230    }
231}