stripe_misc/tax_registration/
types.rs

1/// A Tax `Registration` lets us know that your business is registered to collect tax on payments within a region, enabling you to [automatically collect tax](https://stripe.com/docs/tax).
2///
3/// Stripe doesn't register on your behalf with the relevant authorities when you create a Tax `Registration` object.
4/// For more information on how to register to collect tax, see [our guide](https://stripe.com/docs/tax/registering).
5///
6/// Related guide: [Using the Registrations API](https://stripe.com/docs/tax/registrations-api)
7#[derive(Clone, Debug)]
8#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
9pub struct TaxRegistration {
10    /// Time at which the registration becomes active. Measured in seconds since the Unix epoch.
11    pub active_from: stripe_types::Timestamp,
12    /// Two-letter country code ([ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
13    pub country: String,
14    pub country_options: stripe_misc::TaxProductRegistrationsResourceCountryOptions,
15    /// Time at which the object was created. Measured in seconds since the Unix epoch.
16    pub created: stripe_types::Timestamp,
17    /// If set, the registration stops being active at this time.
18    /// If not set, the registration will be active indefinitely.
19    /// Measured in seconds since the Unix epoch.
20    pub expires_at: Option<stripe_types::Timestamp>,
21    /// Unique identifier for the object.
22    pub id: stripe_misc::TaxRegistrationId,
23    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
24    pub livemode: bool,
25    /// The status of the registration.
26    /// This field is present for convenience and can be deduced from `active_from` and `expires_at`.
27    pub status: TaxRegistrationStatus,
28}
29#[doc(hidden)]
30pub struct TaxRegistrationBuilder {
31    active_from: Option<stripe_types::Timestamp>,
32    country: Option<String>,
33    country_options: Option<stripe_misc::TaxProductRegistrationsResourceCountryOptions>,
34    created: Option<stripe_types::Timestamp>,
35    expires_at: Option<Option<stripe_types::Timestamp>>,
36    id: Option<stripe_misc::TaxRegistrationId>,
37    livemode: Option<bool>,
38    status: Option<TaxRegistrationStatus>,
39}
40
41#[allow(
42    unused_variables,
43    irrefutable_let_patterns,
44    clippy::let_unit_value,
45    clippy::match_single_binding,
46    clippy::single_match
47)]
48const _: () = {
49    use miniserde::de::{Map, Visitor};
50    use miniserde::json::Value;
51    use miniserde::{make_place, Deserialize, Result};
52    use stripe_types::miniserde_helpers::FromValueOpt;
53    use stripe_types::{MapBuilder, ObjectDeser};
54
55    make_place!(Place);
56
57    impl Deserialize for TaxRegistration {
58        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
59            Place::new(out)
60        }
61    }
62
63    struct Builder<'a> {
64        out: &'a mut Option<TaxRegistration>,
65        builder: TaxRegistrationBuilder,
66    }
67
68    impl Visitor for Place<TaxRegistration> {
69        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
70            Ok(Box::new(Builder {
71                out: &mut self.out,
72                builder: TaxRegistrationBuilder::deser_default(),
73            }))
74        }
75    }
76
77    impl MapBuilder for TaxRegistrationBuilder {
78        type Out = TaxRegistration;
79        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
80            Ok(match k {
81                "active_from" => Deserialize::begin(&mut self.active_from),
82                "country" => Deserialize::begin(&mut self.country),
83                "country_options" => Deserialize::begin(&mut self.country_options),
84                "created" => Deserialize::begin(&mut self.created),
85                "expires_at" => Deserialize::begin(&mut self.expires_at),
86                "id" => Deserialize::begin(&mut self.id),
87                "livemode" => Deserialize::begin(&mut self.livemode),
88                "status" => Deserialize::begin(&mut self.status),
89
90                _ => <dyn Visitor>::ignore(),
91            })
92        }
93
94        fn deser_default() -> Self {
95            Self {
96                active_from: Deserialize::default(),
97                country: Deserialize::default(),
98                country_options: Deserialize::default(),
99                created: Deserialize::default(),
100                expires_at: Deserialize::default(),
101                id: Deserialize::default(),
102                livemode: Deserialize::default(),
103                status: Deserialize::default(),
104            }
105        }
106
107        fn take_out(&mut self) -> Option<Self::Out> {
108            let (
109                Some(active_from),
110                Some(country),
111                Some(country_options),
112                Some(created),
113                Some(expires_at),
114                Some(id),
115                Some(livemode),
116                Some(status),
117            ) = (
118                self.active_from,
119                self.country.take(),
120                self.country_options.take(),
121                self.created,
122                self.expires_at,
123                self.id.take(),
124                self.livemode,
125                self.status,
126            )
127            else {
128                return None;
129            };
130            Some(Self::Out {
131                active_from,
132                country,
133                country_options,
134                created,
135                expires_at,
136                id,
137                livemode,
138                status,
139            })
140        }
141    }
142
143    impl Map for Builder<'_> {
144        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
145            self.builder.key(k)
146        }
147
148        fn finish(&mut self) -> Result<()> {
149            *self.out = self.builder.take_out();
150            Ok(())
151        }
152    }
153
154    impl ObjectDeser for TaxRegistration {
155        type Builder = TaxRegistrationBuilder;
156    }
157
158    impl FromValueOpt for TaxRegistration {
159        fn from_value(v: Value) -> Option<Self> {
160            let Value::Object(obj) = v else {
161                return None;
162            };
163            let mut b = TaxRegistrationBuilder::deser_default();
164            for (k, v) in obj {
165                match k.as_str() {
166                    "active_from" => b.active_from = FromValueOpt::from_value(v),
167                    "country" => b.country = FromValueOpt::from_value(v),
168                    "country_options" => b.country_options = FromValueOpt::from_value(v),
169                    "created" => b.created = FromValueOpt::from_value(v),
170                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
171                    "id" => b.id = FromValueOpt::from_value(v),
172                    "livemode" => b.livemode = FromValueOpt::from_value(v),
173                    "status" => b.status = FromValueOpt::from_value(v),
174
175                    _ => {}
176                }
177            }
178            b.take_out()
179        }
180    }
181};
182#[cfg(feature = "serialize")]
183impl serde::Serialize for TaxRegistration {
184    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
185        use serde::ser::SerializeStruct;
186        let mut s = s.serialize_struct("TaxRegistration", 9)?;
187        s.serialize_field("active_from", &self.active_from)?;
188        s.serialize_field("country", &self.country)?;
189        s.serialize_field("country_options", &self.country_options)?;
190        s.serialize_field("created", &self.created)?;
191        s.serialize_field("expires_at", &self.expires_at)?;
192        s.serialize_field("id", &self.id)?;
193        s.serialize_field("livemode", &self.livemode)?;
194        s.serialize_field("status", &self.status)?;
195
196        s.serialize_field("object", "tax.registration")?;
197        s.end()
198    }
199}
200/// The status of the registration.
201/// This field is present for convenience and can be deduced from `active_from` and `expires_at`.
202#[derive(Copy, Clone, Eq, PartialEq)]
203pub enum TaxRegistrationStatus {
204    Active,
205    Expired,
206    Scheduled,
207}
208impl TaxRegistrationStatus {
209    pub fn as_str(self) -> &'static str {
210        use TaxRegistrationStatus::*;
211        match self {
212            Active => "active",
213            Expired => "expired",
214            Scheduled => "scheduled",
215        }
216    }
217}
218
219impl std::str::FromStr for TaxRegistrationStatus {
220    type Err = stripe_types::StripeParseError;
221    fn from_str(s: &str) -> Result<Self, Self::Err> {
222        use TaxRegistrationStatus::*;
223        match s {
224            "active" => Ok(Active),
225            "expired" => Ok(Expired),
226            "scheduled" => Ok(Scheduled),
227            _ => Err(stripe_types::StripeParseError),
228        }
229    }
230}
231impl std::fmt::Display for TaxRegistrationStatus {
232    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
233        f.write_str(self.as_str())
234    }
235}
236
237impl std::fmt::Debug for TaxRegistrationStatus {
238    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
239        f.write_str(self.as_str())
240    }
241}
242#[cfg(feature = "serialize")]
243impl serde::Serialize for TaxRegistrationStatus {
244    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
245    where
246        S: serde::Serializer,
247    {
248        serializer.serialize_str(self.as_str())
249    }
250}
251impl miniserde::Deserialize for TaxRegistrationStatus {
252    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
253        crate::Place::new(out)
254    }
255}
256
257impl miniserde::de::Visitor for crate::Place<TaxRegistrationStatus> {
258    fn string(&mut self, s: &str) -> miniserde::Result<()> {
259        use std::str::FromStr;
260        self.out = Some(TaxRegistrationStatus::from_str(s).map_err(|_| miniserde::Error)?);
261        Ok(())
262    }
263}
264
265stripe_types::impl_from_val_with_from_str!(TaxRegistrationStatus);
266#[cfg(feature = "deserialize")]
267impl<'de> serde::Deserialize<'de> for TaxRegistrationStatus {
268    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
269        use std::str::FromStr;
270        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
271        Self::from_str(&s)
272            .map_err(|_| serde::de::Error::custom("Unknown value for TaxRegistrationStatus"))
273    }
274}
275impl stripe_types::Object for TaxRegistration {
276    type Id = stripe_misc::TaxRegistrationId;
277    fn id(&self) -> &Self::Id {
278        &self.id
279    }
280
281    fn into_id(self) -> Self::Id {
282        self.id
283    }
284}
285stripe_types::def_id!(TaxRegistrationId);