Skip to content

Commit

Permalink
- marked #[enum_def] of sea-query-attr as deprecated
Browse files Browse the repository at this point in the history
- added extended version of  `#[enum_def]`  in `sea-query-derive`
- upgraded `darling` crate from 0.14 to 0.20
  • Loading branch information
AndreiOrmanji authored Apr 18, 2024
1 parent f4c4efc commit 7f996cc
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 172 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,9 @@ impl Iden for Character {
```

If you're okay with running another procedural macro, you can activate
the `derive` or `attr` feature on the crate to save you some boilerplate.
the `derive` feature on the crate to save you some boilerplate.
For more usage information, look at
[the derive examples](https://github.com/SeaQL/sea-query/tree/master/sea-query-derive/tests/pass)
or [the attribute examples](https://github.com/SeaQL/sea-query/tree/master/sea-query-attr/tests/pass).
[the derive examples](https://github.com/SeaQL/sea-query/tree/master/sea-query-derive/tests/pass).

```rust
#[cfg(feature = "derive")]
Expand All @@ -198,7 +197,7 @@ assert_eq!(Glyph.to_string(), "glyph");
```

```rust
#[cfg(feature = "attr")]
#[cfg(feature = "derive")]
use sea_query::{enum_def, Iden};

#[enum_def]
Expand Down
22 changes: 5 additions & 17 deletions sea-query-attr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Default for GenEnumArgs {
}
}

#[deprecated(since = "0.1.2", note = "use #[enum_def] attr defined in `sea-query-derive` crate")]
#[proc_macro_attribute]
pub fn enum_def(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as AttributeArgs);
Expand Down Expand Up @@ -84,7 +85,6 @@ pub fn enum_def(args: TokenStream, input: TokenStream) -> TokenStream {
let pascal_def_names = field_names.iter().map(|field| &field.pascal);
let pascal_def_names2 = pascal_def_names.clone();
let default_names = field_names.iter().map(|field| &field.default);
let default_names2 = default_names.clone();
let import_name = Ident::new(
args.crate_name
.unwrap_or_else(|| DEFAULT_CRATE_NAME.to_string())
Expand All @@ -101,24 +101,12 @@ pub fn enum_def(args: TokenStream, input: TokenStream) -> TokenStream {
#(#pascal_def_names,)*
}

impl #import_name::IdenStatic for #enum_name {
fn as_str(&self) -> &'static str {
match self {
#enum_name::Table => stringify!(#table_name),
#(#enum_name::#pascal_def_names2 => stringify!(#default_names2)),*
}
}
}

impl #import_name::Iden for #enum_name {
fn unquoted(&self, s: &mut dyn sea_query::Write) {
write!(s, "{}", <Self as #import_name::IdenStatic>::as_str(&self)).unwrap();
}
}

impl ::std::convert::AsRef<str> for #enum_name {
fn as_ref(&self) -> &str {
<Self as #import_name::IdenStatic>::as_str(&self)
write!(s, "{}", match self {
#enum_name::Table => stringify!(#table_name),
#(#enum_name::#pascal_def_names2 => stringify!(#default_names)),*
}).unwrap();
}
}
})
Expand Down
10 changes: 1 addition & 9 deletions sea-query-attr/tests/pass/table_iden.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use sea_query::{Iden, IdenStatic};
use sea_query::Iden;
use sea_query_attr::enum_def;
use std::convert::AsRef;

