|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::TokenStream as TokenStream2; |
| 3 | +use quote::quote; |
| 4 | +use syn::Fields; |
| 5 | + |
| 6 | +struct NewtypeStruct { |
| 7 | + struct_name: syn::Ident, |
| 8 | + field_type: syn::Type, |
| 9 | + generics: syn::Generics, |
| 10 | +} |
| 11 | +fn get_newtype_struct(st: &syn::ItemStruct) -> Result<NewtypeStruct, TokenStream2> { |
| 12 | + let struct_name = &st.ident; |
| 13 | + let struct_fields = match &st.fields { |
| 14 | + Fields::Unnamed(named_fields) => named_fields, |
| 15 | + _ => { |
| 16 | + return Err(syn::Error::new_spanned( |
| 17 | + st, |
| 18 | + "#[scylla_newtype] error: struct has named fields.", |
| 19 | + ) |
| 20 | + .to_compile_error()); |
| 21 | + } |
| 22 | + }; |
| 23 | + if struct_fields.unnamed.len() > 1 { |
| 24 | + return Err(syn::Error::new_spanned( |
| 25 | + st, |
| 26 | + "#[scylla_newtype] error: struct has more than 1 field.", |
| 27 | + ) |
| 28 | + .to_compile_error()); |
| 29 | + } |
| 30 | + let field_type = match struct_fields.unnamed.first() { |
| 31 | + Some(field) => &field.ty, |
| 32 | + None => { |
| 33 | + return Err(syn::Error::new_spanned( |
| 34 | + st, |
| 35 | + "#[scylla_newtype] error: struct has no fields.", |
| 36 | + ) |
| 37 | + .to_compile_error()); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + Ok(NewtypeStruct { |
| 42 | + struct_name: struct_name.clone(), |
| 43 | + field_type: field_type.clone(), |
| 44 | + generics: st.generics.clone(), |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +fn impl_wasm_convertible(nst: &NewtypeStruct) -> TokenStream2 { |
| 49 | + let struct_name = &nst.struct_name; |
| 50 | + let struct_type = &nst.field_type; |
| 51 | + let (impl_generics, ty_generics, where_clause) = nst.generics.split_for_impl(); |
| 52 | + quote! { |
| 53 | + impl #impl_generics ::scylla_bindgen::_macro_internal::WasmConvertible for #struct_name #ty_generics #where_clause { |
| 54 | + type WasmType = <#struct_type as ::scylla_bindgen::_macro_internal::WasmConvertible>::WasmType; |
| 55 | + unsafe fn from_wasmtype(arg: Self::WasmType) -> Self { |
| 56 | + #struct_name(<#struct_type as ::scylla_bindgen::_macro_internal::WasmConvertible>::from_wasmtype(arg)) |
| 57 | + } |
| 58 | + fn as_wasmtype(&self) -> Self::WasmType { |
| 59 | + <#struct_type as ::scylla_bindgen::_macro_internal::WasmConvertible>::as_wasmtype(&self.0) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn impl_to_col_type(nst: &NewtypeStruct) -> TokenStream2 { |
| 66 | + let struct_name = &nst.struct_name; |
| 67 | + let struct_type = &nst.field_type; |
| 68 | + let (impl_generics, ty_generics, where_clause) = nst.generics.split_for_impl(); |
| 69 | + quote! { |
| 70 | + impl #impl_generics ::scylla_bindgen::_macro_internal::ToColumnType for #struct_name #ty_generics #where_clause { |
| 71 | + fn to_column_type() -> ::scylla_bindgen::_macro_internal::ColumnType { |
| 72 | + #struct_type::to_column_type() |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +fn impl_value(nst: &NewtypeStruct) -> TokenStream2 { |
| 78 | + let struct_name = &nst.struct_name; |
| 79 | + let struct_type = &nst.field_type; |
| 80 | + let (impl_generics, ty_generics, where_clause) = nst.generics.split_for_impl(); |
| 81 | + |
| 82 | + quote! { |
| 83 | + impl #impl_generics ::scylla_bindgen::_macro_internal::Value for #struct_name #ty_generics #where_clause { |
| 84 | + fn serialize(&self, buf: &mut ::std::vec::Vec<::core::primitive::u8>) -> ::std::result::Result<(), ::scylla_bindgen::_macro_internal::ValueTooBig> { |
| 85 | + <#struct_type as ::scylla_bindgen::_macro_internal::Value>::serialize(&self.0, buf) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn impl_from_cql_val(nst: &NewtypeStruct) -> TokenStream2 { |
| 92 | + let struct_name = &nst.struct_name; |
| 93 | + let struct_type = &nst.field_type; |
| 94 | + let (impl_generics, ty_generics, where_clause) = nst.generics.split_for_impl(); |
| 95 | + |
| 96 | + quote! { |
| 97 | + impl #impl_generics ::scylla_bindgen::_macro_internal::FromCqlVal<::scylla_bindgen::_macro_internal::CqlValue> for #struct_name #ty_generics #where_clause { |
| 98 | + fn from_cql(val: ::scylla_bindgen::_macro_internal::CqlValue) -> ::std::result::Result<Self, ::scylla_bindgen::_macro_internal::FromCqlValError> { |
| 99 | + <#struct_type as ::scylla_bindgen::_macro_internal::FromCqlVal<::scylla_bindgen::_macro_internal::CqlValue>>::from_cql(val).map(|v| #struct_name(v)) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +pub(crate) fn scylla_newtype(_attrs: TokenStream, item: TokenStream) -> TokenStream { |
| 106 | + let mut st = syn::parse_macro_input!(item as syn::ItemStruct); |
| 107 | + // We have to make the struct public, otherwise we can't use "<$struct_name as WasmConvertible>::WasmType" |
| 108 | + // in the exported function signature even if WasmType is public. |
| 109 | + st.vis = syn::Visibility::Public(syn::VisPublic { |
| 110 | + pub_token: syn::token::Pub(proc_macro2::Span::call_site()), |
| 111 | + }); |
| 112 | + let newtype_struct = match get_newtype_struct(&st) { |
| 113 | + Ok(nst) => nst, |
| 114 | + Err(e) => return e.into(), |
| 115 | + }; |
| 116 | + let wasm_convertible = impl_wasm_convertible(&newtype_struct); |
| 117 | + let to_col_type = impl_to_col_type(&newtype_struct); |
| 118 | + let value = impl_value(&newtype_struct); |
| 119 | + let from_cql_val = impl_from_cql_val(&newtype_struct); |
| 120 | + quote! { |
| 121 | + #st |
| 122 | + #wasm_convertible |
| 123 | + #to_col_type |
| 124 | + #value |
| 125 | + #from_cql_val |
| 126 | + } |
| 127 | + .into() |
| 128 | +} |
0 commit comments