Skip to main content

stripe_shared/
internal_card.rs

1#[derive(Clone, Eq, PartialEq)]
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 InternalCard {
6    /// Brand of the card used in the transaction
7    pub brand: Option<String>,
8    /// Two-letter ISO code representing the country of the card
9    pub country: Option<String>,
10    /// Two digit number representing the card's expiration month
11    pub exp_month: Option<i64>,
12    /// Two digit number representing the card's expiration year
13    pub exp_year: Option<i64>,
14    /// The last 4 digits of the card
15    pub last4: Option<String>,
16}
17#[cfg(feature = "redact-generated-debug")]
18impl std::fmt::Debug for InternalCard {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        f.debug_struct("InternalCard").finish_non_exhaustive()
21    }
22}
23#[doc(hidden)]
24pub struct InternalCardBuilder {
25    brand: Option<Option<String>>,
26    country: Option<Option<String>>,
27    exp_month: Option<Option<i64>>,
28    exp_year: Option<Option<i64>>,
29    last4: Option<Option<String>>,
30}
31
32#[allow(
33    unused_variables,
34    irrefutable_let_patterns,
35    clippy::let_unit_value,
36    clippy::match_single_binding,
37    clippy::single_match
38)]
39const _: () = {
40    use miniserde::de::{Map, Visitor};
41    use miniserde::json::Value;
42    use miniserde::{Deserialize, Result, make_place};
43    use stripe_types::miniserde_helpers::FromValueOpt;
44    use stripe_types::{MapBuilder, ObjectDeser};
45
46    make_place!(Place);
47
48    impl Deserialize for InternalCard {
49        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
50            Place::new(out)
51        }
52    }
53
54    struct Builder<'a> {
55        out: &'a mut Option<InternalCard>,
56        builder: InternalCardBuilder,
57    }
58
59    impl Visitor for Place<InternalCard> {
60        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
61            Ok(Box::new(Builder {
62                out: &mut self.out,
63                builder: InternalCardBuilder::deser_default(),
64            }))
65        }
66    }
67
68    impl MapBuilder for InternalCardBuilder {
69        type Out = InternalCard;
70        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
71            Ok(match k {
72                "brand" => Deserialize::begin(&mut self.brand),
73                "country" => Deserialize::begin(&mut self.country),
74                "exp_month" => Deserialize::begin(&mut self.exp_month),
75                "exp_year" => Deserialize::begin(&mut self.exp_year),
76                "last4" => Deserialize::begin(&mut self.last4),
77                _ => <dyn Visitor>::ignore(),
78            })
79        }
80
81        fn deser_default() -> Self {
82            Self {
83                brand: Some(None),
84                country: Some(None),
85                exp_month: Some(None),
86                exp_year: Some(None),
87                last4: Some(None),
88            }
89        }
90
91        fn take_out(&mut self) -> Option<Self::Out> {
92            let (Some(brand), Some(country), Some(exp_month), Some(exp_year), Some(last4)) = (
93                self.brand.take(),
94                self.country.take(),
95                self.exp_month,
96                self.exp_year,
97                self.last4.take(),
98            ) else {
99                return None;
100            };
101            Some(Self::Out { brand, country, exp_month, exp_year, last4 })
102        }
103    }
104
105    impl Map for Builder<'_> {
106        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
107            self.builder.key(k)
108        }
109
110        fn finish(&mut self) -> Result<()> {
111            *self.out = self.builder.take_out();
112            Ok(())
113        }
114    }
115
116    impl ObjectDeser for InternalCard {
117        type Builder = InternalCardBuilder;
118    }
119
120    impl FromValueOpt for InternalCard {
121        fn from_value(v: Value) -> Option<Self> {
122            let Value::Object(obj) = v else {
123                return None;
124            };
125            let mut b = InternalCardBuilder::deser_default();
126            for (k, v) in obj {
127                match k.as_str() {
128                    "brand" => b.brand = FromValueOpt::from_value(v),
129                    "country" => b.country = FromValueOpt::from_value(v),
130                    "exp_month" => b.exp_month = FromValueOpt::from_value(v),
131                    "exp_year" => b.exp_year = FromValueOpt::from_value(v),
132                    "last4" => b.last4 = FromValueOpt::from_value(v),
133                    _ => {}
134                }
135            }
136            b.take_out()
137        }
138    }
139};