From 2284bb38ed17a7782afe9f1b7235db68909322fa Mon Sep 17 00:00:00 2001 From: Yiu Tin Cheung Ivan Date: Tue, 11 Jul 2023 10:54:40 +0800 Subject: [PATCH] added feature flag for sea-orm iden dependency --- Cargo.toml | 1 + sea-query-derive/src/lib.rs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5666a2dbe..5faf3953d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ with-time = ["time"] with-ipnetwork = ["ipnetwork"] with-mac_address = ["mac_address"] tests-cfg = [] +sea-orm = [] [[test]] name = "test-derive" diff --git a/sea-query-derive/src/lib.rs b/sea-query-derive/src/lib.rs index d5cc8cc2c..41b731620 100644 --- a/sea-query-derive/src/lib.rs +++ b/sea-query-derive/src/lib.rs @@ -46,9 +46,15 @@ fn impl_iden_for_unit_struct( ident: &proc_macro2::Ident, table_name: &str, ) -> proc_macro2::TokenStream { + let flagged_path = if cfg!(feature = "sea-orm") { + quote!(sea_orm::sea_query) + } else { + quote!(sea_query) + }; + let prepare = if must_be_valid_iden(table_name) { quote! { - fn prepare(&self, s: &mut dyn ::std::fmt::Write, q: sea_query::Quote) { + fn prepare(&self, s: &mut dyn ::std::fmt::Write, q: #flagged_path::Quote) { write!(s, "{}", q.left()).unwrap(); self.unquoted(s); write!(s, "{}", q.right()).unwrap(); @@ -57,8 +63,9 @@ fn impl_iden_for_unit_struct( } else { quote! {} }; + quote! { - impl sea_query::Iden for #ident { + impl #flagged_path::Iden for #ident { #prepare fn unquoted(&self, s: &mut dyn ::std::fmt::Write) { @@ -91,9 +98,14 @@ where Err(e) => return e.to_compile_error(), }; + let flagged_path = if cfg!(feature = "sea-orm") { + quote!(sea_orm::sea_query) + } else { + quote!(sea_query) + }; let prepare = if is_all_valid { quote! { - fn prepare(&self, s: &mut dyn ::std::fmt::Write, q: sea_query::Quote) { + fn prepare(&self, s: &mut dyn ::std::fmt::Write, q: #flagged_path::Quote) { write!(s, "{}", q.left()).unwrap(); self.unquoted(s); write!(s, "{}", q.right()).unwrap(); @@ -104,7 +116,7 @@ where }; quote! { - impl sea_query::Iden for #ident { + impl #flagged_path::Iden for #ident { #prepare fn unquoted(&self, s: &mut dyn ::std::fmt::Write) {