Skip to main content

stripe_shared/
mandate_upi.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 MandateUpi {
6    /// Amount to be charged for future payments.
7    pub amount: Option<i64>,
8    /// One of `fixed` or `maximum`.
9    /// If `fixed`, the `amount` param refers to the exact amount to be charged in future payments.
10    /// If `maximum`, the amount charged can be up to the value passed for the `amount` param.
11    pub amount_type: Option<MandateUpiAmountType>,
12    /// A description of the mandate or subscription that is meant to be displayed to the customer.
13    pub description: Option<String>,
14    /// End date of the mandate or subscription.
15    pub end_date: Option<stripe_types::Timestamp>,
16}
17#[cfg(feature = "redact-generated-debug")]
18impl std::fmt::Debug for MandateUpi {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        f.debug_struct("MandateUpi").finish_non_exhaustive()
21    }
22}
23#[doc(hidden)]
24pub struct MandateUpiBuilder {
25    amount: Option<Option<i64>>,
26    amount_type: Option<Option<MandateUpiAmountType>>,
27    description: Option<Option<String>>,
28    end_date: Option<Option<stripe_types::Timestamp>>,
29}
30
31#[allow(
32    unused_variables,
33    irrefutable_let_patterns,
34    clippy::let_unit_value,
35    clippy::match_single_binding,
36    clippy::single_match
37)]
38const _: () = {
39    use miniserde::de::{Map, Visitor};
40    use miniserde::json::Value;
41    use miniserde::{Deserialize, Result, make_place};
42    use stripe_types::miniserde_helpers::FromValueOpt;
43    use stripe_types::{MapBuilder, ObjectDeser};
44
45    make_place!(Place);
46
47    impl Deserialize for MandateUpi {
48        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
49            Place::new(out)
50        }
51    }
52
53    struct Builder<'a> {
54        out: &'a mut Option<MandateUpi>,
55        builder: MandateUpiBuilder,
56    }
57
58    impl Visitor for Place<MandateUpi> {
59        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
60            Ok(Box::new(Builder {
61                out: &mut self.out,
62                builder: MandateUpiBuilder::deser_default(),
63            }))
64        }
65    }
66
67    impl MapBuilder for MandateUpiBuilder {
68        type Out = MandateUpi;
69        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
70            Ok(match k {
71                "amount" => Deserialize::begin(&mut self.amount),
72                "amount_type" => Deserialize::begin(&mut self.amount_type),
73                "description" => Deserialize::begin(&mut self.description),
74                "end_date" => Deserialize::begin(&mut self.end_date),
75                _ => <dyn Visitor>::ignore(),
76            })
77        }
78
79        fn deser_default() -> Self {
80            Self {
81                amount: Some(None),
82                amount_type: Some(None),
83                description: Some(None),
84                end_date: Some(None),
85            }
86        }
87
88        fn take_out(&mut self) -> Option<Self::Out> {
89            let (Some(amount), Some(amount_type), Some(description), Some(end_date)) =
90                (self.amount, self.amount_type.take(), self.description.take(), self.end_date)
91            else {
92                return None;
93            };
94            Some(Self::Out { amount, amount_type, description, end_date })
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 MandateUpi {
110        type Builder = MandateUpiBuilder;
111    }
112
113    impl FromValueOpt for MandateUpi {
114        fn from_value(v: Value) -> Option<Self> {
115            let Value::Object(obj) = v else {
116                return None;
117            };
118            let mut b = MandateUpiBuilder::deser_default();
119            for (k, v) in obj {
120                match k.as_str() {
121                    "amount" => b.amount = FromValueOpt::from_value(v),
122                    "amount_type" => b.amount_type = FromValueOpt::from_value(v),
123                    "description" => b.description = FromValueOpt::from_value(v),
124                    "end_date" => b.end_date = FromValueOpt::from_value(v),
125                    _ => {}
126                }
127            }
128            b.take_out()
129        }
130    }
131};
132/// One of `fixed` or `maximum`.
133/// If `fixed`, the `amount` param refers to the exact amount to be charged in future payments.
134/// If `maximum`, the amount charged can be up to the value passed for the `amount` param.
135#[derive(Clone, Eq, PartialEq)]
136#[non_exhaustive]
137pub enum MandateUpiAmountType {
138    Fixed,
139    Maximum,
140    /// An unrecognized value from Stripe. Should not be used as a request parameter.
141    Unknown(String),
142}
143impl MandateUpiAmountType {
144    pub fn as_str(&self) -> &str {
145        use MandateUpiAmountType::*;
146        match self {
147            Fixed => "fixed",
148            Maximum => "maximum",
149            Unknown(v) => v,
150        }
151    }
152}
153
154impl std::str::FromStr for MandateUpiAmountType {
155    type Err = std::convert::Infallible;
156    fn from_str(s: &str) -> Result<Self, Self::Err> {
157        use MandateUpiAmountType::*;
158        match s {
159            "fixed" => Ok(Fixed),
160            "maximum" => Ok(Maximum),
161            v => {
162                tracing::warn!("Unknown value '{}' for enum '{}'", v, "MandateUpiAmountType");
163                Ok(Unknown(v.to_owned()))
164            }
165        }
166    }
167}
168impl std::fmt::Display for MandateUpiAmountType {
169    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
170        f.write_str(self.as_str())
171    }
172}
173
174#[cfg(not(feature = "redact-generated-debug"))]
175impl std::fmt::Debug for MandateUpiAmountType {
176    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
177        f.write_str(self.as_str())
178    }
179}
180#[cfg(feature = "redact-generated-debug")]
181impl std::fmt::Debug for MandateUpiAmountType {
182    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
183        f.debug_struct(stringify!(MandateUpiAmountType)).finish_non_exhaustive()
184    }
185}
186#[cfg(feature = "serialize")]
187impl serde::Serialize for MandateUpiAmountType {
188    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
189    where
190        S: serde::Serializer,
191    {
192        serializer.serialize_str(self.as_str())
193    }
194}
195impl miniserde::Deserialize for MandateUpiAmountType {
196    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
197        crate::Place::new(out)
198    }
199}
200
201impl miniserde::de::Visitor for crate::Place<MandateUpiAmountType> {
202    fn string(&mut self, s: &str) -> miniserde::Result<()> {
203        use std::str::FromStr;
204        self.out = Some(MandateUpiAmountType::from_str(s).expect("infallible"));
205        Ok(())
206    }
207}
208
209stripe_types::impl_from_val_with_from_str!(MandateUpiAmountType);
210#[cfg(feature = "deserialize")]
211impl<'de> serde::Deserialize<'de> for MandateUpiAmountType {
212    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
213        use std::str::FromStr;
214        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
215        Ok(Self::from_str(&s).expect("infallible"))
216    }
217}