| 1 |
use proc_macro2::{Ident, Span, TokenStream}; |
| 2 |
use quote::quote; |
| 3 |
use syn::{parse::ParseStream, Data, DataStruct, DeriveInput, Field, Token}; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
default::{default_value_metadata_calls, DefaultValue}, |
| 7 |
ffiops, |
| 8 |
util::{ |
| 9 |
create_metadata_items, either_attribute_arg, extract_docstring, ident_to_string, kw, |
| 10 |
mod_path, try_metadata_value_from_usize, try_read_field, AttributeSliceExt, |
| 11 |
UniffiAttributeArgs, |
| 12 |
}, |
| 13 |
DeriveOptions, |
| 14 |
}; |
| 15 |
|
| 16 |
/// Stores parsed data from the Derive Input for the struct. |
| 17 |
struct RecordItem { |
| 18 |
ident: Ident, |
| 19 |
record: DataStruct, |
| 20 |
docstring: String, |
| 21 |
} |
| 22 |
|
| 23 |
impl RecordItem { |
| 24 |
fn new(input: DeriveInput) -> syn::Result<Self> { |
| 25 |
let record = match input.data { |
| 26 |
Data::Struct(s) => s, |
| 27 |
_ => { |
| 28 |
return Err(syn::Error::new( |
| 29 |
Span::call_site(), |
| 30 |
"This derive must only be used on structs", |
| 31 |
)); |
| 32 |
} |
| 33 |
}; |
| 34 |
Ok(Self { |
| 35 |
ident: input.ident, |
| 36 |
record, |
| 37 |
docstring: extract_docstring(&input.attrs)?, |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
fn ident(&self) -> &Ident { |
| 42 |
&self.ident |
| 43 |
} |
| 44 |
|
| 45 |
fn name(&self) -> String { |
| 46 |
ident_to_string(&self.ident) |
| 47 |
} |
| 48 |
|
| 49 |
fn struct_(&self) -> &DataStruct { |
| 50 |
&self.record |
| 51 |
} |
| 52 |
|
| 53 |
fn docstring(&self) -> &str { |
| 54 |
self.docstring.as_str() |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pub fn expand_record(input: DeriveInput, options: DeriveOptions) -> syn::Result<TokenStream> { |
| 59 |
if let Some(e) = input.attrs.uniffi_attr_args_not_allowed_here() { |
| 60 |
return Err(e); |
| 61 |
} |
| 62 |
let record = RecordItem::new(input)?; |
| 63 |
let ffi_converter = |
| 64 |
record_ffi_converter_impl(&record, &options).unwrap_or_else(syn::Error::into_compile_error); |
| 65 |
let meta_static_var = options |
| 66 |
.generate_metadata |
| 67 |
.then(|| record_meta_static_var(&record).unwrap_or_else(syn::Error::into_compile_error)); |
| 68 |
|
| 69 |
Ok(quote! { |
| 70 |
#ffi_converter |
| 71 |
#meta_static_var |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
fn record_ffi_converter_impl( |
| 76 |
record: &RecordItem, |
| 77 |
options: &DeriveOptions, |
| 78 |
) -> syn::Result<TokenStream> { |
| 79 |
let ident = record.ident(); |
| 80 |
let impl_spec = options.ffi_impl_header("FfiConverter", ident); |
| 81 |
let derive_ffi_traits = options.derive_all_ffi_traits(ident); |
| 82 |
let name = ident_to_string(ident); |
| 83 |
let mod_path = mod_path()?; |
| 84 |
let write_impl: TokenStream = record.struct_().fields.iter().map(write_field).collect(); |
| 85 |
let try_read_fields: TokenStream = record.struct_().fields.iter().map(try_read_field).collect(); |
| 86 |
|
| 87 |
Ok(quote! { |
| 88 |
#[automatically_derived] |
| 89 |
unsafe #impl_spec { |
| 90 |
::uniffi::ffi_converter_rust_buffer_lift_and_lower!(crate::UniFfiTag); |
| 91 |
|
| 92 |
fn write(obj: Self, buf: &mut ::std::vec::Vec<u8>) { |
| 93 |
#write_impl |
| 94 |
} |
| 95 |
|
| 96 |
fn try_read(buf: &mut &[::std::primitive::u8]) -> ::uniffi::deps::anyhow::Result<Self> { |
| 97 |
::std::result::Result::Ok(Self { #try_read_fields }) |
| 98 |
} |
| 99 |
|
| 100 |
const TYPE_ID_META: ::uniffi::MetadataBuffer = ::uniffi::MetadataBuffer::from_code(::uniffi::metadata::codes::TYPE_RECORD) |
| 101 |
.concat_str(#mod_path) |
| 102 |
.concat_str(#name); |
| 103 |
} |
| 104 |
|
| 105 |
#derive_ffi_traits |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
fn write_field(f: &Field) -> TokenStream { |
| 110 |
let ident = &f.ident; |
| 111 |
let write = ffiops::write(&f.ty); |
| 112 |
quote! { |
| 113 |
#write(obj.#ident, buf); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[derive(Default)] |
| 118 |
pub struct FieldAttributeArguments { |
| 119 |
pub(crate) default: Option<DefaultValue>, |
| 120 |
} |
| 121 |
|
| 122 |
impl UniffiAttributeArgs for FieldAttributeArguments { |
| 123 |
fn parse_one(input: ParseStream<'_>) -> syn::Result<Self> { |
| 124 |
let _: kw::default = input.parse()?; |
| 125 |
let _: Token![=] = input.parse()?; |
| 126 |
let default = input.parse()?; |
| 127 |
Ok(Self { |
| 128 |
default: Some(default), |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
fn merge(self, other: Self) -> syn::Result<Self> { |
| 133 |
Ok(Self { |
| 134 |
default: either_attribute_arg(self.default, other.default)?, |
| 135 |
}) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
fn record_meta_static_var(record: &RecordItem) -> syn::Result<TokenStream> { |
| 140 |
let name = record.name(); |
| 141 |
let docstring = record.docstring(); |
| 142 |
let module_path = mod_path()?; |
| 143 |
let fields_len = try_metadata_value_from_usize( |
| 144 |
record.struct_().fields.len(), |
| 145 |
"UniFFI limits structs to 256 fields", |
| 146 |
)?; |
| 147 |
|
| 148 |
let concat_fields: TokenStream = record |
| 149 |
.struct_() |
| 150 |
.fields |
| 151 |
.iter() |
| 152 |
.map(|f| { |
| 153 |
let attrs = f |
| 154 |
.attrs |
| 155 |
.parse_uniffi_attr_args::<FieldAttributeArguments>()?; |
| 156 |
|
| 157 |
let name = ident_to_string(f.ident.as_ref().unwrap()); |
| 158 |
let docstring = extract_docstring(&f.attrs)?; |
| 159 |
let default = default_value_metadata_calls(&attrs.default)?; |
| 160 |
let type_id_meta = ffiops::type_id_meta(&f.ty); |
| 161 |
|
| 162 |
// Note: fields need to implement both `Lower` and `Lift` to be used in a record. The |
| 163 |
// TYPE_ID_META should be the same for both traits. |
| 164 |
Ok(quote! { |
| 165 |
.concat_str(#name) |
| 166 |
.concat(#type_id_meta) |
| 167 |
#default |
| 168 |
.concat_long_str(#docstring) |
| 169 |
}) |
| 170 |
}) |
| 171 |
.collect::<syn::Result<_>>()?; |
| 172 |
|
| 173 |
Ok(create_metadata_items( |
| 174 |
"record", |
| 175 |
&name, |
| 176 |
quote! { |
| 177 |
::uniffi::MetadataBuffer::from_code(::uniffi::metadata::codes::RECORD) |
| 178 |
.concat_str(#module_path) |
| 179 |
.concat_str(#name) |
| 180 |
.concat_value(#fields_len) |
| 181 |
#concat_fields |
| 182 |
.concat_long_str(#docstring) |
| 183 |
}, |
| 184 |
None, |
| 185 |
)) |
| 186 |
} |