|
1 | 1 | use itertools::MultiUnzip; |
2 | 2 | use proc_macro2::{Literal, TokenStream as TokenStream2}; |
3 | 3 | use quote::{format_ident, quote, ToTokens}; |
4 | | -use syn::{spanned::Spanned, Attribute, DataEnum, Error, Fields, Ident, Path, Type, Visibility}; |
| 4 | +use syn::{spanned::Spanned, Attribute, DataEnum, Error, Fields, Ident, Path, Visibility}; |
5 | 5 |
|
6 | 6 | use stellar_xdr::curr as stellar_xdr; |
7 | 7 | use stellar_xdr::{ |
@@ -156,18 +156,48 @@ pub fn derive_type_enum( |
156 | 156 | None |
157 | 157 | }; |
158 | 158 |
|
159 | | - // Flatten all variant field types for shaking calls, deduplicating |
160 | | - // to avoid redundant calls for types that appear in multiple variants. |
161 | | - let all_field_types: Vec<&Type> = |
162 | | - itertools::Itertools::unique_by(variant_field_types.iter().flatten(), |t| { |
163 | | - t.to_token_stream().to_string() |
| 159 | + // Generated code spec. |
| 160 | + let spec_gen = if let Some(ref spec_xdr) = spec_xdr { |
| 161 | + let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice()); |
| 162 | + let spec_xdr_len = spec_xdr.len(); |
| 163 | + let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", enum_ident.to_string().to_uppercase()); |
| 164 | + Some(quote! { |
| 165 | + #[cfg_attr(target_family = "wasm", link_section = "contractspecv0")] |
| 166 | + pub static #spec_ident: [u8; #spec_xdr_len] = #enum_ident::spec_xdr(); |
| 167 | + |
| 168 | + impl #enum_ident { |
| 169 | + pub const fn spec_xdr() -> [u8; #spec_xdr_len] { |
| 170 | + *#spec_xdr_lit |
| 171 | + } |
| 172 | + } |
164 | 173 | }) |
165 | | - .copied() |
166 | | - .collect(); |
| 174 | + } else { |
| 175 | + None |
| 176 | + }; |
167 | 177 |
|
168 | | - // Generated code spec and SpecShakingMarker impl. |
169 | | - let (spec_gen, spec_shaking_impl) = |
170 | | - shaking::generate_type_spec_and_marker(path, &enum_ident, &spec_xdr, &all_field_types); |
| 178 | + // SpecShakingMarker impl - only generated when spec is true and the |
| 179 | + // experimental_spec_shaking_v2 feature is enabled. |
| 180 | + let spec_shaking_impl = if cfg!(feature = "experimental_spec_shaking_v2") { |
| 181 | + spec_xdr.as_ref().map(|spec_xdr| { |
| 182 | + // Flatten all variant field types for shaking calls, deduplicating |
| 183 | + // to avoid redundant calls for types that appear in multiple variants. |
| 184 | + let all_field_types = |
| 185 | + itertools::Itertools::unique_by(variant_field_types.iter().flatten(), |t| { |
| 186 | + t.to_token_stream().to_string() |
| 187 | + }); |
| 188 | + shaking::generate_marker_impl( |
| 189 | + path, |
| 190 | + quote!(#enum_ident), |
| 191 | + spec_xdr, |
| 192 | + all_field_types.cloned(), |
| 193 | + None, |
| 194 | + None, |
| 195 | + None, |
| 196 | + ) |
| 197 | + }) |
| 198 | + } else { |
| 199 | + None |
| 200 | + }; |
171 | 201 |
|
172 | 202 | // Output. |
173 | 203 | let mut output = quote! { |
|
0 commit comments