Add some From<Value> and TryFrom<Value> implementations (#1401)

This commit is contained in:
Aman Sharma 2022-10-26 19:31:09 +05:30 committed by GitHub
parent e9364d2efd
commit b92536b649
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 0 deletions

View file

@ -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,

View file

@ -51,6 +51,12 @@ impl Deref for Datetime {
}
}
impl From<Datetime> for DateTime<Utc> {
fn from(x: Datetime) -> Self {
x.0
}
}
impl Datetime {
/// Convert the Datetime to a raw String
pub fn to_raw(&self) -> String {

View file

@ -44,6 +44,12 @@ impl From<&str> for Duration {
}
}
impl From<Duration> for time::Duration {
fn from(s: Duration) -> Self {
s.0
}
}
impl Deref for Duration {
type Target = time::Duration;
fn deref(&self) -> &Self::Target {

View file

@ -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<BigDecimal> for Number {
}
}
impl TryFrom<Number> for i64 {
type Error = Error;
fn try_from(value: Number) -> Result<Self, Self::Error> {
match value {
Number::Int(x) => Ok(x),
_ => Err(Error::TryFromError(value.to_string(), "i64")),
}
}
}
impl TryFrom<Number> for f64 {
type Error = Error;
fn try_from(value: Number) -> Result<Self, Self::Error> {
match value {
Number::Float(x) => Ok(x),
_ => Err(Error::TryFromError(value.to_string(), "f64")),
}
}
}
impl TryFrom<Number> for BigDecimal {
type Error = Error;
fn try_from(value: Number) -> Result<Self, Self::Error> {
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 {

View file

@ -43,6 +43,12 @@ impl Deref for Strand {
}
}
impl From<Strand> for String {
fn from(s: Strand) -> Self {
s.0
}
}
impl Strand {
/// Get the underlying String slice
pub fn as_str(&self) -> &str {

View file

@ -464,6 +464,77 @@ impl From<Id> for Value {
}
}
impl TryFrom<Value> for i64 {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Number(x) => x.try_into(),
_ => Err(Error::TryFromError(value.to_string(), "i64")),
}
}
}
impl TryFrom<Value> for f64 {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Number(x) => x.try_into(),
_ => Err(Error::TryFromError(value.to_string(), "f64")),
}
}
}
impl TryFrom<Value> for BigDecimal {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Number(x) => x.try_into(),
_ => Err(Error::TryFromError(value.to_string(), "BigDecimal")),
}
}
}
impl TryFrom<Value> for String {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Strand(x) => Ok(x.into()),
_ => Err(Error::TryFromError(value.to_string(), "String")),
}
}
}
impl TryFrom<Value> for bool {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::True => Ok(true),
Value::False => Ok(false),
_ => Err(Error::TryFromError(value.to_string(), "bool")),
}
}
}
impl TryFrom<Value> for std::time::Duration {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Duration(x) => Ok(x.into()),
_ => Err(Error::TryFromError(value.to_string(), "time::Duration")),
}
}
}
impl TryFrom<Value> for DateTime<Utc> {
type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Datetime(x) => Ok(x.into()),
_ => Err(Error::TryFromError(value.to_string(), "chrono::DateTime<Utc>")),
}
}
}
impl Value {
// -----------------------------------
// Initial record value