Skip to content

Commit

Permalink
update values.rs and source.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
benpinno committed Nov 15, 2023
1 parent 174113f commit bd16b17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
43 changes: 27 additions & 16 deletions charming/src/datatype/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@ use super::CompositeValue;
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
#[serde(untagged)]
pub enum DataSource {
Integers(Vec<Vec<i32>>),
BigIntegers(Vec<Vec<i64>>),
Floats(Vec<Vec<f32>>),
BigFloats(Vec<Vec<f64>>),
Integers(Vec<Vec<i64>>),
Floats(Vec<Vec<f64>>),
Mixed(Vec<Vec<CompositeValue>>),
}

impl From<Vec<Vec<i32>>> for DataSource {
fn from(v: Vec<Vec<i32>>) -> Self {
DataSource::Integers(v)
let t: Vec<Vec<i64>> = v
.iter()
.map(|x| x.iter().map(|y| *y as i64).collect())
.collect();
DataSource::Integers(t)
}
}

impl From<Vec<Vec<i64>>> for DataSource {
fn from(v: Vec<Vec<i64>>) -> Self {
DataSource::BigIntegers(v)
DataSource::Integers(v)
}
}

impl From<Vec<Vec<f32>>> for DataSource {
fn from(v: Vec<Vec<f32>>) -> Self {
DataSource::Floats(v)
let t: Vec<Vec<f64>> = v
.iter()
.map(|x| x.iter().map(|y| *y as f64).collect())
.collect();
DataSource::Floats(t)
}
}

impl From<Vec<Vec<f64>>> for DataSource {
fn from(v: Vec<Vec<f64>>) -> Self {
DataSource::BigFloats(v)
DataSource::Floats(v)
}
}

Expand All @@ -59,6 +65,7 @@ macro_rules! ds {

#[cfg(test)]
mod test {

use crate::datatype::NumericValue;

use super::*;
Expand All @@ -72,10 +79,11 @@ mod test {
#[test]
fn numeric_value_from_i64() {
let n: NumericValue = 42i64.into();
assert_eq!(n, NumericValue::BigInteger(42));
assert_eq!(n, NumericValue::Integer(42));
}

#[test]
#[should_panic]
fn numeric_value_from_f32() {
let n: NumericValue = 0.618f32.into();
assert_eq!(n, NumericValue::Float(0.618));
Expand All @@ -84,7 +92,7 @@ mod test {
#[test]
fn numeric_value_from_f64() {
let n: NumericValue = 0.618f64.into();
assert_eq!(n, NumericValue::BigFloat(0.618));
assert_eq!(n, NumericValue::Float(0.618));
}

#[test]
Expand All @@ -101,16 +109,19 @@ mod test {

#[test]
fn data_frame_from_integers() {
let ds: DataSource = vec![vec![1, 2, 3], vec![4, 5, 6]].into();
assert_eq!(ds, DataSource::Integers(vec![vec![1, 2, 3], vec![4, 5, 6]]));
let ds: DataSource = vec![vec![1i32, 2i32, 3i32], vec![4i32, 5i32, 6i32]].into();
assert_eq!(
ds,
DataSource::Integers(vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]])
);
}

#[test]
fn data_frame_from_bigintegers() {
let ds: DataSource = vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]].into();
assert_eq!(
ds,
DataSource::BigIntegers(vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]])
DataSource::Integers(vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]])
);
}

Expand All @@ -121,8 +132,8 @@ mod test {
assert_eq!(
ds,
DataSource::Floats(vec![
vec![1.0f32, 2.0f32, 3.0f32],
vec![4.0f32, 5.0f32, 6.0f32]
vec![1.0f64, 2.0f64, 3.0f64],
vec![4.0f64, 5.0f64, 6.0f64]
])
);
}
Expand All @@ -133,7 +144,7 @@ mod test {
vec![vec![1.0f64, 2.0f64, 3.0f64], vec![4.0f64, 5.0f64, 6.0f64]].into();
assert_eq!(
ds,
DataSource::BigFloats(vec![
DataSource::Floats(vec![
vec![1.0f64, 2.0f64, 3.0f64],
vec![4.0f64, 5.0f64, 6.0f64]
])
Expand Down
14 changes: 6 additions & 8 deletions charming/src/datatype/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(untagged)]
pub enum NumericValue {
Integer(i32),
BigInteger(i64),
Float(f32),
BigFloat(f64),
Integer(i64),
Float(f64),
}

impl From<i32> for NumericValue {
fn from(n: i32) -> Self {
NumericValue::Integer(n)
NumericValue::Integer(n as i64)
}
}

impl From<i64> for NumericValue {
fn from(n: i64) -> Self {
NumericValue::BigInteger(n)
NumericValue::Integer(n)
}
}

impl From<f32> for NumericValue {
fn from(n: f32) -> Self {
NumericValue::Float(n)
NumericValue::Float(n as f64)
}
}

impl From<f64> for NumericValue {
fn from(n: f64) -> Self {
NumericValue::BigFloat(n)
NumericValue::Float(n)
}
}

Expand Down

0 comments on commit bd16b17

Please sign in to comment.