Skip to content

Commit

Permalink
Implemented AsDbType for MacAddress, IpNetwork and BitVec
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 8, 2023
1 parent c77f3ec commit b8c2ef3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mac_address = { version = "~1", optional = true }
bit-vec = { version = "~0.6", optional = true }

# Ip network support (postgres-only)
ip_network = { version = "~0.4", optional = true }
ipnetwork = { version = "~0.20", optional = true }

# Date and time support
chrono = { version = ">=0.4.20", default-features = false, optional = true }
Expand Down Expand Up @@ -92,7 +92,7 @@ postgres-only = [
"rorm-db/postgres-only",
"rorm-cli?/postgres",
"dep:mac_address",
"dep:ip_network",
"dep:ipnetwork",
"dep:bit-vec",
]

Expand Down
2 changes: 1 addition & 1 deletion rorm-db
Submodule rorm-db updated 2 files
+4 −0 Cargo.toml
+35 −14 src/sqlx_impl/utils.rs
2 changes: 1 addition & 1 deletion rorm-sql
Submodule rorm-sql updated 2 files
+4 −2 Cargo.toml
+6 −6 src/value.rs
15 changes: 15 additions & 0 deletions src/conditions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ pub enum Value<'a> {
/// Uuid representation
#[cfg(feature = "uuid")]
Uuid(uuid::Uuid),
/// Mac address representation
#[cfg(feature = "postgres-only")]
MacAddress(mac_address::MacAddress),
/// IP network presentation
#[cfg(feature = "postgres-only")]
IpNetwork(ipnetwork::IpNetwork),
/// Bit vec representation
#[cfg(feature = "postgres-only")]
BitVec(crate::fields::types::postgres_only::BitCow<'a>),
}
impl<'a> Value<'a> {
/// Convert into an [`sql::Value`](value::Value) instead of an [`sql::Condition`](conditional::Condition) directly.
Expand Down Expand Up @@ -138,6 +147,12 @@ impl<'a> Value<'a> {
Value::TimePrimitiveDateTime(v) => value::Value::TimePrimitiveDateTime(*v),
#[cfg(feature = "uuid")]
Value::Uuid(v) => value::Value::Uuid(*v),
#[cfg(feature = "postgres-only")]
Value::MacAddress(v) => value::Value::MacAddress(*v),
#[cfg(feature = "postgres-only")]
Value::IpNetwork(v) => value::Value::IpNetwork(*v),
#[cfg(feature = "postgres-only")]
Value::BitVec(v) => value::Value::BitVec(v.as_ref()),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/fields/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod foreign_model;
mod json;
#[cfg(feature = "msgpack")]
mod msgpack;
#[cfg(feature = "postgres-only")]
pub(crate) mod postgres_only;
#[cfg(feature = "time")]
mod time;
#[cfg(feature = "uuid")]
Expand Down
31 changes: 31 additions & 0 deletions src/fields/types/postgres_only.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bit_vec::BitVec;
use ipnetwork::IpNetwork;
use mac_address::MacAddress;

use crate::conditions::Value;
use crate::impl_AsDbType;
use crate::internal::hmr::db_type;

impl_AsDbType!(MacAddress, db_type::MacAddress, Value::MacAddress);
impl_AsDbType!(IpNetwork, db_type::IpNetwork, Value::IpNetwork);
impl_AsDbType!(
BitVec,
db_type::BitVec,
|vec| Value::BitVec(BitCow::Owned(vec)),
|vec| Value::BitVec(BitCow::Borrowed(vec))
);

#[derive(Clone, Debug)]
pub enum BitCow<'a> {
Borrowed(&'a BitVec),
Owned(BitVec),
}

impl AsRef<BitVec> for BitCow<'_> {
fn as_ref(&self) -> &BitVec {
match self {
BitCow::Borrowed(bit_vec) => bit_vec,
BitCow::Owned(bit_vec) => bit_vec,
}
}
}
16 changes: 14 additions & 2 deletions src/internal/hmr/db_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,20 @@ impl_db_types!(
ChronoNaiveTime,
/// Type level version of [`imr::DbType::Choices`]
Choices,
Choice, // TODO requires choices
/// Uuid representation
Choice,
/// Type level version of [`imr::DbType::Uuid`]
Uuid,
Uuid,
);
#[cfg(feature = "postgres-only")]
impl_db_types!(
/// Type level version of [`imr::DbType::MacAddress`]
MacAddress,
MacAddress,
/// Type level version of [`imr::DbType::IpNetwork`]
IpNetwork,
IpNetwork,
/// Type level version of [`imr::DbType::BitVec`]
BitVec,
BitVec,
);

0 comments on commit b8c2ef3

Please sign in to comment.