#[enum_def(table_name = "HelloTable")]
pub struct Hello {
Expand All @@ -9,11 +8,4 @@ pub struct Hello {

fn main() {
assert_eq!("HelloTable".to_string(), HelloIden::Table.to_string());
assert_eq!("name".to_string(), HelloIden::Name.to_string());

assert_eq!("HelloTable", HelloIden::Table.as_str());
assert_eq!("name", HelloIden::Name.as_str());

assert_eq!("HelloTable", AsRef::<str>::as_ref(&HelloIden::Table));
assert_eq!("name", AsRef::<str>::as_ref(&HelloIden::Name));
}
1 change: 1 addition & 0 deletions sea-query-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ proc-macro = true
syn = { version = "2", default-features = false, features = ["parsing", "proc-macro", "derive", "printing"] }
quote = { version = "1", default-features = false }
heck = { version = "0.4", default-features = false }
darling = { version = "0.20", default-features = false }
proc-macro2 = { version = "1", default-features = false }
thiserror = { version = "1.0", default-features = false }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto};
use syn::spanned::Spanned;
use syn::{Attribute, Error, Expr, ExprLit, Ident, Lit, LitStr, Meta};

use crate::{error::ErrorMsg, iden_path::IdenPath};
use super::{error::ErrorMsg, path::IdenPath};

#[derive(PartialEq, Eq)]
pub(crate) enum IdenAttr {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use syn::Ident;

use crate::iden_path::IdenPath;
use super::path::IdenPath;

#[derive(Debug, thiserror::Error)]
pub enum ErrorMsg {
Expand Down
34 changes: 34 additions & 0 deletions sea-query-derive/src/iden/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub(crate) mod attr;
pub(crate) mod error;
pub(crate) mod path;
pub(crate) mod write_arm;

use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::Ident;

use self::write_arm::WriteArm;

pub(crate) struct DeriveIden;

impl WriteArm for DeriveIden {
fn variant(variant: TokenStream2, name: TokenStream2) -> TokenStream2 {
quote! { Self::#variant => write!(s, "{}", #name).unwrap() }
}

fn flattened(variant: TokenStream2, name: &Ident) -> TokenStream2 {
quote! { Self::#variant => #name.unquoted(s) }
}
}

pub(crate) struct DeriveIdenStatic;

impl WriteArm for DeriveIdenStatic {
fn variant(variant: TokenStream2, name: TokenStream2) -> TokenStream2 {
quote! { Self::#variant => #name }
}

fn flattened(variant: TokenStream2, name: &Ident) -> TokenStream2 {
quote! { Self::#variant => #name.as_str() }
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,13 @@ use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{Error, Fields, FieldsNamed, Ident, Variant};

use crate::{error::ErrorMsg, find_attr, iden_attr::IdenAttr, must_be_valid_iden};
use super::{attr::IdenAttr, error::ErrorMsg};
use crate::{find_attr, must_be_valid_iden};

pub(crate) trait WriteArm {
fn variant(variant: TokenStream, name: TokenStream) -> TokenStream;
fn flattened(variant: TokenStream, name: &Ident) -> TokenStream;
}

pub(crate) struct DeriveIden;

impl WriteArm for DeriveIden {
fn variant(variant: TokenStream, name: TokenStream) -> TokenStream {
quote! { Self::#variant => write!(s, "{}", #name).unwrap() }
}

fn flattened(variant: TokenStream, name: &Ident) -> TokenStream {
quote! { Self::#variant => #name.unquoted(s) }
}
}

pub(crate) struct DeriveIdenStatic;

impl WriteArm for DeriveIdenStatic {
fn variant(variant: TokenStream, name: TokenStream) -> TokenStream {
quote! { Self::#variant => #name }
}

fn flattened(variant: TokenStream, name: &Ident) -> TokenStream {
quote! { Self::#variant => #name.as_str() }
}
}

pub(crate) struct IdenVariant<'a, T> {
ident: &'a Ident,
fields: &'a Fields,
Expand Down Expand Up @@ -183,7 +159,7 @@ where
T::variant(variant, name)
}

pub(super) fn must_be_valid_iden(&self) -> bool {
pub(crate) fn must_be_valid_iden(&self) -> bool {
let name: String = match &self.attr {
Some(a) => match a {
IdenAttr::Rename(name) => name.to_owned(),
Expand Down
Loading

0 comments on commit 7f996cc

Please sign in to comment.