stripe_shared/
billing_credit_grants_resource_scope.rs

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