Skip to content

Commit

Permalink
feat(named-args): use std::collections::HashMap instead of indexmap
Browse files Browse the repository at this point in the history
… crate
  • Loading branch information
MrFoxPro committed Mar 27, 2024
1 parent ab1db25 commit cb5ad3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
5 changes: 2 additions & 3 deletions edgedb-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ chrono = {version="0.4.23", optional=true, features=["std"], default-features=fa
edgedb-errors = {path = "../edgedb-errors", version = "0.4.0" }
bitflags = "2.4.0"
serde = {version="1.0.190", optional=true}
indexmap = {version = "2.2", optional=true}

[features]
default = []
default = ["macros"]
with-num-bigint = ["num-bigint", "num-traits"]
with-bigdecimal = ["bigdecimal", "num-bigint", "num-traits"]
with-chrono = ["chrono"]
all-types = ["with-num-bigint", "with-bigdecimal", "with-chrono"]
with-serde = ["serde"]
macros = ["indexmap"]
macros = []

[dev-dependencies]
rand = "0.8"
Expand Down
38 changes: 22 additions & 16 deletions edgedb-protocol/src/query_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,27 +594,33 @@ where

#[cfg(feature = "macros")]
pub mod macros {
use crate::query_arg::{Value, ValueWithCardinality, object_from_pairs};

pub struct EdgedbArgsIndexMap<'i>(pub indexmap::IndexMap<&'i str, ValueWithCardinality>);
impl EdgedbArgsIndexMap<'_> {
pub fn to_value(self) -> Value {
Value::from(self)
}
}
use crate::query_arg::{object_from_pairs, Value, ValueWithCardinality};

impl From<EdgedbArgsIndexMap<'_>> for Value {
fn from(value: EdgedbArgsIndexMap) -> Self {
object_from_pairs(value.0)
}
}
pub struct EdgedbArgsIndexMap<'i>(pub std::collections::HashMap<&'i str, ValueWithCardinality>);
impl EdgedbArgsIndexMap<'_> {
pub fn to_value(self) -> Value {
Value::from(self)
}
}

impl From<EdgedbArgsIndexMap<'_>> for Value {
fn from(value: EdgedbArgsIndexMap) -> Self {
object_from_pairs(value.0)
}
}

#[macro_export]
macro_rules! eargs {
($($key:expr => $value:expr,)+) => { $crate::eargs!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
edgedb_protocol::query_arg::macros::EdgedbArgsIndexMap(indexmap::indexmap! {
$($key => edgedb_protocol::query_arg::ValueWithCardinality::from($value)),*
})
{
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
let mut map = std::collections::HashMap::with_capacity(CAP);
$(
map.insert($key, $crate::query_arg::ValueWithCardinality::from($value));
)*
$crate::query_arg::macros::EdgedbArgsIndexMap(map)
}
};
}
}

0 comments on commit cb5ad3e

Please sign in to comment.