stripe_shared/
internal_card.rs1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct InternalCard {
5 pub brand: Option<String>,
7 pub country: Option<String>,
9 pub exp_month: Option<i64>,
11 pub exp_year: Option<i64>,
13 pub last4: Option<String>,
15}
16#[doc(hidden)]
17pub struct InternalCardBuilder {
18 brand: Option<Option<String>>,
19 country: Option<Option<String>>,
20 exp_month: Option<Option<i64>>,
21 exp_year: Option<Option<i64>>,
22 last4: Option<Option<String>>,
23}
24
25#[allow(
26 unused_variables,
27 irrefutable_let_patterns,
28 clippy::let_unit_value,
29 clippy::match_single_binding,
30 clippy::single_match
31)]
32const _: () = {
33 use miniserde::de::{Map, Visitor};
34 use miniserde::json::Value;
35 use miniserde::{Deserialize, Result, make_place};
36 use stripe_types::miniserde_helpers::FromValueOpt;
37 use stripe_types::{MapBuilder, ObjectDeser};
38
39 make_place!(Place);
40
41 impl Deserialize for InternalCard {
42 fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
43 Place::new(out)
44 }
45 }
46
47 struct Builder<'a> {
48 out: &'a mut Option<InternalCard>,
49 builder: InternalCardBuilder,
50 }
51
52 impl Visitor for Place<InternalCard> {
53 fn map(&mut self) -> Result<Box<dyn Map + '_>> {
54 Ok(Box::new(Builder {
55 out: &mut self.out,
56 builder: InternalCardBuilder::deser_default(),
57 }))
58 }
59 }
60
61 impl MapBuilder for InternalCardBuilder {
62 type Out = InternalCard;
63 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
64 Ok(match k {
65 "brand" => Deserialize::begin(&mut self.brand),
66 "country" => Deserialize::begin(&mut self.country),
67 "exp_month" => Deserialize::begin(&mut self.exp_month),
68 "exp_year" => Deserialize::begin(&mut self.exp_year),
69 "last4" => Deserialize::begin(&mut self.last4),
70 _ => <dyn Visitor>::ignore(),
71 })
72 }
73
74 fn deser_default() -> Self {
75 Self {
76 brand: Deserialize::default(),
77 country: Deserialize::default(),
78 exp_month: Deserialize::default(),
79 exp_year: Deserialize::default(),
80 last4: Deserialize::default(),
81 }
82 }
83
84 fn take_out(&mut self) -> Option<Self::Out> {
85 let (Some(brand), Some(country), Some(exp_month), Some(exp_year), Some(last4)) = (
86 self.brand.take(),
87 self.country.take(),
88 self.exp_month,
89 self.exp_year,
90 self.last4.take(),
91 ) else {
92 return None;
93 };
94 Some(Self::Out { brand, country, exp_month, exp_year, last4 })
95 }
96 }
97
98 impl Map for Builder<'_> {
99 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
100 self.builder.key(k)
101 }
102
103 fn finish(&mut self) -> Result<()> {
104 *self.out = self.builder.take_out();
105 Ok(())
106 }
107 }
108
109 impl ObjectDeser for InternalCard {
110 type Builder = InternalCardBuilder;
111 }
112
113 impl FromValueOpt for InternalCard {
114 fn from_value(v: Value) -> Option<Self> {
115 let Value::Object(obj) = v else {
116 return None;
117 };
118 let mut b = InternalCardBuilder::deser_default();
119 for (k, v) in obj {
120 match k.as_str() {
121 "brand" => b.brand = FromValueOpt::from_value(v),
122 "country" => b.country = FromValueOpt::from_value(v),
123 "exp_month" => b.exp_month = FromValueOpt::from_value(v),
124 "exp_year" => b.exp_year = FromValueOpt::from_value(v),
125 "last4" => b.last4 = FromValueOpt::from_value(v),
126 _ => {}
127 }
128 }
129 b.take_out()
130 }
131 }
132};