diff --git a/01.workspace/heave/src/imp/bool_from_value.rs b/01.workspace/heave/src/imp/bool_from_value.rs new file mode 100644 index 0000000..0257530 --- /dev/null +++ b/01.workspace/heave/src/imp/bool_from_value.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl From for bool { + fn from(value: Value) -> bool { + match value { + Value::Bool(value) => value, + _ => panic!("Type mismatch"), + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/imp/entity_from_value.rs b/01.workspace/heave/src/imp/entity_from_value.rs new file mode 100644 index 0000000..7939b15 --- /dev/null +++ b/01.workspace/heave/src/imp/entity_from_value.rs @@ -0,0 +1,17 @@ +use crate::*; + +impl From for Entity { + fn from(value: Value) -> Entity { + let id = match value { + Value::Text(value) => value, + _ => panic!("Type mismatch"), + }; + Entity { + id, + ..Entity::default() + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/imp/f64_from_value.rs b/01.workspace/heave/src/imp/f64_from_value.rs new file mode 100644 index 0000000..983664c --- /dev/null +++ b/01.workspace/heave/src/imp/f64_from_value.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl From for f64 { + fn from(value: Value) -> f64 { + match value { + Value::Real(value) => value, + _ => panic!("Type mismatch"), + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/imp/i64_from_value.rs b/01.workspace/heave/src/imp/i64_from_value.rs new file mode 100644 index 0000000..1cdb441 --- /dev/null +++ b/01.workspace/heave/src/imp/i64_from_value.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl From for i64 { + fn from(value: Value) -> i64 { + match value { + Value::SignedInt(value) => value, + _ => panic!("Type mismatch"), + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/imp/mod.rs b/01.workspace/heave/src/imp/mod.rs index 431ad43..7796546 100644 --- a/01.workspace/heave/src/imp/mod.rs +++ b/01.workspace/heave/src/imp/mod.rs @@ -1,7 +1,13 @@ +pub mod bool_from_value; pub mod bool_to_value; +pub mod entity_from_value; pub mod entity_to_value; +pub mod f64_from_value; pub mod f64_to_value; +pub mod i64_from_value; pub mod i64_to_value; pub mod str_to_value; +pub mod string_from_value; pub mod string_to_value; +pub mod u64_from_value; pub mod u64_to_value; diff --git a/01.workspace/heave/src/imp/string_from_value.rs b/01.workspace/heave/src/imp/string_from_value.rs new file mode 100644 index 0000000..adf70de --- /dev/null +++ b/01.workspace/heave/src/imp/string_from_value.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl From for String { + fn from(value: Value) -> String { + match value { + Value::Text(value) => value, + _ => panic!("Type mismatch"), + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/imp/u64_from_value.rs b/01.workspace/heave/src/imp/u64_from_value.rs new file mode 100644 index 0000000..486dba7 --- /dev/null +++ b/01.workspace/heave/src/imp/u64_from_value.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl From for u64 { + fn from(value: Value) -> u64 { + match value { + Value::UnsignedInt(value) => value, + _ => panic!("Type mismatch"), + } + } +} + +// #[cfg(test)] +// mod unit_tests { use super::*; } diff --git a/01.workspace/heave/src/str/entity.rs b/01.workspace/heave/src/str/entity.rs index 49ed4fc..0dbe0d5 100644 --- a/01.workspace/heave/src/str/entity.rs +++ b/01.workspace/heave/src/str/entity.rs @@ -42,4 +42,11 @@ impl O { Some(attribute) => Some(attribute.value), } } + pub fn unwrap(&self, id: &str) -> T + where + T: From, + { + let value = self.value_of(id).unwrap(); + T::from(value.clone()) + } } diff --git a/01.workspace/heave/src/tst/intended_use.rs b/01.workspace/heave/src/tst/intended_use.rs index e9b9f97..688db0b 100644 --- a/01.workspace/heave/src/tst/intended_use.rs +++ b/01.workspace/heave/src/tst/intended_use.rs @@ -1,6 +1,26 @@ #[cfg(test)] mod tests { use crate::*; + #[derive(Debug, Clone, PartialEq)] + struct Product { + pub name: String, + pub price: u64, + } + impl ToEAV for Product { + fn to_eav(self) -> Entity { + Entity::new("product") + .with_attribute("name", self.name) + .with_attribute("price", self.price) + } + } + impl FromEAV for Product { + fn from_eav(entity: Entity) -> Product { + Product { + name: entity.unwrap("name"), + price: entity.unwrap("price"), + } + } + } #[test] fn check_001() { // Demonstrates the costruction of a new entity instance @@ -61,4 +81,39 @@ mod tests { Some(&Value::Text(category_id)) ); } + #[test] + fn check_005() { + let tag = Entity::new("tag").with_attribute("label", "new"); + let tag_id = tag.id.clone(); + let entity = Entity::new("product") + .with_attribute("name", "laptop") + .with_attribute("price", 200000u64) + .with_attribute("delta", -50i64) + .with_attribute("in_stock", true) + .with_attribute("discount", 5.2f64) + .with_attribute("tag", tag); + let name: String = entity.unwrap("name"); + let price: u64 = entity.unwrap("price"); + let delta: i64 = entity.unwrap("delta"); + let in_stock: bool = entity.unwrap("in_stock"); + let discount: f64 = entity.unwrap("discount"); + let tag: Entity = entity.unwrap("tag"); + assert_eq!(name, "laptop".to_string()); + assert_eq!(price, 200000u64); + assert_eq!(delta, -50i64); + assert!(in_stock); + assert_eq!(discount, 5.2f64); + assert_eq!(tag.id, tag_id); + } + #[test] + fn check_006() { + let product = Product { + name: "laptop".to_string(), + price: 200000u64, + }; + let expected_product = product.clone(); + let entity = product.to_eav(); + let converted_product = Product::from_eav(entity); + assert_eq!(expected_product, converted_product); + } }