stripe_shared/
external_account.rs1#[derive(Clone)]
3#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
4#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
5#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
6#[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(tag = "object"))]
7pub enum ExternalAccount {
8 #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "bank_account"))]
9 BankAccount(stripe_shared::BankAccount),
10 #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "card"))]
11 Card(stripe_shared::Card),
12}
13
14#[derive(Default)]
15pub struct ExternalAccountBuilder {
16 inner: stripe_types::miniserde_helpers::ObjectBuilderInner,
17}
18
19const _: () = {
20 use miniserde::de::{Map, Visitor};
21 use miniserde::json::Value;
22 use miniserde::{Deserialize, Result, make_place};
23 use stripe_types::MapBuilder;
24 use stripe_types::miniserde_helpers::FromValueOpt;
25
26 use super::*;
27
28 make_place!(Place);
29
30 struct Builder<'a> {
31 out: &'a mut Option<ExternalAccount>,
32 builder: ExternalAccountBuilder,
33 }
34
35 impl Deserialize for ExternalAccount {
36 fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
37 Place::new(out)
38 }
39 }
40
41 impl Visitor for Place<ExternalAccount> {
42 fn map(&mut self) -> Result<Box<dyn Map + '_>> {
43 Ok(Box::new(Builder { out: &mut self.out, builder: Default::default() }))
44 }
45 }
46
47 impl Map for Builder<'_> {
48 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
49 self.builder.key(k)
50 }
51
52 fn finish(&mut self) -> Result<()> {
53 *self.out = self.builder.take_out();
54 Ok(())
55 }
56 }
57
58 impl MapBuilder for ExternalAccountBuilder {
59 type Out = ExternalAccount;
60 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
61 self.inner.key_inner(k)
62 }
63
64 fn deser_default() -> Self {
65 Self::default()
66 }
67
68 fn take_out(&mut self) -> Option<Self::Out> {
69 let (k, o) = self.inner.finish_inner()?;
70 ExternalAccount::construct(&k, o)
71 }
72 }
73
74 impl stripe_types::ObjectDeser for ExternalAccount {
75 type Builder = ExternalAccountBuilder;
76 }
77 impl ExternalAccount {
78 fn construct(key: &str, o: miniserde::json::Object) -> Option<Self> {
79 Some(match key {
80 "bank_account" => Self::BankAccount(FromValueOpt::from_value(Value::Object(o))?),
81 "card" => Self::Card(FromValueOpt::from_value(Value::Object(o))?),
82
83 _ => {
84 tracing::warn!(
85 "Unknown object type '{}' for enum '{}'",
86 key,
87 "ExternalAccount"
88 );
89 return None;
90 }
91 })
92 }
93 }
94
95 impl FromValueOpt for ExternalAccount {
96 fn from_value(v: Value) -> Option<Self> {
97 let (typ, obj) = stripe_types::miniserde_helpers::extract_object_discr(v)?;
98 Self::construct(&typ, obj)
99 }
100 }
101};
102
103#[cfg(feature = "redact-generated-debug")]
104impl std::fmt::Debug for ExternalAccount {
105 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
106 f.debug_struct("ExternalAccount").finish_non_exhaustive()
107 }
108}
109impl stripe_types::Object for ExternalAccount {
110 type Id = smol_str::SmolStr;
111 fn id(&self) -> &Self::Id {
112 match self {
113 Self::BankAccount(v) => v.id.inner(),
114 Self::Card(v) => v.id.inner(),
115 }
116 }
117
118 fn into_id(self) -> Self::Id {
119 match self {
120 Self::BankAccount(v) => v.id.into_inner(),
121 Self::Card(v) => v.id.into_inner(),
122 }
123 }
124}