Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wtx): Add optional support for uuid format in postgres. #293

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .scripts/internal-tests-0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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.

1 change: 1 addition & 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.0" }
webpki-roots = { default-features = false, optional = true, version = "0.26" }
x509-certificate = { default-features = false, optional = true, version = "0.24" }

Expand Down
40 changes: 40 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,43 @@ mod serde_json {
const TY: Ty = Ty::Jsonb;
}
}

#[cfg(feature = "uuid")]
mod uuid {
use crate::database::{
client::postgres::{DecodeValue, EncodeValue, Postgres, Ty},
Decode, Encode, Typed,
};
use uuid::Uuid;

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;
}

test!(uuid, Uuid, Uuid::max());
}
10 changes: 10 additions & 0 deletions wtx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 Expand Up @@ -441,6 +443,14 @@ impl<T> From<std::sync::TryLockError<T>> for Error {
}
}

#[cfg(feature = "uuid")]
impl From<uuid::Error> for Error {
#[inline]
fn from(value: uuid::Error) -> Self {
Self::UuidError(value.into())
}
}

#[cfg(feature = "x509-certificate")]
impl From<x509_certificate::X509CertificateError> for Error {
#[inline]
Expand Down
Loading