From 1d9c93d2aeff5dbb93f9d6e0d59c5f817a6162b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Wed, 20 Apr 2022 16:52:47 +0200 Subject: [PATCH] Add conversions from Uuid 1.0 --- postgres-types/Cargo.toml | 2 ++ postgres-types/src/lib.rs | 2 ++ postgres-types/src/uuid_1.rs | 25 +++++++++++++++++++++++ postgres/Cargo.toml | 1 + postgres/src/lib.rs | 1 + tokio-postgres/Cargo.toml | 2 ++ tokio-postgres/src/lib.rs | 1 + tokio-postgres/tests/test/types/mod.rs | 2 ++ tokio-postgres/tests/test/types/uuid_1.rs | 18 ++++++++++++++++ 9 files changed, 54 insertions(+) create mode 100644 postgres-types/src/uuid_1.rs create mode 100644 tokio-postgres/tests/test/types/uuid_1.rs diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml index 000d71ea..e0ccb949 100644 --- a/postgres-types/Cargo.toml +++ b/postgres-types/Cargo.toml @@ -22,6 +22,7 @@ with-geo-types-0_6 = ["geo-types-06"] with-geo-types-0_7 = ["geo-types-0_7"] with-serde_json-1 = ["serde-1", "serde_json-1"] with-uuid-0_8 = ["uuid-08"] +with-uuid-1 = ["uuid-1"] with-time-0_2 = ["time-02"] with-time-0_3 = ["time-03"] @@ -42,5 +43,6 @@ geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true } serde-1 = { version = "1.0", package = "serde", optional = true } serde_json-1 = { version = "1.0", package = "serde_json", optional = true } uuid-08 = { version = "0.8", package = "uuid", optional = true } +uuid-1 = { version = "1.0", package = "uuid", optional = true } time-02 = { version = "0.2", package = "time", optional = true } time-03 = { version = "0.3", package = "time", default-features = false, optional = true } diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index d029d394..f9c1c5ce 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -230,6 +230,8 @@ mod time_02; mod time_03; #[cfg(feature = "with-uuid-0_8")] mod uuid_08; +#[cfg(feature = "with-uuid-1")] +mod uuid_1; // The time::{date, time} macros produce compile errors if the crate package is renamed. #[cfg(feature = "with-time-0_2")] diff --git a/postgres-types/src/uuid_1.rs b/postgres-types/src/uuid_1.rs new file mode 100644 index 00000000..d9969f60 --- /dev/null +++ b/postgres-types/src/uuid_1.rs @@ -0,0 +1,25 @@ +use bytes::BytesMut; +use postgres_protocol::types; +use std::error::Error; +use uuid_1::Uuid; + +use crate::{FromSql, IsNull, ToSql, Type}; + +impl<'a> FromSql<'a> for Uuid { + fn from_sql(_: &Type, raw: &[u8]) -> Result> { + let bytes = types::uuid_from_sql(raw)?; + Ok(Uuid::from_bytes(bytes)) + } + + accepts!(UUID); +} + +impl ToSql for Uuid { + fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result> { + types::uuid_to_sql(*self.as_bytes(), w); + Ok(IsNull::No) + } + + accepts!(UUID); + to_sql_checked!(); +} diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index b61e42ac..12f9e98e 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -30,6 +30,7 @@ with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"] with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"] with-serde_json-1 = ["tokio-postgres/with-serde_json-1"] with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"] +with-uuid-1 = ["tokio-postgres/with-uuid-1"] with-time-0_2 = ["tokio-postgres/with-time-0_2"] with-time-0_3 = ["tokio-postgres/with-time-0_3"] diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index a599532e..fbe85cbd 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -61,6 +61,7 @@ //! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no | //! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no | +//! | `with-uuid-1` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 1.0 | no | //! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no | //! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no | #![warn(clippy::all, rust_2018_idioms, missing_docs)] diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index 82e71fb1..acb4d04e 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -36,6 +36,7 @@ with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"] with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"] with-serde_json-1 = ["postgres-types/with-serde_json-1"] with-uuid-0_8 = ["postgres-types/with-uuid-0_8"] +with-uuid-1 = ["postgres-types/with-uuid-1"] with-time-0_2 = ["postgres-types/with-time-0_2"] with-time-0_3 = ["postgres-types/with-time-0_3"] @@ -70,5 +71,6 @@ geo-types-07 = { version = "0.7", package = "geo-types" } serde-1 = { version = "1.0", package = "serde" } serde_json-1 = { version = "1.0", package = "serde_json" } uuid-08 = { version = "0.8", package = "uuid" } +uuid-1 = { version = "1.0", package = "uuid" } time-02 = { version = "0.2", package = "time" } time-03 = { version = "0.3", package = "time", features = ["parsing"] } diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index e9516e0b..9afb9380 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -112,6 +112,7 @@ //! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no | //! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no | +//! | `with-uuid-1` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 1.0 | no | //! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no | //! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no | #![doc(html_root_url = "https://docs.rs/tokio-postgres/0.7")] diff --git a/tokio-postgres/tests/test/types/mod.rs b/tokio-postgres/tests/test/types/mod.rs index de700d79..e3fd663a 100644 --- a/tokio-postgres/tests/test/types/mod.rs +++ b/tokio-postgres/tests/test/types/mod.rs @@ -33,6 +33,8 @@ mod time_02; mod time_03; #[cfg(feature = "with-uuid-0_8")] mod uuid_08; +#[cfg(feature = "with-uuid-1")] +mod uuid_1; async fn test_type(sql_type: &str, checks: &[(T, S)]) where diff --git a/tokio-postgres/tests/test/types/uuid_1.rs b/tokio-postgres/tests/test/types/uuid_1.rs new file mode 100644 index 00000000..0eb89be8 --- /dev/null +++ b/tokio-postgres/tests/test/types/uuid_1.rs @@ -0,0 +1,18 @@ +use uuid_1::Uuid; + +use crate::types::test_type; + +#[tokio::test] +async fn test_uuid_params() { + test_type( + "UUID", + &[ + ( + Some(Uuid::parse_str("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11").unwrap()), + "'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'", + ), + (None, "NULL"), + ], + ) + .await +}