Debian Codegraph @

third_party/rust/uniffi_macros/src/derive.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 //! General handling for the derive and udl_derive macros
6
7 use crate::util::kw;
8 use proc_macro2::{Ident, Span, TokenStream};
9 use quote::{quote, ToTokens};
10 use syn::{
11 parse::{Parse, ParseStream},
12 DeriveInput,
13 };
14
15 pub fn expand_derive(
16 kind: DeriveKind,
17 input: DeriveInput,
18 options: DeriveOptions,
19 ) -> syn::Result<TokenStream> {
20 match kind {
21 DeriveKind::Record(_) => crate::record::expand_record(input, options),
22 DeriveKind::Object(_) => crate::object::expand_object(input, options),
23 DeriveKind::Enum(_) => crate::enum_::expand_enum(input, options),
24 DeriveKind::Error(_) => crate::error::expand_error(input, options),
25 }
26 }
27
28 pub enum DeriveKind {
29 Record(kw::Record),
30 Enum(kw::Enum),
31 Error(kw::Error),
32 Object(kw::Object),
33 }
34
35 impl Parse for DeriveKind {
36 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
37 let lookahead = input.lookahead1();
38 if lookahead.peek(kw::Record) {
39 Ok(Self::Record(input.parse()?))
40 } else if lookahead.peek(kw::Enum) {
41 Ok(Self::Enum(input.parse()?))
42 } else if lookahead.peek(kw::Error) {
43 Ok(Self::Error(input.parse()?))
44 } else if lookahead.peek(kw::Object) {
45 Ok(Self::Object(input.parse()?))
46 } else {
47 Err(lookahead.error())
48 }
49 }
50 }
51
52 pub struct DeriveOptions {
53 /// Should we implement FFI traits for the local UniFfiTag only?
54 pub local_tag: bool,
55 /// Should we generate metadata symbols?
56 pub generate_metadata: bool,
57 }
58
59 /// default() is used to construct a DeriveOptions for a regular `derive` invocation
60 impl Default for DeriveOptions {
61 fn default() -> Self {
62 Self {
63 local_tag: false,
64 generate_metadata: true,
65 }
66 }
67 }
68
69 impl DeriveOptions {
70 /// Construct DeriveOptions for `udl_derive`
71 pub fn udl_derive() -> Self {
72 Self {
73 local_tag: false,
74 generate_metadata: false,
75 }
76 }
77
78 /// DeriveOptions for `#[remote]`
79 pub fn remote() -> Self {
80 Self {
81 local_tag: true,
82 generate_metadata: true,
83 }
84 }
85
86 /// DeriveOptions for `#[udl_remote]`
87 pub fn udl_remote() -> Self {
88 Self {
89 local_tag: true,
90 generate_metadata: false,
91 }
92 }
93
94 /// Generate the impl header for a FFI trait
95 ///
96 /// This will output something like `impl<UT> FfiConverter<UT> for #type`. The caller is
97 /// responsible for providing the body if the impl block.
98 pub fn ffi_impl_header(&self, trait_name: &str, ident: &impl ToTokens) -> TokenStream {
99 let trait_name = Ident::new(trait_name, Span::call_site());
100 if self.local_tag {
101 quote! { impl ::uniffi::#trait_name<crate::UniFfiTag> for #ident }
102 } else {
103 quote! { impl<UT> ::uniffi::#trait_name<UT> for #ident }
104 }
105 }
106
107 /// Generate a call to `derive_ffi_traits!` that will derive all the FFI traits
108 pub fn derive_all_ffi_traits(&self, ty: &Ident) -> TokenStream {
109 if self.local_tag {
110 quote! { ::uniffi::derive_ffi_traits!(local #ty); }
111 } else {
112 quote! { ::uniffi::derive_ffi_traits!(blanket #ty); }
113 }
114 }
115
116 /// Generate a call to `derive_ffi_traits!` that will derive some of the FFI traits
117 pub fn derive_ffi_traits(&self, ty: impl ToTokens, trait_names: &[&str]) -> TokenStream {
118 let trait_idents = trait_names
119 .iter()
120 .map(|name| Ident::new(name, Span::call_site()));
121 if self.local_tag {
122 quote! {
123 #(
124 ::uniffi::derive_ffi_traits!(impl #trait_idents<crate::UniFfiTag> for #ty);
125 )*
126 }
127 } else {
128 quote! {
129 #(
130 ::uniffi::derive_ffi_traits!(impl<UT> #trait_idents<UT> for #ty);
131 )*
132 }
133 }
134 }
135 }