Skip to content

Commit

Permalink
Upgrade syn to 2 (#129) (#135)
Browse files Browse the repository at this point in the history
* Upgrade `syn` to `2` (#129)

* clippy

---------

Co-authored-by: Zoey <[email protected]>
  • Loading branch information
tyt2y3 and Dessix authored Jun 23, 2024
1 parent f03ee2c commit 1fe3424
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sea-schema-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = [ "database", "sql", "mysql", "postgres", "sqlite" ]
proc-macro = true

[dependencies]
syn = { version = "1", default-features = false, features = [ "derive", "parsing", "proc-macro", "printing" ] }
syn = { version = "2", default-features = false, features = [ "derive", "parsing", "proc-macro", "printing" ] }
quote = { version = "1", default-features = false }
heck = { version = "0.4", default-features = false }
proc-macro2 = { version = "1", default-features = false }
31 changes: 17 additions & 14 deletions sea-schema-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ use proc_macro::{self, TokenStream};
use proc_macro2::Span;
use quote::{quote, quote_spanned};
use syn::{
parse_macro_input, Attribute, DataEnum, DataStruct, DeriveInput, Fields, Ident, Lit, Meta,
Variant,
parse_macro_input, Attribute, DataEnum, DataStruct, DeriveInput, Expr, ExprLit, Fields, Ident,
Lit, Meta, Variant,
};

fn get_iden_attr(attrs: &[Attribute]) -> Option<syn::Lit> {
fn get_iden_attr(attrs: &[Attribute]) -> Option<&syn::Expr> {
for attr in attrs {
let name_value = match attr.parse_meta() {
Ok(Meta::NameValue(nv)) => nv,
let name_value = match &attr.meta {
Meta::NameValue(nv) => nv,
_ => continue,
};
if name_value.path.is_ident("iden") || // interoperate with sea_query_derive Iden
name_value.path.is_ident("name")
{
return Some(name_value.lit);
return Some(&name_value.value);
}
}
None
}

fn get_catch_attr(attrs: &[Attribute]) -> Option<syn::Lit> {
fn get_catch_attr(attrs: &[Attribute]) -> Option<&syn::Expr> {
for attr in attrs {
let name_value = match attr.parse_meta() {
Ok(Meta::NameValue(nv)) => nv,
let name_value = match &attr.meta {
Meta::NameValue(nv) => nv,
_ => continue,
};
if name_value.path.is_ident("catch") {
return Some(name_value.lit);
return Some(&name_value.value);
}
}
None
Expand All @@ -42,7 +42,7 @@ pub fn derive_iden(input: TokenStream) -> TokenStream {
} = parse_macro_input!(input);

let table_name = match get_iden_attr(&attrs) {
Some(lit) => quote! { #lit },
Some(iden) => quote! { #iden },
None => {
let normalized = ident.to_string().to_snake_case();
quote! { #normalized }
Expand All @@ -52,7 +52,10 @@ pub fn derive_iden(input: TokenStream) -> TokenStream {
let catch = match get_catch_attr(&attrs) {
Some(lit) => {
let name: String = match lit {
Lit::Str(name) => name.value(),
Expr::Lit(ExprLit {
lit: Lit::Str(name),
..
}) => name.value(),
_ => panic!("expected string for `catch`"),
};
let method = Ident::new(name.as_str(), Span::call_site());
Expand Down Expand Up @@ -104,9 +107,9 @@ pub fn derive_iden(input: TokenStream) -> TokenStream {
});

let name = variants.iter().map(|v| {
if let Some(lit) = get_iden_attr(&v.attrs) {
if let Some(iden) = get_iden_attr(&v.attrs) {
// If the user supplied a name, just use it
quote! { #lit }
quote! { #iden }
} else if v.ident == "Table" {
table_name.clone()
} else {
Expand Down

0 comments on commit 1fe3424

Please sign in to comment.