|
1 | 1 | use proc_macro::TokenStream;
|
2 | 2 | use quote::quote;
|
3 |
| -use syn::DeriveInput; |
| 3 | +use syn::{Data, DeriveInput}; |
| 4 | + |
| 5 | +use crate::utils::get_repr; |
4 | 6 |
|
5 | 7 | pub(crate) fn impl_encode(ast: &DeriveInput) -> TokenStream {
|
6 |
| - let name = &ast.ident; |
7 |
| - let gen = quote! { |
8 |
| - unsafe impl ::objc2_encode::Encode for #name { |
9 |
| - const ENCODING: ::objc2_encode::Encoding<'static> = ::objc2_encode::Encoding::Struct( |
10 |
| - stringify!(#name), |
11 |
| - &[CGFloat::ENCODING, CGFloat::ENCODING], |
12 |
| - ); |
| 8 | + let DeriveInput { |
| 9 | + ident, data, attrs, .. |
| 10 | + } = ast; |
| 11 | + |
| 12 | + let repr = match get_repr(&attrs) { |
| 13 | + Some(repr) => repr, |
| 14 | + None => panic!("Missing repr"), |
| 15 | + }; |
| 16 | + |
| 17 | + let encoding = match data { |
| 18 | + Data::Struct(data) => { |
| 19 | + let fields = data |
| 20 | + .fields |
| 21 | + .iter() |
| 22 | + .map(|field| &field.ty) |
| 23 | + .collect::<Vec<_>>(); |
| 24 | + match &*repr.to_string() { |
| 25 | + "transparent" => { |
| 26 | + let field = fields[0]; |
| 27 | + assert_eq!(fields.len(), 1, "Expected one item"); |
| 28 | + quote!(<#field as ::objc2_encode::Encode>::ENCODING) |
| 29 | + } |
| 30 | + "C" => { |
| 31 | + quote!( |
| 32 | + ::objc2_encode::Encoding::Struct( |
| 33 | + stringify!(#ident), |
| 34 | + &[#(<#fields as ::objc2_encode::Encode>::ENCODING),*], |
| 35 | + ) |
| 36 | + ) |
| 37 | + } |
| 38 | + _ => panic!("Unknown repr"), |
| 39 | + } |
13 | 40 | }
|
| 41 | + Data::Enum(_) => { |
| 42 | + let ty = match &*repr.to_string() { |
| 43 | + "usize" => quote! { core::primitive::usize }, |
| 44 | + "isize" => quote! { core::primitive::isize }, |
| 45 | + "u64" => quote! { core::primitive::u64 }, |
| 46 | + "i64" => quote! { core::primitive::i64 }, |
| 47 | + "u32" => quote! { core::primitive::u32 }, |
| 48 | + "i32" => quote! { core::primitive::i32 }, |
| 49 | + "u16" => quote! { core::primitive::u16 }, |
| 50 | + "i16" => quote! { core::primitive::i16 }, |
| 51 | + "u8" => quote! { core::primitive::u8 }, |
| 52 | + "i8" => quote! { core::primitive::i8 }, |
| 53 | + _ => panic!("Unknown repr"), |
| 54 | + }; |
| 55 | + quote! { <#ty as ::objc2_encode::Encode>::ENCODING } |
| 56 | + } |
| 57 | + Data::Union(_) => unimplemented!(), |
14 | 58 | };
|
15 |
| - gen.into() |
| 59 | + |
| 60 | + // TODO: Generics |
| 61 | + quote! { |
| 62 | + unsafe impl ::objc2_encode::Encode for #ident { |
| 63 | + const ENCODING: ::objc2_encode::Encoding<'static> = #encoding; |
| 64 | + } |
| 65 | + } |
| 66 | + .into() |
16 | 67 | }
|
17 | 68 |
|
18 | 69 | pub(crate) fn impl_ref_encode(ast: &DeriveInput) -> TokenStream {
|
19 |
| - let name = &ast.ident; |
| 70 | + let DeriveInput { ident, .. } = ast; |
| 71 | + // TODO: Generics |
| 72 | + // TODO: Objects |
| 73 | + |
20 | 74 | let gen = quote! {
|
21 |
| - unsafe impl ::objc2_encode::RefEncode for #name { |
| 75 | + unsafe impl ::objc2_encode::RefEncode for #ident { |
22 | 76 | const ENCODING_REF: ::objc2_encode::Encoding<'static> = ::objc2_encode::Encoding::Pointer(
|
23 | 77 | &<Self as ::objc2_encode::Encode>::ENCODING
|
24 | 78 | );
|
|
0 commit comments