From 1819b1467da2ddd6a32ee741089d62f360d5bf4a Mon Sep 17 00:00:00 2001 From: Luka Peschke Date: Wed, 31 Jan 2024 10:47:01 +0100 Subject: [PATCH 1/2] feat: support bools in DataType.as_f64 and DataType.as_i64 variants Signed-off-by: Luka Peschke --- Changelog.md | 2 ++ src/datatype.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Changelog.md b/Changelog.md index 7e42b5ef..2205c790 100644 --- a/Changelog.md +++ b/Changelog.md @@ -12,6 +12,8 @@ - refactor: rename `DataType` enum to `Data` and `DataTypeRef` to `DataRef` - feat: introduce a `DataType` trait implemented by both `Data` and `DataRef`. +- feat: `Data` and `DataType` now return `Some(0{.0})` and `Some(1{.0})` rather than `None` when `.as_i64` or `.as_f64` + is used on a Bool value ## 0.23.1 diff --git a/src/datatype.rs b/src/datatype.rs index e7ac200c..b30bcf97 100644 --- a/src/datatype.rs +++ b/src/datatype.rs @@ -142,6 +142,7 @@ impl DataType for Data { match self { Data::Int(v) => Some(*v), Data::Float(v) => Some(*v as i64), + Data::Bool(v) => Some(*v as i64), Data::String(v) => v.parse::().ok(), _ => None, } @@ -151,6 +152,7 @@ impl DataType for Data { match self { Data::Int(v) => Some(*v as f64), Data::Float(v) => Some(*v), + Data::Bool(v) => Some((*v).into()), Data::String(v) => v.parse::().ok(), _ => None, } @@ -450,6 +452,7 @@ impl DataType for DataRef<'_> { match self { DataRef::Int(v) => Some(*v), DataRef::Float(v) => Some(*v as i64), + DataRef::Bool(v) => Some(*v as i64), DataRef::String(v) => v.parse::().ok(), DataRef::SharedString(v) => v.parse::().ok(), _ => None, @@ -460,6 +463,7 @@ impl DataType for DataRef<'_> { match self { DataRef::Int(v) => Some(*v as f64), DataRef::Float(v) => Some(*v), + DataRef::Bool(v) => Some((*v).into()), DataRef::String(v) => v.parse::().ok(), DataRef::SharedString(v) => v.parse::().ok(), _ => None, @@ -803,4 +807,20 @@ mod tests { assert_eq!(Data::Bool(true), true); assert_eq!(Data::Int(100), 100i64); } + + #[test] + fn test_as_i64_with_bools() { + assert_eq!(Data::Bool(true).as_i64(), Some(1)); + assert_eq!(Data::Bool(false).as_i64(), Some(0)); + assert_eq!(DataRef::Bool(true).as_i64(), Some(1)); + assert_eq!(DataRef::Bool(false).as_i64(), Some(0)); + } + + #[test] + fn test_as_f64_with_bools() { + assert_eq!(Data::Bool(true).as_f64(), Some(1.0)); + assert_eq!(Data::Bool(false).as_f64(), Some(0.0)); + assert_eq!(DataRef::Bool(true).as_f64(), Some(1.0)); + assert_eq!(DataRef::Bool(false).as_f64(), Some(0.0)); + } } From 440b6a8847d677d82bb79d5606face1168525b74 Mon Sep 17 00:00:00 2001 From: Luka Peschke Date: Wed, 31 Jan 2024 11:01:41 +0100 Subject: [PATCH 2/2] fix: From for f64 has been added in 1.68.0 Signed-off-by: Luka Peschke --- src/datatype.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/datatype.rs b/src/datatype.rs index b30bcf97..ab8764e4 100644 --- a/src/datatype.rs +++ b/src/datatype.rs @@ -152,7 +152,7 @@ impl DataType for Data { match self { Data::Int(v) => Some(*v as f64), Data::Float(v) => Some(*v), - Data::Bool(v) => Some((*v).into()), + Data::Bool(v) => Some((*v as i32).into()), Data::String(v) => v.parse::().ok(), _ => None, } @@ -463,7 +463,7 @@ impl DataType for DataRef<'_> { match self { DataRef::Int(v) => Some(*v as f64), DataRef::Float(v) => Some(*v), - DataRef::Bool(v) => Some((*v).into()), + DataRef::Bool(v) => Some((*v as i32).into()), DataRef::String(v) => v.parse::().ok(), DataRef::SharedString(v) => v.parse::().ok(), _ => None,