Skip to content

Commit

Permalink
Support Property/VertexProperty in TryFrom<GValue> (#185)
Browse files Browse the repository at this point in the history
* support Property/VertexProperty in TryFrom<GValue>

* fix error strings for various conversion implementations
  • Loading branch information
OmriSteiner committed Apr 6, 2023
1 parent 543ba55 commit 0302042
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions gremlin-client/src/structure/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ impl std::convert::TryFrom<GValue> for String {
match value {
GValue::String(s) => Ok(s),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to String",
value
Expand All @@ -311,6 +313,8 @@ impl std::convert::TryFrom<GValue> for i32 {
match value {
GValue::Int32(s) => Ok(s),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to i32",
value
Expand All @@ -326,8 +330,10 @@ impl std::convert::TryFrom<GValue> for i64 {
match value {
GValue::Int64(s) => Ok(s),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to i32",
"Cannot cast {:?} to i64",
value
))),
}
Expand All @@ -341,6 +347,8 @@ impl std::convert::TryFrom<GValue> for uuid::Uuid {
match value {
GValue::Uuid(uid) => Ok(uid),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to Uuid",
value
Expand All @@ -356,6 +364,8 @@ impl std::convert::TryFrom<GValue> for Date {
match value {
GValue::Date(date) => Ok(date),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to DateTime<Utc>",
value
Expand All @@ -371,6 +381,8 @@ impl std::convert::TryFrom<GValue> for bool {
match value {
GValue::Bool(val) => Ok(val),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to bool",
value
Expand All @@ -386,6 +398,8 @@ impl std::convert::TryFrom<GValue> for f32 {
match value {
GValue::Float(x) => Ok(x),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to f32",
value
Expand All @@ -401,6 +415,8 @@ impl std::convert::TryFrom<GValue> for f64 {
match value {
GValue::Double(x) => Ok(x),
GValue::List(s) => from_list(s),
GValue::VertexProperty(vp) => vp.take(),
GValue::Property(p) => p.take(),
_ => Err(GremlinError::Cast(format!(
"Cannot cast {:?} to f64",
value
Expand All @@ -417,7 +433,7 @@ impl std::convert::TryFrom<GValue> for BTreeMap<String, GValue> {
m.try_into()
} else {
Err(GremlinError::Cast(format!(
"Cannot cast {:?} to HashMap<GKey, GValue>",
"Cannot cast {:?} to BTreeMap<String, GValue>",
value
)))
}
Expand Down Expand Up @@ -447,7 +463,7 @@ impl std::convert::TryFrom<GValue> for HashMap<String, GValue> {
m.try_into()
} else {
Err(GremlinError::Cast(format!(
"Cannot cast {:?} to HashMap<GKey, GValue>",
"Cannot cast {:?} to HashMap<String, GValue>",
value
)))
}
Expand All @@ -462,8 +478,9 @@ where

match vec.len() {
1 => vec.pop().unwrap().try_into(),
_ => Err(GremlinError::Cast(String::from(
"Cannot cast a List to String",
_ => Err(GremlinError::Cast(format!(
"Cannot cast a List to {}",
std::any::type_name::<T>(),
))),
}
}
Expand Down

0 comments on commit 0302042

Please sign in to comment.