From eb6f397a3b63e212f0fda83071337aa6e63d5a39 Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Fri, 9 Jun 2023 14:23:32 -0400 Subject: [PATCH] Added some missing conversion for `ToDatabaseValue` trait. --- CHANGELOG.md | 4 ++++ src/tables.rs | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2420d16..5c7df58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.2] + +* Added some missing conversion for `ToDatabaseValue` trait. + ## [1.1.1] * Fixed encoding of `Vec` to be in `hex` format as expected by Postgres (and hopefully other database). This is technically a breaking change but it was probably never actually working, so we are changing. If you were relying on `base64` decoding, please let use know and we will find a solution. diff --git a/src/tables.rs b/src/tables.rs index 2f8bf1c..793a6d9 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -197,30 +197,17 @@ impl_to_database_value_proxy_to_string!(u8); impl_to_database_value_proxy_to_string!(u16); impl_to_database_value_proxy_to_string!(u32); impl_to_database_value_proxy_to_string!(u64); +impl_to_database_value_proxy_to_string!(bool); +impl_to_database_value_proxy_to_string!(::prost_types::Timestamp); +impl_to_database_value_proxy_to_string!(&::prost_types::Timestamp); +impl_to_database_value_proxy_to_string!(&str); +impl_to_database_value_proxy_to_string!(BigDecimal); +impl_to_database_value_proxy_to_string!(&BigDecimal); +impl_to_database_value_proxy_to_string!(BigInt); +impl_to_database_value_proxy_to_string!(&BigInt); -impl_to_database_value_proxy_to_ref!(bool); -impl_to_database_value_proxy_to_ref!(BigDecimal); -impl_to_database_value_proxy_to_ref!(BigInt); impl_to_database_value_proxy_to_ref!(Vec); -impl ToDatabaseValue for &bool { - fn to_value(self) -> String { - (if *self == true { "true" } else { "false" }).to_string() - } -} - -impl ToDatabaseValue for &BigDecimal { - fn to_value(self) -> String { - ToString::to_string(self) - } -} - -impl ToDatabaseValue for &BigInt { - fn to_value(self) -> String { - ToString::to_string(self) - } -} - impl ToDatabaseValue for &String { fn to_value(self) -> String { self.clone() @@ -250,3 +237,19 @@ impl> ToDatabaseValue for &Hex { ToString::to_string(self) } } + +#[cfg(test)] +mod test { + use crate::tables::ToDatabaseValue; + + #[test] + fn to_database_value_proto_timestamp() { + assert_eq!( + ToDatabaseValue::to_value(::prost_types::Timestamp { + seconds: 60 * 60 + 60 + 1, + nanos: 1 + }), + "1970-01-01T01:01:01.000000001Z" + ); + } +}