Skip to content

Commit

Permalink
feat(wtx): Add optional support for uuid format in postgres.
Browse files Browse the repository at this point in the history
  • Loading branch information
humb1t committed Dec 21, 2024
1 parent 3c9c367 commit 64320ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions wtx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down Expand Up @@ -114,6 +115,7 @@ std = [
tls = []
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"]

Expand Down
47 changes: 47 additions & 0 deletions wtx/src/database/client/postgres/tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>> for Uuid
where
E: From<crate::Error>,
{
#[inline]
fn decode(input: &DecodeValue<'de>) -> Result<Self, E> {
let elem = Uuid::from_slice(input.bytes()).map_err(Into::into)?;
Ok(elem)
}
}

impl<E> Encode<Postgres<E>> for Uuid
where
E: From<crate::Error>,
{
#[inline]
fn encode(&self, ev: &mut EncodeValue<'_, '_>) -> Result<(), E> {
ev.fbw()._extend_from_slice(self.as_bytes()).map_err(Into::into)?;
Ok(())
}
}

impl<E> Typed<Postgres<E>> for Uuid
where
E: From<crate::Error>,
{
const TY: Ty = Ty::Uuid;
}

impl From<UuidError> for crate::Error {
fn from(value: UuidError) -> Self {
Self::UuidError(Box::new(value))
}
}
}
2 changes: 2 additions & 0 deletions wtx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum Error {
TryInitError(tracing_subscriber::util::TryInitError),
#[cfg(feature = "std")]
TryLockError(std::sync::TryLockError<()>),
#[cfg(feature = "uuid")]
UuidError(Box<uuid::Error>),
#[cfg(feature = "x509-certificate")]
X509CertificateError(Box<x509_certificate::X509CertificateError>),

Expand Down

0 comments on commit 64320ec

Please sign in to comment.