stripe_shared/
subscriptions_trials_resource_end_behavior.rs

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