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::{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,
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/// 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.
154/// Specifies whether the price is considered inclusive of taxes or exclusive of taxes.
155/// One of `inclusive`, `exclusive`, or `unspecified`.
156/// Once specified as either `inclusive` or `exclusive`, it cannot be changed.
157#[derive(Copy, Clone, Eq, PartialEq)]
158pub enum CurrencyOptionTaxBehavior {
159    Exclusive,
160    Inclusive,
161    Unspecified,
162}
163impl CurrencyOptionTaxBehavior {
164    pub fn as_str(self) -> &'static str {
165        use CurrencyOptionTaxBehavior::*;
166        match self {
167            Exclusive => "exclusive",
168            Inclusive => "inclusive",
169            Unspecified => "unspecified",
170        }
171    }
172}
173
174impl std::str::FromStr for CurrencyOptionTaxBehavior {
175    type Err = stripe_types::StripeParseError;
176    fn from_str(s: &str) -> Result<Self, Self::Err> {
177        use CurrencyOptionTaxBehavior::*;
178        match s {
179            "exclusive" => Ok(Exclusive),
180            "inclusive" => Ok(Inclusive),
181            "unspecified" => Ok(Unspecified),
182            _ => Err(stripe_types::StripeParseError),
183        }
184    }
185}
186impl std::fmt::Display for CurrencyOptionTaxBehavior {
187    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
188        f.write_str(self.as_str())
189    }
190}
191
192impl std::fmt::Debug for CurrencyOptionTaxBehavior {
193    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
194        f.write_str(self.as_str())
195    }
196}
197#[cfg(feature = "serialize")]
198impl serde::Serialize for CurrencyOptionTaxBehavior {
199    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
200    where
201        S: serde::Serializer,
202    {
203        serializer.serialize_str(self.as_str())
204    }
205}
206impl miniserde::Deserialize for CurrencyOptionTaxBehavior {
207    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
208        crate::Place::new(out)
209    }
210}
211
212impl miniserde::de::Visitor for crate::Place<CurrencyOptionTaxBehavior> {
213    fn string(&mut self, s: &str) -> miniserde::Result<()> {
214        use std::str::FromStr;
215        self.out = Some(CurrencyOptionTaxBehavior::from_str(s).map_err(|_| miniserde::Error)?);
216        Ok(())
217    }
218}
219
220stripe_types::impl_from_val_with_from_str!(CurrencyOptionTaxBehavior);
221#[cfg(feature = "deserialize")]
222impl<'de> serde::Deserialize<'de> for CurrencyOptionTaxBehavior {
223    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
224        use std::str::FromStr;
225        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
226        Self::from_str(&s)
227            .map_err(|_| serde::de::Error::custom("Unknown value for CurrencyOptionTaxBehavior"))
228    }
229}