forked from stellar/rs-soroban-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderive_error_enum_int.rs
More file actions
192 lines (176 loc) · 7.03 KB
/
derive_error_enum_int.rs
File metadata and controls
192 lines (176 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use itertools::MultiUnzip;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{ScSpecEntry, ScSpecUdtErrorEnumCaseV0, ScSpecUdtErrorEnumV0, StringM, WriteXdr};
use syn::{spanned::Spanned, Attribute, DataEnum, Error, ExprLit, Ident, Lit, Path};
use crate::{doc::docs_from_attrs, DEFAULT_XDR_RW_LIMITS};
pub fn derive_type_error_enum_int(
path: &Path,
enum_ident: &Ident,
attrs: &[Attribute],
data: &DataEnum,
spec: bool,
lib: &Option<String>,
) -> TokenStream2 {
// Collect errors as they are encountered and emit them at the end.
let mut errors = Vec::<Error>::new();
let variants = &data.variants;
let (spec_cases, try_froms, into_errors, into_invoke_errors): (Vec<_>, Vec<_>, Vec<_>, Vec<_>) = variants
.iter()
.map(|v| {
let ident = &v.ident;
let name = &ident.to_string();
let discriminant: u32 = if let syn::Expr::Lit(ExprLit {
lit: Lit::Int(ref lit_int),
..
}) = v.discriminant.as_ref().unwrap().1
{
lit_int.base10_parse().unwrap_or_else(|_| {
errors.push(Error::new(
lit_int.span(),
"unsupported discriminant value on enum variant, must be parseable as u32",
));
0
})
} else {
errors.push(Error::new(
v.discriminant.as_ref().unwrap().1.span(),
"unsupported discriminant value on enum variant",
));
0
};
let spec_case = ScSpecUdtErrorEnumCaseV0 {
doc: docs_from_attrs(&v.attrs),
name: name.try_into().unwrap_or_else(|_| StringM::default()),
value: discriminant,
};
let try_from = quote! { #discriminant => Self::#ident };
let into_error =
quote! { #enum_ident::#ident => #path::Error::from_contract_error(#discriminant) };
let into_invoke_error =
quote! { #enum_ident::#ident => #path::InvokeError::Contract(#discriminant) };
(spec_case, try_from, into_error, into_invoke_error)
})
.multiunzip();
// If errors have occurred, render them instead.
if !errors.is_empty() {
let compile_errors = errors.iter().map(Error::to_compile_error);
return quote! { #(#compile_errors)* };
}
// Generated code spec.
let spec_gen = if spec {
let spec_entry = ScSpecEntry::UdtErrorEnumV0(ScSpecUdtErrorEnumV0 {
doc: docs_from_attrs(attrs),
lib: lib.as_deref().unwrap_or_default().try_into().unwrap(),
name: enum_ident.to_string().try_into().unwrap(),
cases: spec_cases.try_into().unwrap(),
});
let spec_xdr = spec_entry.to_xdr(DEFAULT_XDR_RW_LIMITS).unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", enum_ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #enum_ident::spec_xdr();
impl #enum_ident {
pub const fn spec_xdr() -> [u8; #spec_xdr_len] {
*#spec_xdr_lit
}
}
})
} else {
None
};
// Output.
quote! {
#spec_gen
impl TryFrom<#path::Error> for #enum_ident {
type Error = #path::Error;
#[inline(always)]
fn try_from(error: #path::Error) -> Result<Self, #path::Error> {
if error.is_type(#path::xdr::ScErrorType::Contract) {
let discriminant = error.get_code();
Ok(match discriminant {
#(#try_froms,)*
_ => return Err(error),
})
} else {
Err(error)
}
}
}
impl TryFrom<&#path::Error> for #enum_ident {
type Error = #path::Error;
#[inline(always)]
fn try_from(error: &#path::Error) -> Result<Self, #path::Error> {
<_ as TryFrom<#path::Error>>::try_from(*error)
}
}
impl From<#enum_ident> for #path::Error {
#[inline(always)]
fn from(val: #enum_ident) -> #path::Error {
<_ as From<&#enum_ident>>::from(&val)
}
}
impl From<&#enum_ident> for #path::Error {
#[inline(always)]
fn from(val: &#enum_ident) -> #path::Error {
match val {
#(#into_errors,)*
}
}
}
impl TryFrom<#path::InvokeError> for #enum_ident {
type Error = #path::InvokeError;
#[inline(always)]
fn try_from(error: #path::InvokeError) -> Result<Self, #path::InvokeError> {
match error {
#path::InvokeError::Abort => Err(error),
#path::InvokeError::Contract(code) => Ok(match code {
#(#try_froms,)*
_ => return Err(error),
}),
}
}
}
impl TryFrom<&#path::InvokeError> for #enum_ident {
type Error = #path::InvokeError;
#[inline(always)]
fn try_from(error: &#path::InvokeError) -> Result<Self, #path::InvokeError> {
<_ as TryFrom<#path::InvokeError>>::try_from(*error)
}
}
impl From<#enum_ident> for #path::InvokeError {
#[inline(always)]
fn from(val: #enum_ident) -> #path::InvokeError {
<_ as From<&#enum_ident>>::from(&val)
}
}
impl From<&#enum_ident> for #path::InvokeError {
#[inline(always)]
fn from(val: &#enum_ident) -> #path::InvokeError {
match val {
#(#into_invoke_errors,)*
}
}
}
impl #path::TryFromVal<#path::Env, #path::Val> for #enum_ident {
type Error = #path::ConversionError;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: &#path::Val) -> Result<Self, #path::ConversionError> {
use #path::TryIntoVal;
let error: #path::Error = val.try_into_val(env)?;
error.try_into().map_err(|_| #path::ConversionError)
}
}
impl #path::TryFromVal<#path::Env, #enum_ident> for #path::Val {
type Error = #path::ConversionError;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result<Self, #path::ConversionError> {
let error: #path::Error = val.into();
Ok(error.into())
}
}
}
}