Skip to main content

stripe_shared/
payment_data.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 PaymentData {
6    /// An arbitrary string attached to the destination payment. Often useful for displaying to users.
7    pub description: Option<String>,
8    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
9    /// This can be useful for storing additional information about the object in a structured format.
10    pub metadata: Option<std::collections::HashMap<String, String>>,
11}
12#[cfg(feature = "redact-generated-debug")]
13impl std::fmt::Debug for PaymentData {
14    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15        f.debug_struct("PaymentData").finish_non_exhaustive()
16    }
17}
18#[doc(hidden)]
19pub struct PaymentDataBuilder {
20    description: Option<Option<String>>,
21    metadata: Option<Option<std::collections::HashMap<String, String>>>,
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 PaymentData {
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<PaymentData>,
48        builder: PaymentDataBuilder,
49    }
50
51    impl Visitor for Place<PaymentData> {
52        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
53            Ok(Box::new(Builder {
54                out: &mut self.out,
55                builder: PaymentDataBuilder::deser_default(),
56            }))
57        }
58    }
59
60    impl MapBuilder for PaymentDataBuilder {
61        type Out = PaymentData;
62        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
63            Ok(match k {
64                "description" => Deserialize::begin(&mut self.description),
65                "metadata" => Deserialize::begin(&mut self.metadata),
66                _ => <dyn Visitor>::ignore(),
67            })
68        }
69
70        fn deser_default() -> Self {
71            Self { description: Some(None), metadata: Some(None) }
72        }
73
74        fn take_out(&mut self) -> Option<Self::Out> {
75            let (Some(description), Some(metadata)) =
76                (self.description.take(), self.metadata.take())
77            else {
78                return None;
79            };
80            Some(Self::Out { description, metadata })
81        }
82    }
83
84    impl Map for Builder<'_> {
85        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
86            self.builder.key(k)
87        }
88
89        fn finish(&mut self) -> Result<()> {
90            *self.out = self.builder.take_out();
91            Ok(())
92        }
93    }
94
95    impl ObjectDeser for PaymentData {
96        type Builder = PaymentDataBuilder;
97    }
98
99    impl FromValueOpt for PaymentData {
100        fn from_value(v: Value) -> Option<Self> {
101            let Value::Object(obj) = v else {
102                return None;
103            };
104            let mut b = PaymentDataBuilder::deser_default();
105            for (k, v) in obj {
106                match k.as_str() {
107                    "description" => b.description = FromValueOpt::from_value(v),
108                    "metadata" => b.metadata = FromValueOpt::from_value(v),
109                    _ => {}
110                }
111            }
112            b.take_out()
113        }
114    }
115};