Skip to content

Commit

Permalink
support macaddr::MacAddr6 with Pg
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed May 23, 2023
1 parent 201e4b8 commit 132ad80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bitflags = { version = "2.0.0", optional = true }
r2d2 = { version = ">= 0.8.2, < 0.9.0", optional = true }
itoa = { version = "1.0.0", optional = true }
time = { version = "0.3.9", optional = true, features = ["macros"] }
macaddr = { version = "1.0.1", optional = true }

[dependencies.diesel_derives]
version = "~2.0.0"
Expand All @@ -47,7 +48,7 @@ quickcheck = "1.0.3"

[features]
default = ["with-deprecated", "32-column-tables"]
extras = ["chrono", "time", "serde_json", "uuid", "network-address", "numeric", "r2d2"]
extras = ["chrono", "time", "serde_json", "uuid", "network-address", "numeric", "r2d2", "macaddr"]
unstable = ["diesel_derives/nightly"]
large-tables = ["32-column-tables"]
huge-tables = ["64-column-tables"]
Expand Down
27 changes: 27 additions & 0 deletions diesel/src/pg/types/mac_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ impl FromSql<MacAddr, Pg> for [u8; 6] {
}
}

#[cfg(all(feature = "macaddr", feature = "postgres_backend"))]
impl FromSql<MacAddr, Pg> for macaddr::MacAddr6 {
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
value
.as_bytes()
.try_into()
.map_err(|_| "invalid network address format: input isn't 6 bytes.".into())
}
}

#[cfg(feature = "postgres_backend")]
impl ToSql<MacAddr, Pg> for [u8; 6] {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
Expand All @@ -37,6 +47,15 @@ impl ToSql<MacAddr, Pg> for [u8; 6] {
}
}

#[cfg(all(feature = "macaddr", feature = "postgres_backend"))]
impl ToSql<MacAddr, Pg> for macaddr::MacAddr6 {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_all(&self[..])
.map(|_| IsNull::No)
.map_err(Into::into)
}
}

#[test]
fn macaddr_roundtrip() {
use crate::query_builder::bind_collector::ByteWrapper;
Expand All @@ -47,4 +66,12 @@ fn macaddr_roundtrip() {
ToSql::<MacAddr, Pg>::to_sql(&input_address, &mut bytes).unwrap();
let output_address: [u8; 6] = FromSql::from_sql(PgValue::for_test(&buffer)).unwrap();
assert_eq!(input_address, output_address);

#[cfg(feature = "macaddr")]
{
use macaddr::MacAddr6;

let output_address: MacAddr6 = FromSql::from_sql(PgValue::for_test(&buffer)).unwrap();
assert_eq!(input_address, output_address);
}
}
12 changes: 12 additions & 0 deletions diesel_tests/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,12 @@ fn pg_macaddress_from_sql() {
expected_value,
query_single_value::<MacAddr, [u8; 6]>(query)
);

#[cfg(feature = "macaddr")]
assert_eq!(
expected_value,
query_single_value::<MacAddr, macaddr::MacAddr6>(query)
);
}

#[test]
Expand All @@ -1059,6 +1065,12 @@ fn pg_macaddress_to_sql_macaddress() {
expected_value,
value
));

#[cfg(feature = "macaddr")]
assert!(query_to_sql_equality::<MacAddr, macaddr::MacAddr6>(
expected_value,
value
));
}

#[test]
Expand Down

0 comments on commit 132ad80

Please sign in to comment.