Skip to main content

stripe_shared/
shipping_rate.rs

1/// Shipping rates describe the price of shipping presented to your customers and
2/// applied to a purchase.
3/// For more information, see [Charge for shipping](https://docs.stripe.com/payments/during-payment/charge-shipping).
4///
5/// For more details see <<https://stripe.com/docs/api/shipping_rates/object>>.
6#[derive(Clone)]
7#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
8#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
9pub struct ShippingRate {
10    /// Whether the shipping rate can be used for new purchases. Defaults to `true`.
11    pub active: bool,
12    /// Time at which the object was created. Measured in seconds since the Unix epoch.
13    pub created: stripe_types::Timestamp,
14    /// The estimated range for how long shipping will take, meant to be displayable to the customer.
15    /// This will appear on CheckoutSessions.
16    pub delivery_estimate: Option<stripe_shared::ShippingRateDeliveryEstimate>,
17    /// The name of the shipping rate, meant to be displayable to the customer.
18    /// This will appear on CheckoutSessions.
19    pub display_name: Option<String>,
20    pub fixed_amount: Option<stripe_shared::ShippingRateFixedAmount>,
21    /// Unique identifier for the object.
22    pub id: stripe_shared::ShippingRateId,
23    /// If the object exists in live mode, the value is `true`.
24    /// If the object exists in test mode, the value is `false`.
25    pub livemode: bool,
26    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
27    /// This can be useful for storing additional information about the object in a structured format.
28    pub metadata: std::collections::HashMap<String, String>,
29    /// Specifies whether the rate is considered inclusive of taxes or exclusive of taxes.
30    /// One of `inclusive`, `exclusive`, or `unspecified`.
31    pub tax_behavior: Option<stripe_shared::ShippingRateTaxBehavior>,
32    /// A [tax code](https://docs.stripe.com/tax/tax-categories) ID.
33    /// The Shipping tax code is `txcd_92010001`.
34    pub tax_code: Option<stripe_types::Expandable<stripe_shared::TaxCode>>,
35    /// The type of calculation to use on the shipping rate.
36    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
37    pub type_: stripe_shared::ShippingRateType,
38}
39#[cfg(feature = "redact-generated-debug")]
40impl std::fmt::Debug for ShippingRate {
41    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42        f.debug_struct("ShippingRate").finish_non_exhaustive()
43    }
44}
45#[doc(hidden)]
46pub struct ShippingRateBuilder {
47    active: Option<bool>,
48    created: Option<stripe_types::Timestamp>,
49    delivery_estimate: Option<Option<stripe_shared::ShippingRateDeliveryEstimate>>,
50    display_name: Option<Option<String>>,
51    fixed_amount: Option<Option<stripe_shared::ShippingRateFixedAmount>>,
52    id: Option<stripe_shared::ShippingRateId>,
53    livemode: Option<bool>,
54    metadata: Option<std::collections::HashMap<String, String>>,
55    tax_behavior: Option<Option<stripe_shared::ShippingRateTaxBehavior>>,
56    tax_code: Option<Option<stripe_types::Expandable<stripe_shared::TaxCode>>>,
57    type_: Option<stripe_shared::ShippingRateType>,
58}
59
60#[allow(
61    unused_variables,
62    irrefutable_let_patterns,
63    clippy::let_unit_value,
64    clippy::match_single_binding,
65    clippy::single_match
66)]
67const _: () = {
68    use miniserde::de::{Map, Visitor};
69    use miniserde::json::Value;
70    use miniserde::{Deserialize, Result, make_place};
71    use stripe_types::miniserde_helpers::FromValueOpt;
72    use stripe_types::{MapBuilder, ObjectDeser};
73
74    make_place!(Place);
75
76    impl Deserialize for ShippingRate {
77        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
78            Place::new(out)
79        }
80    }
81
82    struct Builder<'a> {
83        out: &'a mut Option<ShippingRate>,
84        builder: ShippingRateBuilder,
85    }
86
87    impl Visitor for Place<ShippingRate> {
88        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
89            Ok(Box::new(Builder {
90                out: &mut self.out,
91                builder: ShippingRateBuilder::deser_default(),
92            }))
93        }
94    }
95
96    impl MapBuilder for ShippingRateBuilder {
97        type Out = ShippingRate;
98        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
99            Ok(match k {
100                "active" => Deserialize::begin(&mut self.active),
101                "created" => Deserialize::begin(&mut self.created),
102                "delivery_estimate" => Deserialize::begin(&mut self.delivery_estimate),
103                "display_name" => Deserialize::begin(&mut self.display_name),
104                "fixed_amount" => Deserialize::begin(&mut self.fixed_amount),
105                "id" => Deserialize::begin(&mut self.id),
106                "livemode" => Deserialize::begin(&mut self.livemode),
107                "metadata" => Deserialize::begin(&mut self.metadata),
108                "tax_behavior" => Deserialize::begin(&mut self.tax_behavior),
109                "tax_code" => Deserialize::begin(&mut self.tax_code),
110                "type" => Deserialize::begin(&mut self.type_),
111                _ => <dyn Visitor>::ignore(),
112            })
113        }
114
115        fn deser_default() -> Self {
116            Self {
117                active: None,
118                created: None,
119                delivery_estimate: Some(None),
120                display_name: Some(None),
121                fixed_amount: Some(None),
122                id: None,
123                livemode: None,
124                metadata: None,
125                tax_behavior: Some(None),
126                tax_code: Some(None),
127                type_: None,
128            }
129        }
130
131        fn take_out(&mut self) -> Option<Self::Out> {
132            let (
133                Some(active),
134                Some(created),
135                Some(delivery_estimate),
136                Some(display_name),
137                Some(fixed_amount),
138                Some(id),
139                Some(livemode),
140                Some(metadata),
141                Some(tax_behavior),
142                Some(tax_code),
143                Some(type_),
144            ) = (
145                self.active,
146                self.created,
147                self.delivery_estimate.take(),
148                self.display_name.take(),
149                self.fixed_amount.take(),
150                self.id.take(),
151                self.livemode,
152                self.metadata.take(),
153                self.tax_behavior.take(),
154                self.tax_code.take(),
155                self.type_.take(),
156            )
157            else {
158                return None;
159            };
160            Some(Self::Out {
161                active,
162                created,
163                delivery_estimate,
164                display_name,
165                fixed_amount,
166                id,
167                livemode,
168                metadata,
169                tax_behavior,
170                tax_code,
171                type_,
172            })
173        }
174    }
175
176    impl Map for Builder<'_> {
177        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
178            self.builder.key(k)
179        }
180
181        fn finish(&mut self) -> Result<()> {
182            *self.out = self.builder.take_out();
183            Ok(())
184        }
185    }
186
187    impl ObjectDeser for ShippingRate {
188        type Builder = ShippingRateBuilder;
189    }
190
191    impl FromValueOpt for ShippingRate {
192        fn from_value(v: Value) -> Option<Self> {
193            let Value::Object(obj) = v else {
194                return None;
195            };
196            let mut b = ShippingRateBuilder::deser_default();
197            for (k, v) in obj {
198                match k.as_str() {
199                    "active" => b.active = FromValueOpt::from_value(v),
200                    "created" => b.created = FromValueOpt::from_value(v),
201                    "delivery_estimate" => b.delivery_estimate = FromValueOpt::from_value(v),
202                    "display_name" => b.display_name = FromValueOpt::from_value(v),
203                    "fixed_amount" => b.fixed_amount = FromValueOpt::from_value(v),
204                    "id" => b.id = FromValueOpt::from_value(v),
205                    "livemode" => b.livemode = FromValueOpt::from_value(v),
206                    "metadata" => b.metadata = FromValueOpt::from_value(v),
207                    "tax_behavior" => b.tax_behavior = FromValueOpt::from_value(v),
208                    "tax_code" => b.tax_code = FromValueOpt::from_value(v),
209                    "type" => b.type_ = FromValueOpt::from_value(v),
210                    _ => {}
211                }
212            }
213            b.take_out()
214        }
215    }
216};
217#[cfg(feature = "serialize")]
218impl serde::Serialize for ShippingRate {
219    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
220        use serde::ser::SerializeStruct;
221        let mut s = s.serialize_struct("ShippingRate", 12)?;
222        s.serialize_field("active", &self.active)?;
223        s.serialize_field("created", &self.created)?;
224        s.serialize_field("delivery_estimate", &self.delivery_estimate)?;
225        s.serialize_field("display_name", &self.display_name)?;
226        s.serialize_field("fixed_amount", &self.fixed_amount)?;
227        s.serialize_field("id", &self.id)?;
228        s.serialize_field("livemode", &self.livemode)?;
229        s.serialize_field("metadata", &self.metadata)?;
230        s.serialize_field("tax_behavior", &self.tax_behavior)?;
231        s.serialize_field("tax_code", &self.tax_code)?;
232        s.serialize_field("type", &self.type_)?;
233
234        s.serialize_field("object", "shipping_rate")?;
235        s.end()
236    }
237}
238impl stripe_types::Object for ShippingRate {
239    type Id = stripe_shared::ShippingRateId;
240    fn id(&self) -> &Self::Id {
241        &self.id
242    }
243
244    fn into_id(self) -> Self::Id {
245        self.id
246    }
247}
248stripe_types::def_id!(ShippingRateId);
249#[derive(Clone, Eq, PartialEq)]
250#[non_exhaustive]
251pub enum ShippingRateTaxBehavior {
252    Exclusive,
253    Inclusive,
254    Unspecified,
255    /// An unrecognized value from Stripe. Should not be used as a request parameter.
256    Unknown(String),
257}
258impl ShippingRateTaxBehavior {
259    pub fn as_str(&self) -> &str {
260        use ShippingRateTaxBehavior::*;
261        match self {
262            Exclusive => "exclusive",
263            Inclusive => "inclusive",
264            Unspecified => "unspecified",
265            Unknown(v) => v,
266        }
267    }
268}
269
270impl std::str::FromStr for ShippingRateTaxBehavior {
271    type Err = std::convert::Infallible;
272    fn from_str(s: &str) -> Result<Self, Self::Err> {
273        use ShippingRateTaxBehavior::*;
274        match s {
275            "exclusive" => Ok(Exclusive),
276            "inclusive" => Ok(Inclusive),
277            "unspecified" => Ok(Unspecified),
278            v => {
279                tracing::warn!("Unknown value '{}' for enum '{}'", v, "ShippingRateTaxBehavior");
280                Ok(Unknown(v.to_owned()))
281            }
282        }
283    }
284}
285impl std::fmt::Display for ShippingRateTaxBehavior {
286    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
287        f.write_str(self.as_str())
288    }
289}
290
291#[cfg(not(feature = "redact-generated-debug"))]
292impl std::fmt::Debug for ShippingRateTaxBehavior {
293    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
294        f.write_str(self.as_str())
295    }
296}
297#[cfg(feature = "redact-generated-debug")]
298impl std::fmt::Debug for ShippingRateTaxBehavior {
299    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
300        f.debug_struct(stringify!(ShippingRateTaxBehavior)).finish_non_exhaustive()
301    }
302}
303impl serde::Serialize for ShippingRateTaxBehavior {
304    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
305    where
306        S: serde::Serializer,
307    {
308        serializer.serialize_str(self.as_str())
309    }
310}
311impl miniserde::Deserialize for ShippingRateTaxBehavior {
312    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
313        crate::Place::new(out)
314    }
315}
316
317impl miniserde::de::Visitor for crate::Place<ShippingRateTaxBehavior> {
318    fn string(&mut self, s: &str) -> miniserde::Result<()> {
319        use std::str::FromStr;
320        self.out = Some(ShippingRateTaxBehavior::from_str(s).expect("infallible"));
321        Ok(())
322    }
323}
324
325stripe_types::impl_from_val_with_from_str!(ShippingRateTaxBehavior);
326#[cfg(feature = "deserialize")]
327impl<'de> serde::Deserialize<'de> for ShippingRateTaxBehavior {
328    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
329        use std::str::FromStr;
330        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
331        Ok(Self::from_str(&s).expect("infallible"))
332    }
333}
334#[derive(Clone, Eq, PartialEq)]
335#[non_exhaustive]
336pub enum ShippingRateType {
337    FixedAmount,
338    /// An unrecognized value from Stripe. Should not be used as a request parameter.
339    Unknown(String),
340}
341impl ShippingRateType {
342    pub fn as_str(&self) -> &str {
343        use ShippingRateType::*;
344        match self {
345            FixedAmount => "fixed_amount",
346            Unknown(v) => v,
347        }
348    }
349}
350
351impl std::str::FromStr for ShippingRateType {
352    type Err = std::convert::Infallible;
353    fn from_str(s: &str) -> Result<Self, Self::Err> {
354        use ShippingRateType::*;
355        match s {
356            "fixed_amount" => Ok(FixedAmount),
357            v => {
358                tracing::warn!("Unknown value '{}' for enum '{}'", v, "ShippingRateType");
359                Ok(Unknown(v.to_owned()))
360            }
361        }
362    }
363}
364impl std::fmt::Display for ShippingRateType {
365    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
366        f.write_str(self.as_str())
367    }
368}
369
370#[cfg(not(feature = "redact-generated-debug"))]
371impl std::fmt::Debug for ShippingRateType {
372    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
373        f.write_str(self.as_str())
374    }
375}
376#[cfg(feature = "redact-generated-debug")]
377impl std::fmt::Debug for ShippingRateType {
378    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
379        f.debug_struct(stringify!(ShippingRateType)).finish_non_exhaustive()
380    }
381}
382impl serde::Serialize for ShippingRateType {
383    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
384    where
385        S: serde::Serializer,
386    {
387        serializer.serialize_str(self.as_str())
388    }
389}
390impl miniserde::Deserialize for ShippingRateType {
391    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
392        crate::Place::new(out)
393    }
394}
395
396impl miniserde::de::Visitor for crate::Place<ShippingRateType> {
397    fn string(&mut self, s: &str) -> miniserde::Result<()> {
398        use std::str::FromStr;
399        self.out = Some(ShippingRateType::from_str(s).expect("infallible"));
400        Ok(())
401    }
402}
403
404stripe_types::impl_from_val_with_from_str!(ShippingRateType);
405#[cfg(feature = "deserialize")]
406impl<'de> serde::Deserialize<'de> for ShippingRateType {
407    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
408        use std::str::FromStr;
409        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
410        Ok(Self::from_str(&s).expect("infallible"))
411    }
412}