From f7b5cacd163cbf052c4396da01ddc01e44f7430e Mon Sep 17 00:00:00 2001 From: Pmarquez <48651252+pxp9@users.noreply.github.com> Date: Mon, 14 Oct 2024 08:46:21 +0200 Subject: [PATCH] fix compiling issue with diesel asynk (#157) * fix compiling issue with diesel asynk * fix optional compilation, FangQueryable * rework optional compilation --- fang/Cargo.toml | 8 ++++---- fang/src/asynk.rs | 20 +++++++++++++++----- fang/src/asynk/backend_sqlx.rs | 19 ++++++------------- fang/src/lib.rs | 21 +++++++++++++++------ 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/fang/Cargo.toml b/fang/Cargo.toml index 5d8bc106..97be1657 100644 --- a/fang/Cargo.toml +++ b/fang/Cargo.toml @@ -26,10 +26,10 @@ blocking = ["dep:diesel", "dep:diesel-derive-enum", "dep:dotenvy", "diesel?/chro blocking-postgres = [ "blocking", "diesel?/postgres"] blocking-sqlite = ["blocking", "diesel?/sqlite" ] blocking-mysql = [ "blocking", "diesel?/mysql"] -migrations-postgres = ["migrations"] -migrations-sqlite = ["migrations"] -migrations-mysql = ["migrations"] -migrations = ["dep:diesel_migrations"] +migrations-postgres = ["migrations", "diesel?/postgres"] +migrations-sqlite = ["migrations", "diesel?/sqlite"] +migrations-mysql = ["migrations", "diesel?/mysql"] +migrations = ["dep:diesel_migrations", "dep:diesel"] [dev-dependencies] diff --git a/fang/src/asynk.rs b/fang/src/asynk.rs index 2a8ab87d..58d8fcf8 100644 --- a/fang/src/asynk.rs +++ b/fang/src/asynk.rs @@ -1,10 +1,20 @@ +// Here you should put all the backends based on sqlx library +#[cfg(any( + feature = "asynk-postgres", + feature = "asynk-mysql", + feature = "asynk-sqlite" +))] +pub mod backend_sqlx; + pub mod async_queue; pub mod async_runnable; pub mod async_worker; pub mod async_worker_pool; -pub mod backend_sqlx; -pub use async_queue::*; -pub use async_runnable::AsyncRunnable; -pub use async_worker::*; -pub use async_worker_pool::*; +// Here you should put all the backends. +#[cfg(any( + feature = "asynk-postgres", + feature = "asynk-mysql", + feature = "asynk-sqlite" +))] +pub use {async_queue::*, async_runnable::AsyncRunnable, async_worker::*, async_worker_pool::*}; diff --git a/fang/src/asynk/backend_sqlx.rs b/fang/src/asynk/backend_sqlx.rs index 4093ed13..cfe57bb9 100644 --- a/fang/src/asynk/backend_sqlx.rs +++ b/fang/src/asynk/backend_sqlx.rs @@ -1,15 +1,11 @@ -use chrono::{DateTime, Duration, Utc}; +use chrono::{DateTime, Utc}; use sha2::Digest; use sha2::Sha256; -use sqlx::any::AnyQueryResult; -use sqlx::database::HasArguments; -use sqlx::Database; -use sqlx::Encode; -use sqlx::Executor; -use sqlx::FromRow; -use sqlx::IntoArguments; -use sqlx::Pool; -use sqlx::Type; +use { + chrono::Duration, sqlx::any::AnyQueryResult, sqlx::database::HasArguments, sqlx::Database, + sqlx::Encode, sqlx::Executor, sqlx::FromRow, sqlx::IntoArguments, sqlx::Pool, sqlx::Type, +}; + use std::fmt::Debug; use typed_builder::TypedBuilder; use uuid::Uuid; @@ -40,7 +36,6 @@ pub(crate) enum BackendSqlX { MySql, } -#[allow(dead_code)] #[derive(TypedBuilder, Clone)] pub(crate) struct QueryParams<'a> { #[builder(default, setter(strip_option))] @@ -63,7 +58,6 @@ pub(crate) struct QueryParams<'a> { task: Option<&'a Task>, } -#[allow(dead_code)] pub(crate) enum Res { Bigint(u64), Task(Task), @@ -143,7 +137,6 @@ use crate::FangTaskState; use crate::InternalPool; use crate::Task; -#[allow(dead_code)] pub(crate) fn calculate_hash(json: &str) -> String { let mut hasher = Sha256::new(); hasher.update(json.as_bytes()); diff --git a/fang/src/lib.rs b/fang/src/lib.rs index 7c1d0096..f50abe7f 100644 --- a/fang/src/lib.rs +++ b/fang/src/lib.rs @@ -149,7 +149,7 @@ impl From for &str { #[derive(Debug, Eq, PartialEq, Clone, TypedBuilder)] #[cfg_attr(feature = "blocking", derive(Queryable, Identifiable))] -#[diesel(table_name = fang_tasks)] +#[cfg_attr(feature = "blocking", diesel(table_name = fang_tasks))] pub struct Task { #[builder(setter(into))] pub id: Uuid, @@ -204,9 +204,18 @@ pub mod blocking; #[cfg(feature = "blocking")] pub use blocking::*; -#[cfg(feature = "asynk")] +#[cfg(any( + feature = "asynk-postgres", + feature = "asynk-mysql", + feature = "asynk-sqlite" +))] pub mod asynk; +#[cfg(any( + feature = "asynk-postgres", + feature = "asynk-mysql", + feature = "asynk-sqlite" +))] #[cfg(feature = "asynk")] pub use asynk::*; @@ -218,10 +227,10 @@ pub use async_trait::async_trait; pub use fang_derive_error::ToFangError; #[cfg(feature = "migrations")] -use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; - -#[cfg(feature = "migrations")] -use std::error::Error as SomeError; +use { + diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}, + std::error::Error as SomeError, +}; #[cfg(feature = "migrations-postgres")] use diesel::pg::Pg;