Skip to main content

stripe_shared/
promotion_codes_resource_promotion.rs

1#[derive(Clone)]
2#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
3#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct PromotionCodesResourcePromotion {
6    /// If promotion `type` is `coupon`, the coupon for this promotion.
7    pub coupon: Option<stripe_types::Expandable<stripe_shared::Coupon>>,
8    /// The type of promotion.
9    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
10    pub type_: PromotionCodesResourcePromotionType,
11}
12#[cfg(feature = "redact-generated-debug")]
13impl std::fmt::Debug for PromotionCodesResourcePromotion {
14    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15        f.debug_struct("PromotionCodesResourcePromotion").finish_non_exhaustive()
16    }
17}
18#[doc(hidden)]
19pub struct PromotionCodesResourcePromotionBuilder {
20    coupon: Option<Option<stripe_types::Expandable<stripe_shared::Coupon>>>,
21    type_: Option<PromotionCodesResourcePromotionType>,
22}
23
24#[allow(
25    unused_variables,
26    irrefutable_let_patterns,
27    clippy::let_unit_value,
28    clippy::match_single_binding,
29    clippy::single_match
30)]
31const _: () = {
32    use miniserde::de::{Map, Visitor};
33    use miniserde::json::Value;
34    use miniserde::{Deserialize, Result, make_place};
35    use stripe_types::miniserde_helpers::FromValueOpt;
36    use stripe_types::{MapBuilder, ObjectDeser};
37
38    make_place!(Place);
39
40    impl Deserialize for PromotionCodesResourcePromotion {
41        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
42            Place::new(out)
43        }
44    }
45
46    struct Builder<'a> {
47        out: &'a mut Option<PromotionCodesResourcePromotion>,
48        builder: PromotionCodesResourcePromotionBuilder,
49    }
50
51    impl Visitor for Place<PromotionCodesResourcePromotion> {
52        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
53            Ok(Box::new(Builder {
54                out: &mut self.out,
55                builder: PromotionCodesResourcePromotionBuilder::deser_default(),
56            }))
57        }
58    }
59
60    impl MapBuilder for PromotionCodesResourcePromotionBuilder {
61        type Out = PromotionCodesResourcePromotion;
62        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
63            Ok(match k {
64                "coupon" => Deserialize::begin(&mut self.coupon),
65                "type" => Deserialize::begin(&mut self.type_),
66                _ => <dyn Visitor>::ignore(),
67            })
68        }
69
70        fn deser_default() -> Self {
71            Self { coupon: Deserialize::default(), type_: Deserialize::default() }
72        }
73
74        fn take_out(&mut self) -> Option<Self::Out> {
75            let (Some(coupon), Some(type_)) = (self.coupon.take(), self.type_.take()) else {
76                return None;
77            };
78            Some(Self::Out { coupon, type_ })
79        }
80    }
81
82    impl Map for Builder<'_> {
83        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
84            self.builder.key(k)
85        }
86
87        fn finish(&mut self) -> Result<()> {
88            *self.out = self.builder.take_out();
89            Ok(())
90        }
91    }
92
93    impl ObjectDeser for PromotionCodesResourcePromotion {
94        type Builder = PromotionCodesResourcePromotionBuilder;
95    }
96
97    impl FromValueOpt for PromotionCodesResourcePromotion {
98        fn from_value(v: Value) -> Option<Self> {
99            let Value::Object(obj) = v else {
100                return None;
101            };
102            let mut b = PromotionCodesResourcePromotionBuilder::deser_default();
103            for (k, v) in obj {
104                match k.as_str() {
105                    "coupon" => b.coupon = FromValueOpt::from_value(v),
106                    "type" => b.type_ = FromValueOpt::from_value(v),
107                    _ => {}
108                }
109            }
110            b.take_out()
111        }
112    }
113};
114/// The type of promotion.
115#[derive(Clone, Eq, PartialEq)]
116#[non_exhaustive]
117pub enum PromotionCodesResourcePromotionType {
118    Coupon,
119    /// An unrecognized value from Stripe. Should not be used as a request parameter.
120    Unknown(String),
121}
122impl PromotionCodesResourcePromotionType {
123    pub fn as_str(&self) -> &str {
124        use PromotionCodesResourcePromotionType::*;
125        match self {
126            Coupon => "coupon",
127            Unknown(v) => v,
128        }
129    }
130}
131
132impl std::str::FromStr for PromotionCodesResourcePromotionType {
133    type Err = std::convert::Infallible;
134    fn from_str(s: &str) -> Result<Self, Self::Err> {
135        use PromotionCodesResourcePromotionType::*;
136        match s {
137            "coupon" => Ok(Coupon),
138            v => {
139                tracing::warn!(
140                    "Unknown value '{}' for enum '{}'",
141                    v,
142                    "PromotionCodesResourcePromotionType"
143                );
144                Ok(Unknown(v.to_owned()))
145            }
146        }
147    }
148}
149impl std::fmt::Display for PromotionCodesResourcePromotionType {
150    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
151        f.write_str(self.as_str())
152    }
153}
154
155#[cfg(not(feature = "redact-generated-debug"))]
156impl std::fmt::Debug for PromotionCodesResourcePromotionType {
157    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
158        f.write_str(self.as_str())
159    }
160}
161#[cfg(feature = "redact-generated-debug")]
162impl std::fmt::Debug for PromotionCodesResourcePromotionType {
163    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
164        f.debug_struct(stringify!(PromotionCodesResourcePromotionType)).finish_non_exhaustive()
165    }
166}
167#[cfg(feature = "serialize")]
168impl serde::Serialize for PromotionCodesResourcePromotionType {
169    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
170    where
171        S: serde::Serializer,
172    {
173        serializer.serialize_str(self.as_str())
174    }
175}
176impl miniserde::Deserialize for PromotionCodesResourcePromotionType {
177    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
178        crate::Place::new(out)
179    }
180}
181
182impl miniserde::de::Visitor for crate::Place<PromotionCodesResourcePromotionType> {
183    fn string(&mut self, s: &str) -> miniserde::Result<()> {
184        use std::str::FromStr;
185        self.out = Some(PromotionCodesResourcePromotionType::from_str(s).expect("infallible"));
186        Ok(())
187    }
188}
189
190stripe_types::impl_from_val_with_from_str!(PromotionCodesResourcePromotionType);
191#[cfg(feature = "deserialize")]
192impl<'de> serde::Deserialize<'de> for PromotionCodesResourcePromotionType {
193    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
194        use std::str::FromStr;
195        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
196        Ok(Self::from_str(&s).expect("infallible"))
197    }
198}