feat: add trait ToValue; implement it for base types
This commit is contained in:
10
01.workspace/heave/src/imp/bool_to_value.rs
Normal file
10
01.workspace/heave/src/imp/bool_to_value.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::*;
|
||||
|
||||
impl ToValue for bool {
|
||||
fn to_value(self) -> Value {
|
||||
Value::Bool(self)
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests { use super::*; }
|
||||
10
01.workspace/heave/src/imp/f64_to_value.rs
Normal file
10
01.workspace/heave/src/imp/f64_to_value.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::*;
|
||||
|
||||
impl ToValue for f64 {
|
||||
fn to_value(self) -> Value {
|
||||
Value::Real(self)
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests { use super::*; }
|
||||
10
01.workspace/heave/src/imp/i64_to_value.rs
Normal file
10
01.workspace/heave/src/imp/i64_to_value.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::*;
|
||||
|
||||
impl ToValue for i64 {
|
||||
fn to_value(self) -> Value {
|
||||
Value::SignedInt(self)
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests { use super::*; }
|
||||
@@ -1 +1,5 @@
|
||||
|
||||
pub mod bool_to_value;
|
||||
pub mod f64_to_value;
|
||||
pub mod i64_to_value;
|
||||
pub mod string_to_value;
|
||||
pub mod u64_to_value;
|
||||
|
||||
10
01.workspace/heave/src/imp/string_to_value.rs
Normal file
10
01.workspace/heave/src/imp/string_to_value.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::*;
|
||||
|
||||
impl ToValue for String {
|
||||
fn to_value(self) -> Value {
|
||||
Value::Text(self)
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests { use super::*; }
|
||||
10
01.workspace/heave/src/imp/u64_to_value.rs
Normal file
10
01.workspace/heave/src/imp/u64_to_value.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::*;
|
||||
|
||||
impl ToValue for u64 {
|
||||
fn to_value(self) -> Value {
|
||||
Value::UnsignedInt(self)
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests { use super::*; }
|
||||
@@ -12,3 +12,4 @@ pub use crate::str::entity::O as Entity;
|
||||
pub use crate::str::value::E as Value;
|
||||
pub use crate::trt::from_eav::T as FromEAV;
|
||||
pub use crate::trt::to_eav::T as ToEAV;
|
||||
pub use crate::trt::to_value::T as ToValue;
|
||||
|
||||
@@ -7,10 +7,10 @@ pub struct O {
|
||||
}
|
||||
|
||||
impl O {
|
||||
pub fn new(id: &str, value: Value) -> Self {
|
||||
pub fn new(id: &str, value: impl ToValue) -> Self {
|
||||
Self {
|
||||
id: String::from(id),
|
||||
value,
|
||||
value: value.to_value(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ impl O {
|
||||
attributes: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn with_attribute(mut self, id: &str, value: Value) -> Self {
|
||||
pub fn with_attribute(mut self, id: &str, value: impl ToValue) -> Self {
|
||||
let attribute = Attribute::new(id, value);
|
||||
self.attributes.insert(attribute.id.clone(), attribute);
|
||||
self
|
||||
@@ -25,7 +25,7 @@ impl O {
|
||||
Some(attribute) => Some(&attribute.value),
|
||||
}
|
||||
}
|
||||
pub fn set(&mut self, id: &str, value: Value) -> Option<Value> {
|
||||
pub fn set(&mut self, id: &str, value: impl ToValue) -> Option<Value> {
|
||||
let attribute = Attribute::new(id, value);
|
||||
let previous_attribute = self.attributes.insert(attribute.id.clone(), attribute);
|
||||
match previous_attribute {
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod from_eav;
|
||||
pub mod to_eav;
|
||||
pub mod to_value;
|
||||
|
||||
8
01.workspace/heave/src/trt/to_value.rs
Normal file
8
01.workspace/heave/src/trt/to_value.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use crate::*;
|
||||
|
||||
pub trait T {
|
||||
fn to_value(self) -> Value;
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod unit_tests {}
|
||||
Reference in New Issue
Block a user