Skip to main content

stripe_shared/
tax_code.rs

1/// [Tax codes](https://stripe.com/docs/tax/tax-categories) classify goods and services for tax purposes.
2///
3/// For more details see <<https://stripe.com/docs/api/tax_codes/object>>.
4#[derive(Clone, Eq, PartialEq)]
5#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
6#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
7pub struct TaxCode {
8    /// A detailed description of which types of products the tax code represents.
9    pub description: String,
10    /// Unique identifier for the object.
11    pub id: stripe_shared::TaxCodeId,
12    /// A short name for the tax code.
13    pub name: String,
14}
15#[cfg(feature = "redact-generated-debug")]
16impl std::fmt::Debug for TaxCode {
17    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18        f.debug_struct("TaxCode").finish_non_exhaustive()
19    }
20}
21#[doc(hidden)]
22pub struct TaxCodeBuilder {
23    description: Option<String>,
24    id: Option<stripe_shared::TaxCodeId>,
25    name: Option<String>,
26}
27
28#[allow(
29    unused_variables,
30    irrefutable_let_patterns,
31    clippy::let_unit_value,
32    clippy::match_single_binding,
33    clippy::single_match
34)]
35const _: () = {
36    use miniserde::de::{Map, Visitor};
37    use miniserde::json::Value;
38    use miniserde::{Deserialize, Result, make_place};
39    use stripe_types::miniserde_helpers::FromValueOpt;
40    use stripe_types::{MapBuilder, ObjectDeser};
41
42    make_place!(Place);
43
44    impl Deserialize for TaxCode {
45        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
46            Place::new(out)
47        }
48    }
49
50    struct Builder<'a> {
51        out: &'a mut Option<TaxCode>,
52        builder: TaxCodeBuilder,
53    }
54
55    impl Visitor for Place<TaxCode> {
56        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
57            Ok(Box::new(Builder { out: &mut self.out, builder: TaxCodeBuilder::deser_default() }))
58        }
59    }
60
61    impl MapBuilder for TaxCodeBuilder {
62        type Out = TaxCode;
63        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
64            Ok(match k {
65                "description" => Deserialize::begin(&mut self.description),
66                "id" => Deserialize::begin(&mut self.id),
67                "name" => Deserialize::begin(&mut self.name),
68                _ => <dyn Visitor>::ignore(),
69            })
70        }
71
72        fn deser_default() -> Self {
73            Self { description: None, id: None, name: None }
74        }
75
76        fn take_out(&mut self) -> Option<Self::Out> {
77            let (Some(description), Some(id), Some(name)) =
78                (self.description.take(), self.id.take(), self.name.take())
79            else {
80                return None;
81            };
82            Some(Self::Out { description, id, name })
83        }
84    }
85
86    impl Map for Builder<'_> {
87        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
88            self.builder.key(k)
89        }
90
91        fn finish(&mut self) -> Result<()> {
92            *self.out = self.builder.take_out();
93            Ok(())
94        }
95    }
96
97    impl ObjectDeser for TaxCode {
98        type Builder = TaxCodeBuilder;
99    }
100
101    impl FromValueOpt for TaxCode {
102        fn from_value(v: Value) -> Option<Self> {
103            let Value::Object(obj) = v else {
104                return None;
105            };
106            let mut b = TaxCodeBuilder::deser_default();
107            for (k, v) in obj {
108                match k.as_str() {
109                    "description" => b.description = FromValueOpt::from_value(v),
110                    "id" => b.id = FromValueOpt::from_value(v),
111                    "name" => b.name = FromValueOpt::from_value(v),
112                    _ => {}
113                }
114            }
115            b.take_out()
116        }
117    }
118};
119#[cfg(feature = "serialize")]
120impl serde::Serialize for TaxCode {
121    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
122        use serde::ser::SerializeStruct;
123        let mut s = s.serialize_struct("TaxCode", 4)?;
124        s.serialize_field("description", &self.description)?;
125        s.serialize_field("id", &self.id)?;
126        s.serialize_field("name", &self.name)?;
127
128        s.serialize_field("object", "tax_code")?;
129        s.end()
130    }
131}
132impl stripe_types::Object for TaxCode {
133    type Id = stripe_shared::TaxCodeId;
134    fn id(&self) -> &Self::Id {
135        &self.id
136    }
137
138    fn into_id(self) -> Self::Id {
139        self.id
140    }
141}
142stripe_types::def_id!(TaxCodeId);