Skip to content

Commit

Permalink
feat(convert): Into/TryFrom BoltType for Vec
Browse files Browse the repository at this point in the history
Co-authored-By: Peikos <Peikos@github.com>

https://github.com/yehohanan7/neo4rs/pull/11
  • Loading branch information
kkharji committed Mar 29, 2022
1 parent 26d0d54 commit 3d0725a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ use crate::row::*;
use crate::types::*;
use std::convert::{TryFrom, TryInto};

impl<A: TryFrom<BoltType>> TryFrom<BoltType> for Vec<A> {
type Error = Error;

fn try_from(input: BoltType) -> Result<Vec<A>> {
match input {
BoltType::List(l) => Ok(l
.value
.to_vec()
.iter()
.flat_map(|x| A::try_from(x.clone()))
.collect()),
_ => Err(Error::ConverstionError),
}
}
}

impl TryFrom<BoltType> for f64 {
type Error = Error;

Expand Down Expand Up @@ -259,6 +275,14 @@ impl Into<BoltType> for (chrono::NaiveDateTime, &str) {
}
}

impl<A: Into<BoltType> + Clone> Into<BoltType> for Vec<A> {
fn into(self) -> BoltType {
BoltType::List(BoltList {
value: self.iter().map(|v| v.clone().into()).collect(),
})
}
}

impl Into<BoltType> for Vec<u8> {
fn into(self) -> BoltType {
BoltType::Bytes(BoltBytes::new(self.into()))
Expand All @@ -271,6 +295,12 @@ impl Into<BoltType> for i64 {
}
}

impl Into<BoltType> for f64 {
fn into(self) -> BoltType {
BoltType::Float(BoltFloat::new(self))
}
}

impl Into<BoltType> for String {
fn into(self) -> BoltType {
BoltType::String(self.into())
Expand Down

0 comments on commit 3d0725a

Please sign in to comment.