feat: add trait ToValue; implement it for base types

This commit is contained in:
2025-09-27 08:26:18 +02:00
parent 01568e87c0
commit f32d828b2a
11 changed files with 69 additions and 5 deletions

View 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::*; }

View 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::*; }

View 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::*; }

View File

@@ -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;

View 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::*; }

View 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::*; }

View File

@@ -12,3 +12,4 @@ pub use crate::str::entity::O as Entity;
pub use crate::str::value::E as Value; pub use crate::str::value::E as Value;
pub use crate::trt::from_eav::T as FromEAV; pub use crate::trt::from_eav::T as FromEAV;
pub use crate::trt::to_eav::T as ToEAV; pub use crate::trt::to_eav::T as ToEAV;
pub use crate::trt::to_value::T as ToValue;

View File

@@ -7,10 +7,10 @@ pub struct O {
} }
impl O { impl O {
pub fn new(id: &str, value: Value) -> Self { pub fn new(id: &str, value: impl ToValue) -> Self {
Self { Self {
id: String::from(id), id: String::from(id),
value, value: value.to_value(),
} }
} }
} }

View File

@@ -13,7 +13,7 @@ impl O {
attributes: std::collections::HashMap::new(), 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); let attribute = Attribute::new(id, value);
self.attributes.insert(attribute.id.clone(), attribute); self.attributes.insert(attribute.id.clone(), attribute);
self self
@@ -25,7 +25,7 @@ impl O {
Some(attribute) => Some(&attribute.value), 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 attribute = Attribute::new(id, value);
let previous_attribute = self.attributes.insert(attribute.id.clone(), attribute); let previous_attribute = self.attributes.insert(attribute.id.clone(), attribute);
match previous_attribute { match previous_attribute {

View File

@@ -1,2 +1,3 @@
pub mod from_eav; pub mod from_eav;
pub mod to_eav; pub mod to_eav;
pub mod to_value;

View File

@@ -0,0 +1,8 @@
use crate::*;
pub trait T {
fn to_value(self) -> Value;
}
// #[cfg(test)]
// mod unit_tests {}