From b92536b649f965184730d7f27a1c471bec70880b Mon Sep 17 00:00:00 2001 From: Aman Sharma <35651798+lunchspider@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:31:09 +0530 Subject: [PATCH] Add some From and TryFrom implementations (#1401) --- lib/src/err/mod.rs | 4 +++ lib/src/sql/datetime.rs | 6 ++++ lib/src/sql/duration.rs | 6 ++++ lib/src/sql/number.rs | 31 +++++++++++++++++ lib/src/sql/strand.rs | 6 ++++ lib/src/sql/value/value.rs | 71 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 124 insertions(+) diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 621868d0..0117bff3 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -41,6 +41,10 @@ pub enum Error { #[error("The key being inserted already exists")] TxKeyAlreadyExists, + /// It's is not possible to convert between the two types + #[error("Cannot convert from '{0}' to '{1}'")] + TryFromError(String, &'static str), + /// No namespace has been selected #[error("Specify a namespace to use")] NsEmpty, diff --git a/lib/src/sql/datetime.rs b/lib/src/sql/datetime.rs index 1d2c8773..21d73622 100644 --- a/lib/src/sql/datetime.rs +++ b/lib/src/sql/datetime.rs @@ -51,6 +51,12 @@ impl Deref for Datetime { } } +impl From for DateTime { + fn from(x: Datetime) -> Self { + x.0 + } +} + impl Datetime { /// Convert the Datetime to a raw String pub fn to_raw(&self) -> String { diff --git a/lib/src/sql/duration.rs b/lib/src/sql/duration.rs index ae4a90b2..640ba7ac 100644 --- a/lib/src/sql/duration.rs +++ b/lib/src/sql/duration.rs @@ -44,6 +44,12 @@ impl From<&str> for Duration { } } +impl From for time::Duration { + fn from(s: Duration) -> Self { + s.0 + } +} + impl Deref for Duration { type Target = time::Duration; fn deref(&self) -> &Self::Target { diff --git a/lib/src/sql/number.rs b/lib/src/sql/number.rs index 5af6c8ec..1929495a 100644 --- a/lib/src/sql/number.rs +++ b/lib/src/sql/number.rs @@ -1,3 +1,4 @@ +use crate::err::Error; use crate::sql::ending::number as ending; use crate::sql::error::IResult; use crate::sql::serde::is_internal_serialization; @@ -119,6 +120,36 @@ impl From for Number { } } +impl TryFrom for i64 { + type Error = Error; + fn try_from(value: Number) -> Result { + match value { + Number::Int(x) => Ok(x), + _ => Err(Error::TryFromError(value.to_string(), "i64")), + } + } +} + +impl TryFrom for f64 { + type Error = Error; + fn try_from(value: Number) -> Result { + match value { + Number::Float(x) => Ok(x), + _ => Err(Error::TryFromError(value.to_string(), "f64")), + } + } +} + +impl TryFrom for BigDecimal { + type Error = Error; + fn try_from(value: Number) -> Result { + match value { + Number::Decimal(x) => Ok(x), + _ => Err(Error::TryFromError(value.to_string(), "BigDecimal")), + } + } +} + impl Display for Number { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { diff --git a/lib/src/sql/strand.rs b/lib/src/sql/strand.rs index 70810e96..e0683bb1 100644 --- a/lib/src/sql/strand.rs +++ b/lib/src/sql/strand.rs @@ -43,6 +43,12 @@ impl Deref for Strand { } } +impl From for String { + fn from(s: Strand) -> Self { + s.0 + } +} + impl Strand { /// Get the underlying String slice pub fn as_str(&self) -> &str { diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 2590241e..88d3805b 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -464,6 +464,77 @@ impl From for Value { } } +impl TryFrom for i64 { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Number(x) => x.try_into(), + _ => Err(Error::TryFromError(value.to_string(), "i64")), + } + } +} + +impl TryFrom for f64 { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Number(x) => x.try_into(), + _ => Err(Error::TryFromError(value.to_string(), "f64")), + } + } +} + +impl TryFrom for BigDecimal { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Number(x) => x.try_into(), + _ => Err(Error::TryFromError(value.to_string(), "BigDecimal")), + } + } +} + +impl TryFrom for String { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Strand(x) => Ok(x.into()), + _ => Err(Error::TryFromError(value.to_string(), "String")), + } + } +} + +impl TryFrom for bool { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::True => Ok(true), + Value::False => Ok(false), + _ => Err(Error::TryFromError(value.to_string(), "bool")), + } + } +} + +impl TryFrom for std::time::Duration { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Duration(x) => Ok(x.into()), + _ => Err(Error::TryFromError(value.to_string(), "time::Duration")), + } + } +} + +impl TryFrom for DateTime { + type Error = Error; + fn try_from(value: Value) -> Result { + match value { + Value::Datetime(x) => Ok(x.into()), + _ => Err(Error::TryFromError(value.to_string(), "chrono::DateTime")), + } + } +} + impl Value { // ----------------------------------- // Initial record value