stripe_shared/
internal_card.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct InternalCard {
5    /// Brand of the card used in the transaction
6    pub brand: Option<String>,
7    /// Two-letter ISO code representing the country of the card
8    pub country: Option<String>,
9    /// Two digit number representing the card's expiration month
10    pub exp_month: Option<i64>,
11    /// Two digit number representing the card's expiration year
12    pub exp_year: Option<i64>,
13    /// The last 4 digits of the card
14    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::{make_place, Deserialize, Result};
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
71                _ => <dyn Visitor>::ignore(),
72            })
73        }
74
75        fn deser_default() -> Self {
76            Self {
77                brand: Deserialize::default(),
78                country: Deserialize::default(),
79                exp_month: Deserialize::default(),
80                exp_year: Deserialize::default(),
81                last4: Deserialize::default(),
82            }
83        }
84
85        fn take_out(&mut self) -> Option<Self::Out> {
86            let (Some(brand), Some(country), Some(exp_month), Some(exp_year), Some(last4)) = (
87                self.brand.take(),
88                self.country.take(),
89                self.exp_month,
90                self.exp_year,
91                self.last4.take(),
92            ) else {
93                return None;
94            };
95            Some(Self::Out { brand, country, exp_month, exp_year, last4 })
96        }
97    }
98
99    impl<'a> Map for Builder<'a> {
100        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
101            self.builder.key(k)
102        }
103
104        fn finish(&mut self) -> Result<()> {
105            *self.out = self.builder.take_out();
106            Ok(())
107        }
108    }
109
110    impl ObjectDeser for InternalCard {
111        type Builder = InternalCardBuilder;
112    }
113
114    impl FromValueOpt for InternalCard {
115        fn from_value(v: Value) -> Option<Self> {
116            let Value::Object(obj) = v else {
117                return None;
118            };
119            let mut b = InternalCardBuilder::deser_default();
120            for (k, v) in obj {
121                match k.as_str() {
122                    "brand" => b.brand = FromValueOpt::from_value(v),
123                    "country" => b.country = FromValueOpt::from_value(v),
124                    "exp_month" => b.exp_month = FromValueOpt::from_value(v),
125                    "exp_year" => b.exp_year = FromValueOpt::from_value(v),
126                    "last4" => b.last4 = FromValueOpt::from_value(v),
127
128                    _ => {}
129                }
130            }
131            b.take_out()
132        }
133    }
134};