From 920bd6b1b91b0f9ddf08e3bd568a36863d436ae5 Mon Sep 17 00:00:00 2001 From: Nikita Bishonen Date: Sat, 21 Dec 2024 12:58:32 +0000 Subject: [PATCH 1/2] feat(wtx): Add optional support for uuid format in postgres. --- Cargo.lock | 1 + wtx/Cargo.toml | 2 ++ wtx/src/database/client/postgres/tys.rs | 47 +++++++++++++++++++++++++ wtx/src/error.rs | 2 ++ 4 files changed, 52 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6cf50cc1..fc6c9bc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2493,6 +2493,7 @@ dependencies = [ "tracing", "tracing-subscriber", "tracing-tree", + "uuid", "webpki-roots", "x509-certificate", ] diff --git a/wtx/Cargo.toml b/wtx/Cargo.toml index e29c1e9d..46dffe00 100644 --- a/wtx/Cargo.toml +++ b/wtx/Cargo.toml @@ -41,6 +41,7 @@ tokio-rustls = { default-features = false, features = ["ring"], optional = true, tracing = { default-features = false, features = ["attributes"], optional = true, version = "0.1" } tracing-subscriber = { default-features = false, features = ["env-filter", "fmt"], optional = true, version = "0.3" } tracing-tree = { default-features = false, optional = true, version = "0.4" } +uuid = { default-features = false, optional = true, version = "1.11.0" } webpki-roots = { default-features = false, optional = true, version = "0.26" } x509-certificate = { default-features = false, optional = true, version = "0.24" } @@ -113,6 +114,7 @@ std = [ ] tokio = ["std", "dep:tokio"] tokio-rustls = ["ring", "rustls", "dep:rustls-pemfile", "rustls-pki-types", "tokio", "dep:tokio-rustls"] +uuid = ["dep:uuid"] web-socket = ["http"] web-socket-handshake = ["base64", "httparse", "sha1", "web-socket"] diff --git a/wtx/src/database/client/postgres/tys.rs b/wtx/src/database/client/postgres/tys.rs index 17065768..4aaa2972 100644 --- a/wtx/src/database/client/postgres/tys.rs +++ b/wtx/src/database/client/postgres/tys.rs @@ -836,3 +836,50 @@ mod serde_json { const TY: Ty = Ty::Jsonb; } } + +#[cfg(feature = "uuid")] +mod uuid { + use std::prelude::v1::Box; + + use uuid::{Error as UuidError, Uuid}; + + use crate::database::{ + client::postgres::{DecodeValue, EncodeValue, Postgres, Ty}, + Decode, Encode, Typed, + }; + + impl<'de, E> Decode<'de, Postgres> for Uuid + where + E: From, + { + #[inline] + fn decode(input: &DecodeValue<'de>) -> Result { + let elem = Uuid::from_slice(input.bytes()).map_err(Into::into)?; + Ok(elem) + } + } + + impl Encode> for Uuid + where + E: From, + { + #[inline] + fn encode(&self, ev: &mut EncodeValue<'_, '_>) -> Result<(), E> { + ev.fbw()._extend_from_slice(self.as_bytes()).map_err(Into::into)?; + Ok(()) + } + } + + impl Typed> for Uuid + where + E: From, + { + const TY: Ty = Ty::Uuid; + } + + impl From for crate::Error { + fn from(value: UuidError) -> Self { + Self::UuidError(Box::new(value)) + } + } +} diff --git a/wtx/src/error.rs b/wtx/src/error.rs index b0aebe63..d4165823 100644 --- a/wtx/src/error.rs +++ b/wtx/src/error.rs @@ -70,6 +70,8 @@ pub enum Error { TryInitError(tracing_subscriber::util::TryInitError), #[cfg(feature = "std")] TryLockError(std::sync::TryLockError<()>), + #[cfg(feature = "uuid")] + UuidError(Box), #[cfg(feature = "x509-certificate")] X509CertificateError(Box), From 97e2d38fb6fe1e789ec34a1213c04c8039865373 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 23 Dec 2024 11:46:14 -0300 Subject: [PATCH 2/2] Tweaks --- .scripts/internal-tests-0.sh | 1 + wtx/Cargo.toml | 3 +-- wtx/src/database/client/postgres/tys.rs | 11 ++--------- wtx/src/error.rs | 8 ++++++++ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.scripts/internal-tests-0.sh b/.scripts/internal-tests-0.sh index b2aee8ca..72f332c2 100755 --- a/.scripts/internal-tests-0.sh +++ b/.scripts/internal-tests-0.sh @@ -56,6 +56,7 @@ $rt test-with-features wtx std $rt test-with-features wtx tokio $rt test-with-features wtx tokio-rustls $rt test-with-features wtx tracing +$rt test-with-features wtx uuid $rt test-with-features wtx web-socket $rt test-with-features wtx web-socket-handshake $rt test-with-features wtx webpki-roots diff --git a/wtx/Cargo.toml b/wtx/Cargo.toml index 46dffe00..00bd9bc8 100644 --- a/wtx/Cargo.toml +++ b/wtx/Cargo.toml @@ -41,7 +41,7 @@ tokio-rustls = { default-features = false, features = ["ring"], optional = true, tracing = { default-features = false, features = ["attributes"], optional = true, version = "0.1" } tracing-subscriber = { default-features = false, features = ["env-filter", "fmt"], optional = true, version = "0.3" } tracing-tree = { default-features = false, optional = true, version = "0.4" } -uuid = { default-features = false, optional = true, version = "1.11.0" } +uuid = { default-features = false, optional = true, version = "1.0" } webpki-roots = { default-features = false, optional = true, version = "0.26" } x509-certificate = { default-features = false, optional = true, version = "0.24" } @@ -114,7 +114,6 @@ std = [ ] tokio = ["std", "dep:tokio"] tokio-rustls = ["ring", "rustls", "dep:rustls-pemfile", "rustls-pki-types", "tokio", "dep:tokio-rustls"] -uuid = ["dep:uuid"] web-socket = ["http"] web-socket-handshake = ["base64", "httparse", "sha1", "web-socket"] diff --git a/wtx/src/database/client/postgres/tys.rs b/wtx/src/database/client/postgres/tys.rs index 4aaa2972..b8b86e12 100644 --- a/wtx/src/database/client/postgres/tys.rs +++ b/wtx/src/database/client/postgres/tys.rs @@ -839,14 +839,11 @@ mod serde_json { #[cfg(feature = "uuid")] mod uuid { - use std::prelude::v1::Box; - - use uuid::{Error as UuidError, Uuid}; - use crate::database::{ client::postgres::{DecodeValue, EncodeValue, Postgres, Ty}, Decode, Encode, Typed, }; + use uuid::Uuid; impl<'de, E> Decode<'de, Postgres> for Uuid where @@ -877,9 +874,5 @@ mod uuid { const TY: Ty = Ty::Uuid; } - impl From for crate::Error { - fn from(value: UuidError) -> Self { - Self::UuidError(Box::new(value)) - } - } + test!(uuid, Uuid, Uuid::max()); } diff --git a/wtx/src/error.rs b/wtx/src/error.rs index d4165823..bf259219 100644 --- a/wtx/src/error.rs +++ b/wtx/src/error.rs @@ -443,6 +443,14 @@ impl From> for Error { } } +#[cfg(feature = "uuid")] +impl From for Error { + #[inline] + fn from(value: uuid::Error) -> Self { + Self::UuidError(value.into()) + } +} + #[cfg(feature = "x509-certificate")] impl From for Error { #[inline]