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::{Deserialize, Result, make_place};
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                _ => <dyn Visitor>::ignore(),
73            })
74        }
75
76        fn deser_default() -> Self {
77            Self {
78                interval: Deserialize::default(),
79                interval_count: Deserialize::default(),
80                meter: Deserialize::default(),
81                trial_period_days: Deserialize::default(),
82                usage_type: Deserialize::default(),
83            }
84        }
85
86        fn take_out(&mut self) -> Option<Self::Out> {
87            let (
88                Some(interval),
89                Some(interval_count),
90                Some(meter),
91                Some(trial_period_days),
92                Some(usage_type),
93            ) = (
94                self.interval,
95                self.interval_count,
96                self.meter.take(),
97                self.trial_period_days,
98                self.usage_type,
99            )
100            else {
101                return None;
102            };
103            Some(Self::Out { interval, interval_count, meter, trial_period_days, usage_type })
104        }
105    }
106
107    impl Map for Builder<'_> {
108        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
109            self.builder.key(k)
110        }
111
112        fn finish(&mut self) -> Result<()> {
113            *self.out = self.builder.take_out();
114            Ok(())
115        }
116    }
117
118    impl ObjectDeser for Recurring {
119        type Builder = RecurringBuilder;
120    }
121
122    impl FromValueOpt for Recurring {
123        fn from_value(v: Value) -> Option<Self> {
124            let Value::Object(obj) = v else {
125                return None;
126            };
127            let mut b = RecurringBuilder::deser_default();
128            for (k, v) in obj {
129                match k.as_str() {
130                    "interval" => b.interval = FromValueOpt::from_value(v),
131                    "interval_count" => b.interval_count = FromValueOpt::from_value(v),
132                    "meter" => b.meter = FromValueOpt::from_value(v),
133                    "trial_period_days" => b.trial_period_days = FromValueOpt::from_value(v),
134                    "usage_type" => b.usage_type = FromValueOpt::from_value(v),
135                    _ => {}
136                }
137            }
138            b.take_out()
139        }
140    }
141};
142/// The frequency at which a subscription is billed. One of `day`, `week`, `month` or `year`.
143#[derive(Copy, Clone, Eq, PartialEq)]
144pub enum RecurringInterval {
145    Day,
146    Month,
147    Week,
148    Year,
149}
150impl RecurringInterval {
151    pub fn as_str(self) -> &'static str {
152        use RecurringInterval::*;
153        match self {
154            Day => "day",
155            Month => "month",
156            Week => "week",
157            Year => "year",
158        }
159    }
160}
161
162impl std::str::FromStr for RecurringInterval {
163    type Err = stripe_types::StripeParseError;
164    fn from_str(s: &str) -> Result<Self, Self::Err> {
165        use RecurringInterval::*;
166        match s {
167            "day" => Ok(Day),
168            "month" => Ok(Month),
169            "week" => Ok(Week),
170            "year" => Ok(Year),
171            _ => Err(stripe_types::StripeParseError),
172        }
173    }
174}
175impl std::fmt::Display for RecurringInterval {
176    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
177        f.write_str(self.as_str())
178    }
179}
180
181impl std::fmt::Debug for RecurringInterval {
182    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
183        f.write_str(self.as_str())
184    }
185}
186#[cfg(feature = "serialize")]
187impl serde::Serialize for RecurringInterval {
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 RecurringInterval {
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<RecurringInterval> {
202    fn string(&mut self, s: &str) -> miniserde::Result<()> {
203        use std::str::FromStr;
204        self.out = Some(RecurringInterval::from_str(s).map_err(|_| miniserde::Error)?);
205        Ok(())
206    }
207}
208
209stripe_types::impl_from_val_with_from_str!(RecurringInterval);
210#[cfg(feature = "deserialize")]
211impl<'de> serde::Deserialize<'de> for RecurringInterval {
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        Self::from_str(&s)
216            .map_err(|_| serde::de::Error::custom("Unknown value for RecurringInterval"))
217    }
218}
219/// Configures how the quantity per period should be determined.
220/// Can be either `metered` or `licensed`.
221/// `licensed` automatically bills the `quantity` set when adding it to a subscription.
222/// `metered` aggregates the total usage based on usage records.
223/// Defaults to `licensed`.
224#[derive(Copy, Clone, Eq, PartialEq)]
225pub enum RecurringUsageType {
226    Licensed,
227    Metered,
228}
229impl RecurringUsageType {
230    pub fn as_str(self) -> &'static str {
231        use RecurringUsageType::*;
232        match self {
233            Licensed => "licensed",
234            Metered => "metered",
235        }
236    }
237}
238
239impl std::str::FromStr for RecurringUsageType {
240    type Err = stripe_types::StripeParseError;
241    fn from_str(s: &str) -> Result<Self, Self::Err> {
242        use RecurringUsageType::*;
243        match s {
244            "licensed" => Ok(Licensed),
245            "metered" => Ok(Metered),
246            _ => Err(stripe_types::StripeParseError),
247        }
248    }
249}
250impl std::fmt::Display for RecurringUsageType {
251    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
252        f.write_str(self.as_str())
253    }
254}
255
256impl std::fmt::Debug for RecurringUsageType {
257    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
258        f.write_str(self.as_str())
259    }
260}
261#[cfg(feature = "serialize")]
262impl serde::Serialize for RecurringUsageType {
263    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
264    where
265        S: serde::Serializer,
266    {
267        serializer.serialize_str(self.as_str())
268    }
269}
270impl miniserde::Deserialize for RecurringUsageType {
271    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
272        crate::Place::new(out)
273    }
274}
275
276impl miniserde::de::Visitor for crate::Place<RecurringUsageType> {
277    fn string(&mut self, s: &str) -> miniserde::Result<()> {
278        use std::str::FromStr;
279        self.out = Some(RecurringUsageType::from_str(s).map_err(|_| miniserde::Error)?);
280        Ok(())
281    }
282}
283
284stripe_types::impl_from_val_with_from_str!(RecurringUsageType);
285#[cfg(feature = "deserialize")]
286impl<'de> serde::Deserialize<'de> for RecurringUsageType {
287    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
288        use std::str::FromStr;
289        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
290        Self::from_str(&s)
291            .map_err(|_| serde::de::Error::custom("Unknown value for RecurringUsageType"))
292    }
293}