summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_macros/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--third_party/rust/uniffi_macros/src/util.rs47
1 files changed, 35 insertions, 12 deletions
diff --git a/third_party/rust/uniffi_macros/src/util.rs b/third_party/rust/uniffi_macros/src/util.rs
index 9f213ea1d7..97faad9c4d 100644
--- a/third_party/rust/uniffi_macros/src/util.rs
+++ b/third_party/rust/uniffi_macros/src/util.rs
@@ -8,7 +8,7 @@ use std::path::{Path as StdPath, PathBuf};
use syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
- Attribute, Token,
+ Attribute, Expr, Lit, Token,
};
pub fn manifest_path() -> Result<PathBuf, String> {
@@ -79,8 +79,13 @@ pub fn try_read_field(f: &syn::Field) -> TokenStream {
let ident = &f.ident;
let ty = &f.ty;
- quote! {
- #ident: <#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_read(buf)?,
+ match ident {
+ Some(ident) => quote! {
+ #ident: <#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_read(buf)?,
+ },
+ None => quote! {
+ <#ty as ::uniffi::Lift<crate::UniFfiTag>>::try_read(buf)?,
+ },
}
}
@@ -151,13 +156,7 @@ pub fn parse_comma_separated<T: UniffiAttributeArgs>(input: ParseStream<'_>) ->
}
#[derive(Default)]
-pub struct ArgumentNotAllowedHere;
-
-impl Parse for ArgumentNotAllowedHere {
- fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
- parse_comma_separated(input)
- }
-}
+struct ArgumentNotAllowedHere;
impl UniffiAttributeArgs for ArgumentNotAllowedHere {
fn parse_one(input: ParseStream<'_>) -> syn::Result<Self> {
@@ -224,7 +223,11 @@ pub(crate) fn derive_all_ffi_traits(ty: &Ident, udl_mode: bool) -> TokenStream {
}
}
-pub(crate) fn derive_ffi_traits(ty: &Ident, udl_mode: bool, trait_names: &[&str]) -> TokenStream {
+pub(crate) fn derive_ffi_traits(
+ ty: impl ToTokens,
+ udl_mode: bool,
+ trait_names: &[&str],
+) -> TokenStream {
let trait_idents = trait_names
.iter()
.map(|name| Ident::new(name, Span::call_site()));
@@ -247,11 +250,14 @@ pub(crate) fn derive_ffi_traits(ty: &Ident, udl_mode: bool, trait_names: &[&str]
pub mod kw {
syn::custom_keyword!(async_runtime);
syn::custom_keyword!(callback_interface);
- syn::custom_keyword!(constructor);
+ syn::custom_keyword!(with_foreign);
syn::custom_keyword!(default);
syn::custom_keyword!(flat_error);
syn::custom_keyword!(None);
+ syn::custom_keyword!(Some);
syn::custom_keyword!(with_try_read);
+ syn::custom_keyword!(name);
+ syn::custom_keyword!(non_exhaustive);
syn::custom_keyword!(Debug);
syn::custom_keyword!(Display);
syn::custom_keyword!(Eq);
@@ -276,3 +282,20 @@ impl Parse for ExternalTypeItem {
})
}
}
+
+pub(crate) fn extract_docstring(attrs: &[Attribute]) -> syn::Result<String> {
+ return attrs
+ .iter()
+ .filter(|attr| attr.path().is_ident("doc"))
+ .map(|attr| {
+ let name_value = attr.meta.require_name_value()?;
+ if let Expr::Lit(expr) = &name_value.value {
+ if let Lit::Str(lit_str) = &expr.lit {
+ return Ok(lit_str.value().trim().to_owned());
+ }
+ }
+ Err(syn::Error::new_spanned(attr, "Cannot parse doc attribute"))
+ })
+ .collect::<syn::Result<Vec<_>>>()
+ .map(|lines| lines.join("\n"));
+}