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 25, 2023
1 parent d29b7f9 commit 9b4a6f4
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
25 changes: 25 additions & 0 deletions diesel/src/pg/types/mac_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ impl ToSql<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> {
<[u8; 6] as FromSql<MacAddr, Pg>>::from_sql(value).map(Into::into)
}
}

#[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 {
let array_value = self.into_array();
<[u8; 6] as ToSql<MacAddr, Pg>>::to_sql(&array_value, &mut out.reborrow())
}
}

#[test]
fn macaddr_roundtrip() {
use crate::query_builder::bind_collector::ByteWrapper;
Expand All @@ -45,6 +60,16 @@ fn macaddr_roundtrip() {
let mut bytes = Output::test(ByteWrapper(&mut buffer));
let input_address = [0x52, 0x54, 0x00, 0xfb, 0xc6, 0x16];
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 input_address: MacAddr6 = input_address.into();
let output_address: MacAddr6 = FromSql::from_sql(PgValue::for_test(&buffer)).unwrap();
assert_eq!(input_address, output_address);
}
}
14 changes: 14 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,14 @@ fn pg_macaddress_to_sql_macaddress() {
expected_value,
value
));

#[cfg(feature = "macaddr")]
{
assert!(query_to_sql_equality::<MacAddr, macaddr::MacAddr6>(
expected_value,
macaddr::MacAddr6::new(0x08, 0x00, 0x2b, 0x01, 0x02, 0x03)
));
}
}

#[test]
Expand Down

0 comments on commit 9b4a6f4

Please sign in to comment.