Skip to content

Commit

Permalink
feat: new derive feature flag (#3113)
Browse files Browse the repository at this point in the history
* feat: new derive feature flag

* doc: add new derive flag to readme

* fix: macros feature flag cfg
  • Loading branch information
saiintbrisson authored Mar 30, 2024
1 parent 7102a7a commit 1c7b3d0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["any", "macros", "migrate", "json"]
macros = ["sqlx-macros"]

derive = ["sqlx-macros/derive"]
macros = ["derive", "sqlx-macros/macros"]
migrate = ["sqlx-core/migrate", "sqlx-macros?/migrate", "sqlx-mysql?/migrate", "sqlx-postgres?/migrate", "sqlx-sqlite?/migrate"]

# intended mainly for CI and docs
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ be removed in the future.

- `any`: Add support for the `Any` database driver, which can proxy to a database driver at runtime.

- `derive`: Add support for the derive family macros, those are `FromRow`, `Type`, `Encode`, `Decode`.

- `macros`: Add support for the `query*!` macros, which allows compile-time checked queries.

- `migrate`: Add support for the migration management and `migrate!` macro, which allow compile-time embedded migrations.
Expand Down
2 changes: 2 additions & 0 deletions sqlx-macros-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ _tls-native-tls = ["sqlx-core/_tls-native-tls"]
_tls-rustls = ["sqlx-core/_tls-rustls"]

# SQLx features
derive = []
macros = []
migrate = ["sqlx-core/migrate"]

# database
Expand Down
5 changes: 5 additions & 0 deletions sqlx-macros-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
feature(track_path)
)]

#[cfg(feature = "macros")]
use crate::query::QueryDriver;

pub type Error = Box<dyn std::error::Error>;
Expand All @@ -28,15 +29,19 @@ pub type Result<T> = std::result::Result<T, Error>;
mod common;
mod database;

#[cfg(feature = "derive")]
pub mod derives;
#[cfg(feature = "macros")]
pub mod query;

#[cfg(feature = "macros")]
// The compiler gives misleading help messages about `#[cfg(test)]` when this is just named `test`.
pub mod test_attr;

#[cfg(feature = "migrate")]
pub mod migrate;

#[cfg(feature = "macros")]
pub const FOSS_DRIVERS: &[QueryDriver] = &[
#[cfg(feature = "mysql")]
QueryDriver::new::<sqlx_mysql::MySql>(),
Expand Down
2 changes: 2 additions & 0 deletions sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ _tls-native-tls = ["sqlx-macros-core/_tls-native-tls"]
_tls-rustls = ["sqlx-macros-core/_tls-rustls"]

# SQLx features
derive = ["sqlx-macros-core/derive"]
macros = ["sqlx-macros-core/macros"]
migrate = ["sqlx-macros-core/migrate"]

# database
Expand Down
6 changes: 6 additions & 0 deletions sqlx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use quote::quote;

use sqlx_macros_core::*;

#[cfg(feature = "macros")]
#[proc_macro]
pub fn expand_query(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as query::QueryMacroInput);
Expand All @@ -21,6 +22,7 @@ pub fn expand_query(input: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "derive")]
#[proc_macro_derive(Encode, attributes(sqlx))]
pub fn derive_encode(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as syn::DeriveInput);
Expand All @@ -30,6 +32,7 @@ pub fn derive_encode(tokenstream: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "derive")]
#[proc_macro_derive(Decode, attributes(sqlx))]
pub fn derive_decode(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as syn::DeriveInput);
Expand All @@ -39,6 +42,7 @@ pub fn derive_decode(tokenstream: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "derive")]
#[proc_macro_derive(Type, attributes(sqlx))]
pub fn derive_type(tokenstream: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(tokenstream as syn::DeriveInput);
Expand All @@ -48,6 +52,7 @@ pub fn derive_type(tokenstream: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "derive")]
#[proc_macro_derive(FromRow, attributes(sqlx))]
pub fn derive_from_row(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
Expand Down Expand Up @@ -77,6 +82,7 @@ pub fn migrate(input: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "macros")]
#[proc_macro_attribute]
pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::ItemFn);
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ pub use sqlx_sqlite::{self as sqlite, Sqlite, SqliteConnection, SqliteExecutor,
#[cfg_attr(docsrs, doc(cfg(feature = "any")))]
pub use crate::any::{reexports::*, Any, AnyExecutor};

#[cfg(feature = "macros")]
#[cfg(any(feature = "derive", feature = "macros"))]
#[doc(hidden)]
pub extern crate sqlx_macros;

// derives
#[cfg(feature = "macros")]
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use sqlx_macros::{FromRow, Type};

Expand Down Expand Up @@ -103,7 +103,7 @@ pub use sqlx_core::rt as __rt;
pub mod types {
pub use sqlx_core::types::*;

#[cfg(feature = "macros")]
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use sqlx_macros::Type;
}
Expand All @@ -112,7 +112,7 @@ pub mod types {
pub mod encode {
pub use sqlx_core::encode::{Encode, IsNull};

#[cfg(feature = "macros")]
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use sqlx_macros::Encode;
}
Expand All @@ -123,7 +123,7 @@ pub use self::encode::Encode;
pub mod decode {
pub use sqlx_core::decode::Decode;

#[cfg(feature = "macros")]
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use sqlx_macros::Decode;
}
Expand Down

0 comments on commit 1c7b3d0

Please sign in to comment.