From 177490cf4d633e78a8955fa1fa7c8acc65e807f4 Mon Sep 17 00:00:00 2001 From: ZacJW Date: Mon, 27 Jan 2025 22:08:43 +0000 Subject: [PATCH] Use generator state for extra type support rather than cargo features --- Cargo.lock | 347 +------------------------------- rasql-core/Cargo.toml | 4 - rasql-core/src/rust/mod.rs | 6 +- rasql-core/src/rust/type_gen.rs | 38 +++- 4 files changed, 42 insertions(+), 353 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 063deb0..a211d09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,23 +17,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" -[[package]] -name = "ahash" -version = "0.7.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "async-trait" version = "0.1.85" @@ -42,7 +25,7 @@ checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn", ] [[package]] @@ -78,18 +61,6 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" -[[package]] -name = "bitvec" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" -dependencies = [ - "funty", - "radium", - "tap", - "wyz", -] - [[package]] name = "block-buffer" version = "0.10.4" @@ -99,57 +70,12 @@ dependencies = [ "generic-array", ] -[[package]] -name = "borsh" -version = "1.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5430e3be710b68d984d1391c854eb431a9d548640711faa54eecb1df93db91cc" -dependencies = [ - "borsh-derive", - "cfg_aliases", -] - -[[package]] -name = "borsh-derive" -version = "1.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8b668d39970baad5356d7c83a86fee3a539e6f93bf6764c97368243e17a0487" -dependencies = [ - "once_cell", - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" -[[package]] -name = "bytecheck" -version = "0.6.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" -dependencies = [ - "bytecheck_derive", - "ptr_meta", - "simdutf8", -] - -[[package]] -name = "bytecheck_derive" -version = "0.6.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "byteorder" version = "1.5.0" @@ -177,12 +103,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" - [[package]] name = "convert_case" version = "0.7.1" @@ -222,24 +142,12 @@ dependencies = [ "subtle", ] -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "fallible-iterator" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" -[[package]] -name = "funty" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" - [[package]] name = "futures-channel" version = "0.3.31" @@ -264,7 +172,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn", ] [[package]] @@ -321,21 +229,6 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] - -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - [[package]] name = "hmac" version = "0.12.1" @@ -345,22 +238,6 @@ dependencies = [ "digest", ] -[[package]] -name = "indexmap" -version = "2.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" -dependencies = [ - "equivalent", - "hashbrown 0.15.2", -] - -[[package]] -name = "itoa" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" - [[package]] name = "js-sys" version = "0.3.77" @@ -429,15 +306,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - [[package]] name = "object" version = "0.36.7" @@ -550,15 +418,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "proc-macro-crate" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" -dependencies = [ - "toml_edit", -] - [[package]] name = "proc-macro2" version = "1.0.93" @@ -577,26 +436,6 @@ dependencies = [ "cc", ] -[[package]] -name = "ptr_meta" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" -dependencies = [ - "ptr_meta_derive", -] - -[[package]] -name = "ptr_meta_derive" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "quote" version = "1.0.38" @@ -606,12 +445,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "radium" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" - [[package]] name = "rand" version = "0.8.5" @@ -654,12 +487,10 @@ dependencies = [ "proc-macro2", "quote", "rasql-traits", - "rust_decimal", "sqlparser", - "syn 2.0.96", + "syn", "thiserror", "tokio-postgres", - "uuid", ] [[package]] @@ -690,7 +521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76009fbe0614077fc1a2ce255e3a1881a2e3a3527097d5dc6d8212c585e7e38b" dependencies = [ "quote", - "syn 2.0.96", + "syn", ] [[package]] @@ -702,116 +533,18 @@ dependencies = [ "bitflags", ] -[[package]] -name = "rend" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" -dependencies = [ - "bytecheck", -] - -[[package]] -name = "rkyv" -version = "0.7.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" -dependencies = [ - "bitvec", - "bytecheck", - "bytes", - "hashbrown 0.12.3", - "ptr_meta", - "rend", - "rkyv_derive", - "seahash", - "tinyvec", - "uuid", -] - -[[package]] -name = "rkyv_derive" -version = "0.7.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "503d1d27590a2b0a3a4ca4c94755aa2875657196ecbf401a42eff41d7de532c0" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "rust_decimal" -version = "1.36.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b082d80e3e3cc52b2ed634388d436fe1f4de6af5786cc2de9ba9737527bdf555" -dependencies = [ - "arrayvec", - "borsh", - "bytes", - "num-traits", - "rand", - "rkyv", - "serde", - "serde_json", -] - [[package]] name = "rustc-demangle" version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" - [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - -[[package]] -name = "serde" -version = "1.0.217" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.217" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.96", -] - -[[package]] -name = "serde_json" -version = "1.0.137" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "930cfb6e6abf99298aaad7d29abbef7a9999a9a8806a40088f55f0dcec03146b" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - [[package]] name = "sha2" version = "0.10.8" @@ -829,12 +562,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "simdutf8" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" - [[package]] name = "siphasher" version = "1.0.1" @@ -906,17 +633,6 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.96" @@ -928,12 +644,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" - [[package]] name = "thiserror" version = "2.0.11" @@ -951,7 +661,7 @@ checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn", ] [[package]] @@ -1023,23 +733,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "toml_datetime" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" - -[[package]] -name = "toml_edit" -version = "0.22.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" -dependencies = [ - "indexmap", - "toml_datetime", - "winnow", -] - [[package]] name = "typenum" version = "1.17.0" @@ -1079,12 +772,6 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" -[[package]] -name = "uuid" -version = "1.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b" - [[package]] name = "version_check" version = "0.9.5" @@ -1124,7 +811,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.96", + "syn", "wasm-bindgen-shared", ] @@ -1146,7 +833,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1263,24 +950,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "winnow" -version = "0.6.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8d71a593cc5c42ad7876e2c1fda56f314f3754c084128833e64f1345ff8a03a" -dependencies = [ - "memchr", -] - -[[package]] -name = "wyz" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" -dependencies = [ - "tap", -] - [[package]] name = "zerocopy" version = "0.7.35" @@ -1299,5 +968,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn", ] diff --git a/rasql-core/Cargo.toml b/rasql-core/Cargo.toml index 9fa9708..d87c7f2 100644 --- a/rasql-core/Cargo.toml +++ b/rasql-core/Cargo.toml @@ -9,8 +9,6 @@ description = "SQL analysis and Rust type generation for Rasql" [features] tokio-postgres = ["rasql-traits/tokio-postgres", "dep:tokio-postgres"] -uuid = ["dep:uuid"] -rust_decimal = ["dep:rust_decimal"] [dependencies] sqlparser = "0.54.0" @@ -21,5 +19,3 @@ syn = { version = "2.0.96", features = ["full"] } tokio-postgres = { version = "0.7.12", optional = true } convert_case = "0.7.1" thiserror = "2.0.11" -uuid = { version = "1.12.1", optional = true } -rust_decimal = { version = "1.36.0", optional = true } diff --git a/rasql-core/src/rust/mod.rs b/rasql-core/src/rust/mod.rs index 3cd0e47..cffe61d 100644 --- a/rasql-core/src/rust/mod.rs +++ b/rasql-core/src/rust/mod.rs @@ -55,6 +55,8 @@ fn generate_table_struct_and_impls< >( table: &crate::sql::Table, module_config: Option<&ModuleCodeGenConfig>, + type_gen: &TypeGen, + client_gen: &ClientGen, ) -> (GeneratedTableStruct, TableStructImpls) { let name = sql_ident_to_type_name(table.name.0.last().unwrap()); let default_struct_config = StructCodeGenConfig { @@ -95,7 +97,7 @@ fn generate_table_struct_and_impls< let r#type = match (&field_config.override_type, field_config.id_promote_mode) { (Some(r#type), _) => r#type.clone(), - (None, IdPromoteMode::None) => TypeGen::sql_datatype_to_rust_type(&column.data_type).unwrap(), + (None, IdPromoteMode::None) => type_gen.sql_datatype_to_rust_type(&column.data_type).unwrap(), (None, IdPromoteMode::TrustedId) => todo!(), (None, IdPromoteMode::Id) => todo!(), }; @@ -114,7 +116,7 @@ fn generate_table_struct_and_impls< db_alias: todo!(), }; ( - GeneratedTableStruct(TypeGen::generate_table_struct(&table_struct)), + GeneratedTableStruct(type_gen.generate_table_struct(&table_struct)), TableStructImpls { base_table_impl: todo!(), table_with_pk_impl: todo!(), diff --git a/rasql-core/src/rust/type_gen.rs b/rasql-core/src/rust/type_gen.rs index 5c86ad3..4d50fe2 100644 --- a/rasql-core/src/rust/type_gen.rs +++ b/rasql-core/src/rust/type_gen.rs @@ -6,10 +6,11 @@ use super::TableStruct; pub trait TypeGenerator { fn sql_datatype_to_rust_type( + &self, datatype: &sqlparser::ast::DataType, ) -> Result; - fn generate_table_struct(table_struct: &TableStruct) -> proc_macro2::TokenStream; + fn generate_table_struct(&self, table_struct: &TableStruct) -> proc_macro2::TokenStream; } #[derive(Debug, Error)] @@ -17,7 +18,23 @@ pub trait TypeGenerator { pub struct UnsupportedDataType(pub sqlparser::ast::DataType); #[cfg(feature = "tokio-postgres")] -pub struct TokioPostgresGenerator; +pub struct TokioPostgresGenerator { + pub use_rust_decimal: UseRustDecimal, + pub use_uuid: UseUuid, +} + +#[cfg(feature = "tokio-postgres")] +pub enum UseRustDecimal { + DontUse, + Version1, +} + +#[cfg(feature = "tokio-postgres")] +pub enum UseUuid { + DontUse, + Version0_8, + Version1, +} #[cfg(feature = "tokio-postgres")] impl TokioPostgresGenerator { @@ -33,6 +50,7 @@ impl TokioPostgresGenerator { #[cfg(feature = "tokio-postgres")] impl TypeGenerator for TokioPostgresGenerator { fn sql_datatype_to_rust_type( + &self, datatype: &sqlparser::ast::DataType, ) -> Result { Ok(match datatype { @@ -50,8 +68,11 @@ impl TypeGenerator for TokioPostgresGenerator | sqlparser::ast::DataType::FixedString(_) => { syn::Type::Verbatim(quote::quote! {String}) } - #[cfg(feature = "uuid")] - sqlparser::ast::DataType::Uuid => syn::Type::Verbatim(quote::quote! {uuid::Uuid}), + sqlparser::ast::DataType::Uuid + if matches!(self.use_uuid, UseUuid::Version0_8 | UseUuid::Version1) => + { + syn::Type::Verbatim(quote::quote! {uuid::Uuid}) + } sqlparser::ast::DataType::Varbinary(_) | sqlparser::ast::DataType::Blob(_) | sqlparser::ast::DataType::TinyBlob @@ -60,10 +81,11 @@ impl TypeGenerator for TokioPostgresGenerator | sqlparser::ast::DataType::Bytes(_) | sqlparser::ast::DataType::Bytea | sqlparser::ast::DataType::Binary(_) => syn::Type::Verbatim(quote::quote! {Vec}), - #[cfg(feature = "rust_decimal")] sqlparser::ast::DataType::Numeric(..) | sqlparser::ast::DataType::Decimal(..) - | sqlparser::ast::DataType::Dec(..) => { + | sqlparser::ast::DataType::Dec(..) + if matches!(self.use_rust_decimal, UseRustDecimal::Version1) => + { syn::Type::Verbatim(quote::quote! {rust_decimal::Decimal}) } sqlparser::ast::DataType::Int2(_) => syn::Type::Verbatim(quote::quote! {i16}), @@ -119,7 +141,7 @@ impl TypeGenerator for TokioPostgresGenerator sqlparser::ast::ArrayElemTypeDef::AngleBracket(data_type) | sqlparser::ast::ArrayElemTypeDef::SquareBracket(data_type, _) | sqlparser::ast::ArrayElemTypeDef::Parenthesis(data_type) => { - let inner_type = Self::sql_datatype_to_rust_type(&datatype)?; + let inner_type = self.sql_datatype_to_rust_type(&datatype)?; syn::Type::Verbatim(quote::quote! {Vec<#inner_type>}) } }, @@ -137,7 +159,7 @@ impl TypeGenerator for TokioPostgresGenerator }) } - fn generate_table_struct(table_struct: &TableStruct) -> proc_macro2::TokenStream { + fn generate_table_struct(&self, table_struct: &TableStruct) -> proc_macro2::TokenStream { let TableStruct { name, fields,