1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::core::OError;
#[cfg(feature = "python")]
use pyo3::IntoPyObject;
#[cfg(feature = "python")]
use pyo3::IntoPyObjectRef;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::HashMap;
/// The data type and value that can be stored in an individual or algorithm..
#[derive(Clone, Deserialize, Debug)]
#[serde(untagged)]
#[cfg_attr(feature = "python", derive(IntoPyObjectRef, IntoPyObject))]
pub enum DataValue {
/// The value for a floating-point number. This is a f64.
Real(f64),
/// The value for an integer number. This is an i64.
Integer(i64),
/// The value for an usize.
USize(usize),
/// The value for a vector of floating-point numbers.
Vector(Vec<f64>),
/// The value for a vector of nested data.
DataVector(Vec<DataValue>),
/// The value for a Hashmap
Map(HashMap<String, DataValue>),
}
impl Serialize for DataValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
DataValue::Real(v) => serializer.serialize_f64(*v),
DataValue::Integer(v) => serializer.serialize_i64(*v),
DataValue::USize(v) => serializer.serialize_u64(*v as u64),
DataValue::Vector(v) => serializer.collect_seq(v),
DataValue::DataVector(v) => serializer.collect_seq(v),
DataValue::Map(v) => serializer.collect_map(v),
}
}
}
impl PartialEq for DataValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(DataValue::Real(s), DataValue::Real(o)) => (s.is_nan() && o.is_nan()) || (*s == *o),
(DataValue::Integer(s), DataValue::Integer(o)) => *s == *o,
(DataValue::USize(s), DataValue::USize(o)) => s == o,
(DataValue::Vector(s), DataValue::Vector(o)) => s == o,
_ => false,
}
}
}
impl DataValue {
/// Get the value if the data is of real type. This returns an error if the data is not real.
///
/// returns: `Result<f64, OError>`
pub fn as_real(&self) -> Result<f64, OError> {
if let DataValue::Real(v) = self {
Ok(*v)
} else {
Err(OError::WrongDataType("real".to_string()))
}
}
/// Get the value if the data is of integer type. This returns an error if the data is not an
/// integer.
///
/// returns: `Result<f64, OError>`
pub fn as_integer(&self) -> Result<i64, OError> {
if let DataValue::Integer(v) = self {
Ok(*v)
} else {
Err(OError::WrongDataType("integer".to_string()))
}
}
/// Get the value if the data is of vector of f64. This returns an error if the data is not a
/// vector.
///
/// returns: `Result<&Vec<f64, OError>`
pub fn as_f64_vec(&self) -> Result<&Vec<f64>, OError> {
if let DataValue::Vector(v) = self {
Ok(v)
} else {
Err(OError::WrongDataType("vector of f64".to_string()))
}
}
/// Get the value if the data is of vector of data. This returns an error if the data is not a
/// data vector.
///
/// returns: `Result<&Vec<DataValue>, OError>`
pub fn as_data_vec(&self) -> Result<&Vec<DataValue>, OError> {
if let DataValue::DataVector(v) = self {
Ok(v)
} else {
Err(OError::WrongDataType("vector of data".to_string()))
}
}
/// Get the value if the data is a mao. This returns an error if the data is not a map.
///
/// returns: `Result<HashMap<String, DataValue>, OError>`
pub fn as_map(&self) -> Result<&HashMap<String, DataValue>, OError> {
if let DataValue::Map(v) = self {
Ok(v)
} else {
Err(OError::WrongDataType("map".to_string()))
}
}
/// Get the value if the data is of usize type. This returns an error if the data is not an
/// usize.
///
/// returns: `Result<f64, OError>`
pub fn as_usize(&self) -> Result<usize, OError> {
if let DataValue::USize(v) = self {
Ok(*v)
} else {
Err(OError::WrongDataType("usize".to_string()))
}
}
}