stripe_shared/
recurring.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct Recurring {
5    /// The frequency at which a subscription is billed. One of `day`, `week`, `month` or `year`.
6    pub interval: RecurringInterval,
7    /// The number of intervals (specified in the `interval` attribute) between subscription billings.
8    /// For example, `interval=month` and `interval_count=3` bills every 3 months.
9    pub interval_count: u64,
10    /// The meter tracking the usage of a metered price
11    pub meter: Option<String>,
12    /// Default number of trial days when subscribing a customer to this price using [`trial_from_plan=true`](https://stripe.com/docs/api#create_subscription-trial_from_plan).
13    pub trial_period_days: Option<u32>,
14    /// Configures how the quantity per period should be determined.
15    /// Can be either `metered` or `licensed`.
16    /// `licensed` automatically bills the `quantity` set when adding it to a subscription.
17    /// `metered` aggregates the total usage based on usage records.
18    /// Defaults to `licensed`.
19    pub usage_type: RecurringUsageType,
20}
21#[doc(hidden)]
22pub struct RecurringBuilder {
23    interval: Option<RecurringInterval>,
24    interval_count: Option<u64>,
25    meter: Option<Option<String>>,
26    trial_period_days: Option<Option<u32>>,
27    usage_type: Option<RecurringUsageType>,
28}
29
30#[allow(
31    unused_variables,
32    irrefutable_let_patterns,
33    clippy::let_unit_value,
34    clippy::match_single_binding,
35    clippy::single_match
36)]
37const _: () = {
38    use miniserde::de::{Map, Visitor};
39    use miniserde::json::Value;
40    use miniserde::{make_place, Deserialize, Result};
41    use stripe_types::miniserde_helpers::FromValueOpt;
42    use stripe_types::{MapBuilder, ObjectDeser};
43
44    make_place!(Place);
45
46    impl Deserialize for Recurring {
47        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
48            Place::new(out)
49        }
50    }
51
52    struct Builder<'a> {
53        out: &'a mut Option<Recurring>,
54        builder: RecurringBuilder,
55    }
56
57    impl Visitor for Place<Recurring> {
58        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
59            Ok(Box::new(Builder { out: &mut self.out, builder: RecurringBuilder::deser_default() }))
60        }
61    }
62
63    impl MapBuilder for RecurringBuilder {
64        type Out = Recurring;
65        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
66            Ok(match k {
67                "interval" => Deserialize::begin(&mut self.interval),
68                "interval_count" => Deserialize::begin(&mut self.interval_count),
69                "meter" => Deserialize::begin(&mut self.meter),
70                "trial_period_days" => Deserialize::begin(&mut self.trial_period_days),
71                "usage_type" => Deserialize::begin(&mut self.usage_type),
72
73                _ => <dyn Visitor>::ignore(),
74            })
75        }
76
77        fn deser_default() -> Self {
78            Self {
79                interval: Deserialize::default(),
80                interval_count: Deserialize::default(),
81                meter: Deserialize::default(),
82                trial_period_days: Deserialize::default(),
83                usage_type: Deserialize::default(),
84            }
85        }
86
87        fn take_out(&mut self) -> Option<Self::Out> {
88            let (
89                Some(interval),
90                Some(interval_count),
91                Some(meter),
92                Some(trial_period_days),
93                Some(usage_type),
94            ) = (
95                self.interval,
96                self.interval_count,
97                self.meter.take(),
98                self.trial_period_days,
99                self.usage_type,
100            )
101            else {
102                return None;
103            };
104            Some(Self::Out { interval, interval_count, meter, trial_period_days, usage_type })
105        }
106    }
107
108    impl<'a> Map for Builder<'a> {
109        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
110            self.builder.key(k)
111        }
112
113        fn finish(&mut self) -> Result<()> {
114            *self.out = self.builder.take_out();
115            Ok(())
116        }
117    }
118
119    impl ObjectDeser for Recurring {
120        type Builder = RecurringBuilder;
121    }
122
123    impl FromValueOpt for Recurring {
124        fn from_value(v: Value) -> Option<Self> {
125            let Value::Object(obj) = v else {
126                return None;
127            };
128            let mut b = RecurringBuilder::deser_default();
129            for (k, v) in obj {
130                match k.as_str() {
131                    "interval" => b.interval = FromValueOpt::from_value(v),
132                    "interval_count" => b.interval_count = FromValueOpt::from_value(v),
133                    "meter" => b.meter = FromValueOpt::from_value(v),
134                    "trial_period_days" => b.trial_period_days = FromValueOpt::from_value(v),
135                    "usage_type" => b.usage_type = FromValueOpt::from_value(v),
136
137                    _ => {}
138                }
139            }
140            b.take_out()
141        }
142    }
143};
144/// The frequency at which a subscription is billed. One of `day`, `week`, `month` or `year`.
145#[derive(Copy, Clone, Eq, PartialEq)]
146pub enum RecurringInterval {
147    Day,
148    Month,
149    Week,
150    Year,
151}
152impl RecurringInterval {
153    pub fn as_str(self) -> &'static str {
154        use RecurringInterval::*;
155        match self {
156            Day => "day",
157            Month => "month",
158            Week => "week",
159            Year => "year",
160        }
161    }
162}
163
164impl std::str::FromStr for RecurringInterval {
165    type Err = stripe_types::StripeParseError;
166    fn from_str(s: &str) -> Result<Self, Self::Err> {
167        use RecurringInterval::*;
168        match s {
169            "day" => Ok(Day),
170            "month" => Ok(Month),
171            "week" => Ok(Week),
172            "year" => Ok(Year),
173            _ => Err(stripe_types::StripeParseError),
174        }
175    }
176}
177impl std::fmt::Display for RecurringInterval {
178    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
179        f.write_str(self.as_str())
180    }
181}
182
183impl std::fmt::Debug for RecurringInterval {
184    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
185        f.write_str(self.as_str())
186    }
187}
188#[cfg(feature = "serialize")]
189impl serde::Serialize for RecurringInterval {
190    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
191    where
192        S: serde::Serializer,
193    {
194        serializer.serialize_str(self.as_str())
195    }
196}
197impl miniserde::Deserialize for RecurringInterval {
198    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
199        crate::Place::new(out)
200    }
201}
202
203impl miniserde::de::Visitor for crate::Place<RecurringInterval> {
204    fn string(&mut self, s: &str) -> miniserde::Result<()> {
205        use std::str::FromStr;
206        self.out = Some(RecurringInterval::from_str(s).map_err(|_| miniserde::Error)?);
207        Ok(())
208    }
209}
210
211stripe_types::impl_from_val_with_from_str!(RecurringInterval);
212#[cfg(feature = "deserialize")]
213impl<'de> serde::Deserialize<'de> for RecurringInterval {
214    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
215        use std::str::FromStr;
216        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
217        Self::from_str(&s)
218            .map_err(|_| serde::de::Error::custom("Unknown value for RecurringInterval"))
219    }
220}
221/// Configures how the quantity per period should be determined.
222/// Can be either `metered` or `licensed`.
223/// `licensed` automatically bills the `quantity` set when adding it to a subscription.
224/// `metered` aggregates the total usage based on usage records.
225/// Defaults to `licensed`.
226#[derive(Copy, Clone, Eq, PartialEq)]
227pub enum RecurringUsageType {
228    Licensed,
229    Metered,
230}
231impl RecurringUsageType {
232    pub fn as_str(self) -> &'static str {
233        use RecurringUsageType::*;
234        match self {
235            Licensed => "licensed",
236            Metered => "metered",
237        }
238    }
239}
240
241impl std::str::FromStr for RecurringUsageType {
242    type Err = stripe_types::StripeParseError;
243    fn from_str(s: &str) -> Result<Self, Self::Err> {
244        use RecurringUsageType::*;
245        match s {
246            "licensed" => Ok(Licensed),
247            "metered" => Ok(Metered),
248            _ => Err(stripe_types::StripeParseError),
249        }
250    }
251}
252impl std::fmt::Display for RecurringUsageType {
253    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
254        f.write_str(self.as_str())
255    }
256}
257
258impl std::fmt::Debug for RecurringUsageType {
259    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
260        f.write_str(self.as_str())
261    }
262}
263#[cfg(feature = "serialize")]
264impl serde::Serialize for RecurringUsageType {
265    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
266    where
267        S: serde::Serializer,
268    {
269        serializer.serialize_str(self.as_str())
270    }
271}
272impl miniserde::Deserialize for RecurringUsageType {
273    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
274        crate::Place::new(out)
275    }
276}
277
278impl miniserde::de::Visitor for crate::Place<RecurringUsageType> {
279    fn string(&mut self, s: &str) -> miniserde::Result<()> {
280        use std::str::FromStr;
281        self.out = Some(RecurringUsageType::from_str(s).map_err(|_| miniserde::Error)?);
282        Ok(())
283    }
284}
285
286stripe_types::impl_from_val_with_from_str!(RecurringUsageType);
287#[cfg(feature = "deserialize")]
288impl<'de> serde::Deserialize<'de> for RecurringUsageType {
289    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
290        use std::str::FromStr;
291        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
292        Self::from_str(&s)
293            .map_err(|_| serde::de::Error::custom("Unknown value for RecurringUsageType"))
294    }
295}