From 82c9d7da2bd439c08900695a561d5501a1aeca41 Mon Sep 17 00:00:00 2001 From: Aman Sharma <35651798+lunchspider@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:05:01 +0530 Subject: [PATCH] Add additional From and TryFrom implementations (#1410) --- lib/src/sql/array.rs | 6 ++++++ lib/src/sql/uuid.rs | 6 ++++++ lib/src/sql/value/value.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/src/sql/array.rs b/lib/src/sql/array.rs index d848f2b8..073c7dd3 100644 --- a/lib/src/sql/array.rs +++ b/lib/src/sql/array.rs @@ -60,6 +60,12 @@ impl From> for Array { } } +impl From for Vec { + fn from(s: Array) -> Self { + s.0 + } +} + impl Deref for Array { type Target = Vec; fn deref(&self) -> &Self::Target { diff --git a/lib/src/sql/uuid.rs b/lib/src/sql/uuid.rs index 0c8cb694..9d4e0b82 100644 --- a/lib/src/sql/uuid.rs +++ b/lib/src/sql/uuid.rs @@ -34,6 +34,12 @@ impl From for Uuid { } } +impl From for uuid::Uuid { + fn from(s: Uuid) -> Self { + s.0 + } +} + impl Deref for Uuid { type Target = uuid::Uuid; fn deref(&self) -> &Self::Target { diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index d0f2681e..c2013b0b 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -535,6 +535,36 @@ impl TryFrom for DateTime { } } +impl TryFrom for uuid::Uuid { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Uuid(x) => Ok(x.into()), + _ => Err(Error::TryFromError(value.to_string(), "uuid::Uuid")), + } + } +} + +impl TryFrom for Vec { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Array(x) => Ok(x.into()), + _ => Err(Error::TryFromError(value.to_string(), "Vec")), + } + } +} + +impl TryFrom for Object { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Object(x) => Ok(x), + _ => Err(Error::TryFromError(value.to_string(), "Object")), + } + } +} + impl Value { // ----------------------------------- // Initial record value