From ca37ea25691165a84a0b9079c5d5a7c22839da21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kubrak?= Date: Tue, 17 Aug 2021 21:15:03 +0200 Subject: [PATCH] Add as_type_or_null to ValueRef --- src/types/value_ref.rs | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index 20a9f63..a7cd416 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -45,6 +45,19 @@ impl<'a> ValueRef<'a> { } } + /// If `self` is case `Null` returns None. + /// If `self` is case `Integer`, returns the integral value. + /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error:: + /// InvalidColumnType). + #[inline] + pub fn as_i64_or_null(&self) -> FromSqlResult> { + match *self { + ValueRef::Null => Ok(None), + ValueRef::Integer(i) => Ok(Some(i)), + _ => Err(FromSqlError::InvalidType), + } + } + /// If `self` is case `Real`, returns the floating point value. Otherwise, /// returns [`Err(Error::InvalidColumnType)`](crate::Error:: /// InvalidColumnType). @@ -56,6 +69,19 @@ impl<'a> ValueRef<'a> { } } + /// If `self` is case `Null` returns None. + /// If `self` is case `Real`, returns the floating point value. + /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error:: + /// InvalidColumnType). + #[inline] + pub fn as_f64_or_null(&self) -> FromSqlResult> { + match *self { + ValueRef::Null => Ok(None), + ValueRef::Real(f) => Ok(Some(f)), + _ => Err(FromSqlError::InvalidType), + } + } + /// If `self` is case `Text`, returns the string value. Otherwise, returns /// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType). #[inline] @@ -68,6 +94,21 @@ impl<'a> ValueRef<'a> { } } + /// If `self` is case `Null` returns None. + /// If `self` is case `Text`, returns the string value. + /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error:: + /// InvalidColumnType). + #[inline] + pub fn as_str_or_null(&self) -> FromSqlResult> { + match *self { + ValueRef::Null => Ok(None), + ValueRef::Text(t) => std::str::from_utf8(t) + .map_err(|e| FromSqlError::Other(Box::new(e))) + .map(Some), + _ => Err(FromSqlError::InvalidType), + } + } + /// If `self` is case `Blob`, returns the byte slice. Otherwise, returns /// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType). #[inline] @@ -78,6 +119,19 @@ impl<'a> ValueRef<'a> { } } + /// If `self` is case `Null` returns None. + /// If `self` is case `Blob`, returns the byte slice. + /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error:: + /// InvalidColumnType). + #[inline] + pub fn as_blob_or_null(&self) -> FromSqlResult> { + match *self { + ValueRef::Null => Ok(None), + ValueRef::Blob(b) => Ok(Some(b)), + _ => Err(FromSqlError::InvalidType), + } + } + /// Returns the byte slice that makes up this ValueRef if it's either /// [`ValueRef::Blob`] or [`ValueRef::Text`]. #[inline] @@ -87,6 +141,18 @@ impl<'a> ValueRef<'a> { _ => Err(FromSqlError::InvalidType), } } + + /// If `self` is case `Null` returns None. + /// If `self` is [`ValueRef::Blob`] or [`ValueRef::Text`] returns the byte + /// slice that makes up this value + #[inline] + pub fn as_bytes_or_null(&self) -> FromSqlResult> { + match *self { + ValueRef::Null => Ok(None), + ValueRef::Text(s) | ValueRef::Blob(s) => Ok(Some(s)), + _ => Err(FromSqlError::InvalidType), + } + } } impl From> for Value {