Skip to content

Commit

Permalink
feat: Add QueryBuilder::try_bind (#25)
Browse files Browse the repository at this point in the history
This provides a more convenient way to bind values without
having to serialize manually.
  • Loading branch information
theduke committed Jul 26, 2020
1 parent b3e3332 commit bbe2941
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/aql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,49 @@ impl<'a, __query, __count, __batch_size, __cache, __memory_limit, __ttl, __optio
_phantom: self._phantom,
}
}

pub fn try_bind<K, V>(
self,
key: K,
value: V,
) -> Result<
AqlQueryBuilder<
'a,
(
__query,
(HashMap<&'a str, Value>,),
__count,
__batch_size,
__cache,
__memory_limit,
__ttl,
__options,
),
>,
serde_json::Error,
>
where
K: Into<&'a str>,
V: serde::Serialize,
{
let mut bind_vars = HashMap::new();
bind_vars.insert(key.into(), serde_json::to_value(value)?);
let (query, _, count, batch_size, cache, memory_limit, ttl, options) = self.fields;
let b = AqlQueryBuilder {
fields: (
query,
(bind_vars,),
count,
batch_size,
cache,
memory_limit,
ttl,
options,
),
_phantom: self._phantom,
};
Ok(b)
}
}

#[allow(non_camel_case_types, missing_docs)]
Expand Down Expand Up @@ -179,6 +222,35 @@ impl<'a, __query, __count, __batch_size, __cache, __memory_limit, __ttl, __optio
(self.fields.1).0.insert(key.into(), value.into());
self
}

pub fn try_bind<K, V>(
mut self,
key: K,
value: V,
) -> Result<
AqlQueryBuilder<
'a,
(
__query,
(HashMap<&'a str, Value>,),
__count,
__batch_size,
__cache,
__memory_limit,
__ttl,
__options,
),
>,
serde_json::Error,
>
where
K: Into<&'a str>,
V: serde::Serialize,
{
let serialized_value = serde_json::to_value(value)?;
(self.fields.1).0.insert(key.into(), serialized_value);
Ok(self)
}
}

#[derive(Debug, Serialize, TypedBuilder)]
Expand Down

0 comments on commit bbe2941

Please sign in to comment.