Skip to content

Commit

Permalink
chore(deps) Update Rust crate syn to v2 (dev) (#7634)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
renovate[bot] and lucasfernog authored Nov 13, 2023
1 parent e9aa727 commit b6ed4ec
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changes/syn-2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-macros": patch:deps
---

Update to syn v2.
2 changes: 1 addition & 1 deletion core/tauri-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = [ "full" ] }
syn = { version = "2", features = [ "full" ] }
heck = "0.4"
tauri-codegen = { version = "2.0.0-alpha.10", default-features = false, path = "../tauri-codegen" }
tauri-utils = { version = "2.0.0-alpha.10", path = "../tauri-utils" }
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-macros/src/command/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Handler {

impl Parse for Handler {
fn parse(input: &ParseBuffer<'_>) -> syn::Result<Self> {
let command_defs = input.parse_terminated::<CommandDef, Token![,]>(CommandDef::parse)?;
let command_defs = input.parse_terminated(CommandDef::parse, Token![,])?;

// parse the command names and wrappers from the passed paths
let (commands, wrappers) = command_defs
Expand Down
96 changes: 57 additions & 39 deletions core/tauri-macros/src/command/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@ use syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
spanned::Spanned,
FnArg, ItemFn, Lit, Meta, Pat, Token, Visibility,
Expr, ExprLit, FnArg, ItemFn, Lit, Meta, Pat, Token, Visibility,
};

enum WrapperAttributeKind {
Meta(Meta),
Async,
}

impl Parse for WrapperAttributeKind {
fn parse(input: ParseStream) -> syn::Result<Self> {
match input.parse::<Meta>() {
Ok(m) => Ok(Self::Meta(m)),
Err(e) => match input.parse::<Token![async]>() {
Ok(_) => Ok(Self::Async),
Err(_) => Err(e),
},
}
}
}

struct WrapperAttributes {
root: TokenStream2,
execution_context: ExecutionContext,
Expand All @@ -28,12 +46,19 @@ impl Parse for WrapperAttributes {
argument_case: ArgumentCase::Camel,
};

loop {
match input.parse::<Meta>() {
Ok(Meta::List(_)) => {}
Ok(Meta::NameValue(v)) => {
let attrs = Punctuated::<WrapperAttributeKind, Token![,]>::parse_terminated(input)?;
for attr in attrs {
match attr {
WrapperAttributeKind::Meta(Meta::List(_)) => {
return Err(syn::Error::new(input.span(), "unexpected list input"));
}
WrapperAttributeKind::Meta(Meta::NameValue(v)) => {
if v.path.is_ident("rename_all") {
if let Lit::Str(s) = v.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s),
attrs: _,
}) = v.value
{
wrapper_attributes.argument_case = match s.value().as_str() {
"snake_case" => ArgumentCase::Snake,
"camelCase" => ArgumentCase::Camel,
Expand All @@ -46,7 +71,11 @@ impl Parse for WrapperAttributes {
};
}
} else if v.path.is_ident("root") {
if let Lit::Str(s) = v.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s),
attrs: _,
}) = v.value
{
let lit = s.value();

wrapper_attributes.root = if lit == "crate" {
Expand All @@ -58,22 +87,16 @@ impl Parse for WrapperAttributes {
}
}
}
Ok(Meta::Path(p)) => {
if p.is_ident("async") {
wrapper_attributes.execution_context = ExecutionContext::Async;
} else {
return Err(syn::Error::new(p.span(), "expected `async`"));
}
WrapperAttributeKind::Meta(Meta::Path(_)) => {
return Err(syn::Error::new(
input.span(),
"unexpected input, expected one of `rename_all`, `root`, `async`",
));
}
Err(_e) => {
break;
WrapperAttributeKind::Async => {
wrapper_attributes.execution_context = ExecutionContext::Async;
}
}

let lookahead = input.lookahead1();
if lookahead.peek(Token![,]) {
input.parse::<Token![,]>()?;
}
}

Ok(wrapper_attributes)
Expand Down Expand Up @@ -101,10 +124,15 @@ struct Invoke {

/// Create a new [`Wrapper`] from the function and the generated code parsed from the function.
pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream {
let mut attrs = parse_macro_input!(attributes as WrapperAttributes);
let function = parse_macro_input!(item as ItemFn);
let wrapper = super::format_command_wrapper(&function.sig.ident);
let visibility = &function.vis;

if function.sig.asyncness.is_some() {
attrs.execution_context = ExecutionContext::Async;
}

// macros used with `pub use my_macro;` need to be exported with `#[macro_export]`
let maybe_macro_export = match &function.vis {
Visibility::Public(_) => quote!(#[macro_export]),
Expand Down Expand Up @@ -173,28 +201,18 @@ pub fn wrapper(attributes: TokenStream, item: TokenStream) -> TokenStream {
}
}

// body to the command wrapper or a `compile_error!` of an error occurred while parsing it.
let (body, attributes) = syn::parse::<WrapperAttributes>(attributes)
.map(|mut attrs| {
if function.sig.asyncness.is_some() {
attrs.execution_context = ExecutionContext::Async;
}
attrs
})
.and_then(|attrs| {
let body = match attrs.execution_context {
ExecutionContext::Async => body_async(&function, &invoke, &attrs),
ExecutionContext::Blocking => body_blocking(&function, &invoke, &attrs),
};
body.map(|b| (b, Some(attrs)))
})
.unwrap_or_else(|e| (syn::Error::into_compile_error(e), None));
let body = match attrs.execution_context {
ExecutionContext::Async => {
body_async(&function, &invoke, &attrs).unwrap_or_else(syn::Error::into_compile_error)
}
ExecutionContext::Blocking => {
body_blocking(&function, &invoke, &attrs).unwrap_or_else(syn::Error::into_compile_error)
}
};

let Invoke { message, resolver } = invoke;

let root = attributes
.map(|a| a.root)
.unwrap_or_else(|| quote!(::tauri));
let root = attrs.root;

// Rely on rust 2018 edition to allow importing a macro from a path.
quote!(
Expand Down
2 changes: 1 addition & 1 deletion examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b6ed4ec

Please sign in to comment.