stripe_shared/
credit_note_refund.rs

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