stripe_misc/exchange_rate/
types.rs

1/// `ExchangeRate` objects allow you to determine the rates that Stripe is currently
2/// using to convert from one currency to another. Since this number is variable
3/// throughout the day, there are various reasons why you might want to know the current
4/// rate (for example, to dynamically price an item for a user with a default
5/// payment in a foreign currency).
6///
7/// Please refer to our [Exchange Rates API](https://stripe.com/docs/fx-rates) guide for more details.
8///
9/// *[Note: this integration path is supported but no longer recommended]* Additionally,
10/// you can guarantee that a charge is made with an exchange rate that you expect is
11/// current. To do so, you must pass in the exchange_rate to charges endpoints. If the
12/// value is no longer up to date, the charge won't go through. Please refer to our
13/// [Using with charges](https://stripe.com/docs/exchange-rates) guide for more details.
14///
15/// -----
16///
17///  
18///
19/// *This Exchange Rates API is a Beta Service and is subject to Stripe's terms of service.
20/// You may use the API solely for the purpose of transacting on Stripe.
21/// For example, the API may be queried in order to:*.
22///
23/// - *localize prices for processing payments on Stripe*
24/// - *reconcile Stripe transactions*
25/// - *determine how much money to send to a connected account*
26/// - *determine app fees to charge a connected account*
27///
28/// *Using this Exchange Rates API beta for any purpose other than to transact on Stripe is strictly prohibited and constitutes a violation of Stripe's terms of service.*.
29#[derive(Clone, Debug)]
30#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
31pub struct ExchangeRate {
32    /// Unique identifier for the object.
33    /// Represented as the three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) in lowercase.
34    pub id: stripe_misc::ExchangeRateId,
35    /// Hash where the keys are supported currencies and the values are the exchange rate at which the base id currency converts to the key currency.
36    pub rates: std::collections::HashMap<String, f64>,
37}
38#[doc(hidden)]
39pub struct ExchangeRateBuilder {
40    id: Option<stripe_misc::ExchangeRateId>,
41    rates: Option<std::collections::HashMap<String, f64>>,
42}
43
44#[allow(
45    unused_variables,
46    irrefutable_let_patterns,
47    clippy::let_unit_value,
48    clippy::match_single_binding,
49    clippy::single_match
50)]
51const _: () = {
52    use miniserde::de::{Map, Visitor};
53    use miniserde::json::Value;
54    use miniserde::{make_place, Deserialize, Result};
55    use stripe_types::miniserde_helpers::FromValueOpt;
56    use stripe_types::{MapBuilder, ObjectDeser};
57
58    make_place!(Place);
59
60    impl Deserialize for ExchangeRate {
61        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
62            Place::new(out)
63        }
64    }
65
66    struct Builder<'a> {
67        out: &'a mut Option<ExchangeRate>,
68        builder: ExchangeRateBuilder,
69    }
70
71    impl Visitor for Place<ExchangeRate> {
72        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
73            Ok(Box::new(Builder {
74                out: &mut self.out,
75                builder: ExchangeRateBuilder::deser_default(),
76            }))
77        }
78    }
79
80    impl MapBuilder for ExchangeRateBuilder {
81        type Out = ExchangeRate;
82        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
83            Ok(match k {
84                "id" => Deserialize::begin(&mut self.id),
85                "rates" => Deserialize::begin(&mut self.rates),
86
87                _ => <dyn Visitor>::ignore(),
88            })
89        }
90
91        fn deser_default() -> Self {
92            Self { id: Deserialize::default(), rates: Deserialize::default() }
93        }
94
95        fn take_out(&mut self) -> Option<Self::Out> {
96            let (Some(id), Some(rates)) = (self.id.take(), self.rates.take()) else {
97                return None;
98            };
99            Some(Self::Out { id, rates })
100        }
101    }
102
103    impl Map for Builder<'_> {
104        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
105            self.builder.key(k)
106        }
107
108        fn finish(&mut self) -> Result<()> {
109            *self.out = self.builder.take_out();
110            Ok(())
111        }
112    }
113
114    impl ObjectDeser for ExchangeRate {
115        type Builder = ExchangeRateBuilder;
116    }
117
118    impl FromValueOpt for ExchangeRate {
119        fn from_value(v: Value) -> Option<Self> {
120            let Value::Object(obj) = v else {
121                return None;
122            };
123            let mut b = ExchangeRateBuilder::deser_default();
124            for (k, v) in obj {
125                match k.as_str() {
126                    "id" => b.id = FromValueOpt::from_value(v),
127                    "rates" => b.rates = FromValueOpt::from_value(v),
128
129                    _ => {}
130                }
131            }
132            b.take_out()
133        }
134    }
135};
136#[cfg(feature = "serialize")]
137impl serde::Serialize for ExchangeRate {
138    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
139        use serde::ser::SerializeStruct;
140        let mut s = s.serialize_struct("ExchangeRate", 3)?;
141        s.serialize_field("id", &self.id)?;
142        s.serialize_field("rates", &self.rates)?;
143
144        s.serialize_field("object", "exchange_rate")?;
145        s.end()
146    }
147}
148impl stripe_types::Object for ExchangeRate {
149    type Id = stripe_misc::ExchangeRateId;
150    fn id(&self) -> &Self::Id {
151        &self.id
152    }
153
154    fn into_id(self) -> Self::Id {
155        self.id
156    }
157}
158stripe_types::def_id!(ExchangeRateId);