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