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