Skip to content

Commit

Permalink
Add conversions from Uuid 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mati865 committed Apr 20, 2022
1 parent 024794a commit 1d9c93d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postgres-types/Cargo.toml
Expand Up @@ -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"]

Expand All @@ -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 }
2 changes: 2 additions & 0 deletions postgres-types/src/lib.rs
Expand Up @@ -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")]
Expand Down
25 changes: 25 additions & 0 deletions 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<Uuid, Box<dyn Error + Sync + Send>> {
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<IsNull, Box<dyn Error + Sync + Send>> {
types::uuid_to_sql(*self.as_bytes(), w);
Ok(IsNull::No)
}

accepts!(UUID);
to_sql_checked!();
}
1 change: 1 addition & 0 deletions postgres/Cargo.toml
Expand Up @@ -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"]

Expand Down
1 change: 1 addition & 0 deletions postgres/src/lib.rs
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions tokio-postgres/Cargo.toml
Expand Up @@ -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"]

Expand Down Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions tokio-postgres/src/lib.rs
Expand Up @@ -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")]
Expand Down
2 changes: 2 additions & 0 deletions tokio-postgres/tests/test/types/mod.rs
Expand Up @@ -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<T, S>(sql_type: &str, checks: &[(T, S)])
where
Expand Down
18 changes: 18 additions & 0 deletions 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
}

0 comments on commit 1d9c93d

Please sign in to comment.