Compare commits
3 Commits
66e7bc22cf
...
next
| Author | SHA1 | Date | |
|---|---|---|---|
| 36a8b5100e | |||
| cb3d07ec26 | |||
| e98f6b7edf |
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "heave"
|
||||
version = "0.10.0"
|
||||
edition = "2024"
|
||||
edition = "2021"
|
||||
description = "An Entity-Attribute-Value (EAV) system library for Rust, backed by SQLite."
|
||||
license = "MIT"
|
||||
keywords = ["eav", "entity-attribute-value", "sqlite", "database", "orm", "data-model", "rust"]
|
||||
|
||||
@@ -46,7 +46,7 @@ impl From<Entity> for Product {
|
||||
// Create a new `Product` from the entity's attributes.
|
||||
Self {
|
||||
// Set the product's ID from the entity's ID.
|
||||
id: value.id(),
|
||||
id: value.id().to_string(),
|
||||
// Unwrap the "name" attribute to get the product's name.
|
||||
name: value.unwrap("name").expect("name is always present"),
|
||||
// Unwrap the optional "model" attribute to get the product's model.
|
||||
|
||||
@@ -37,7 +37,7 @@ impl From<Entity> for Component {
|
||||
// `from` is a function that converts an `Entity` into a `Component`.
|
||||
fn from(value: Entity) -> Self {
|
||||
Self {
|
||||
id: value.id(),
|
||||
id: value.id().to_string(),
|
||||
part_number: value
|
||||
.unwrap("part_number")
|
||||
.expect("part_number is always present"),
|
||||
|
||||
@@ -41,18 +41,18 @@ impl From<Entity> for Product {
|
||||
if let Some(ref subclass) = value.subclass() {
|
||||
match subclass.as_ref() {
|
||||
"laptop" => Product::Laptop(Laptop {
|
||||
id: value.id(),
|
||||
id: value.id().to_string(),
|
||||
model: value.unwrap("model").expect("model is mandatory"),
|
||||
price: value.unwrap("price").expect("price is mandatory"),
|
||||
}),
|
||||
"display" => Product::Display(Display {
|
||||
id: value.id(),
|
||||
id: value.id().to_string(),
|
||||
model: value.unwrap("model").expect("model is mandatory"),
|
||||
price: value.unwrap("price").expect("price is mandatory"),
|
||||
resolution: value.unwrap("resolution").expect("resolution is mandatory"),
|
||||
}),
|
||||
"mouse" => Product::Mouse(Mouse {
|
||||
id: value.id(),
|
||||
id: value.id().to_string(),
|
||||
model: value.unwrap("model").expect("model is mandatory"),
|
||||
price: value.unwrap("price").expect("price is mandatory"),
|
||||
wireless: value.unwrap("wireless").expect("wireless is mandatory"),
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
//!
|
||||
//! fn try_from(entity: Entity) -> Result<Self, Self::Error> {
|
||||
//! Ok(Self {
|
||||
//! id: entity.id(),
|
||||
//! id: entity.id().to_string(),
|
||||
//! name: entity.unwrap("name").map_err(|_| FailedTo::ConvertEntity)?,
|
||||
//! price: entity.unwrap("price").map_err(|_| FailedTo::ConvertEntity)?,
|
||||
//! in_stock: entity.unwrap("in_stock").map_err(|_| FailedTo::ConvertEntity)?,
|
||||
|
||||
@@ -141,14 +141,15 @@ impl Entity {
|
||||
/// # Returns
|
||||
///
|
||||
/// A `Result<T, FailedTo>` which is `Ok(T)` if the conversion is successful,
|
||||
/// or `Err(FailedTo::ConvertValue)` if it fails.
|
||||
/// or `Err(FailedTo::ConvertValue)` if the conversion fails,
|
||||
/// or `Err(FailedTo::FindAttribute)` if the attribute doesn't exist.
|
||||
pub fn unwrap<T>(&self, id: &str) -> Result<T, FailedTo>
|
||||
where
|
||||
T: TryFrom<Value>,
|
||||
{
|
||||
self.value_of(id)
|
||||
.map(|value| T::try_from(value.clone()).map_err(|_| FailedTo::ConvertValue))
|
||||
.unwrap()
|
||||
.unwrap_or(Err(FailedTo::FindAttribute))
|
||||
}
|
||||
/// Unwraps an attribute's value into an `Option<T>`.
|
||||
///
|
||||
@@ -194,8 +195,8 @@ impl Entity {
|
||||
.map(|value| value.unwrap_or(default))
|
||||
}
|
||||
/// Returns the ID of the entity.
|
||||
pub fn id(&self) -> String {
|
||||
self.id.clone()
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
/// Returns the subclass of the entity, if it has one.
|
||||
pub fn subclass(&self) -> Option<String> {
|
||||
|
||||
@@ -13,6 +13,8 @@ pub enum FailedTo {
|
||||
ConvertValue,
|
||||
/// Failed to execute predicate to mutate an item.
|
||||
ExecutePredicate(Vec<Box<dyn error::Error>>),
|
||||
/// Failed to find given attribute.
|
||||
FindAttribute,
|
||||
/// Failed to initialize the database.
|
||||
InitDatabase,
|
||||
/// Failed to load data from the database.
|
||||
@@ -38,6 +40,7 @@ impl PartialEq for FailedTo {
|
||||
| (FailedTo::ConvertObject, FailedTo::ConvertObject)
|
||||
| (FailedTo::ConvertValue, FailedTo::ConvertValue)
|
||||
| (FailedTo::ExecutePredicate(_), FailedTo::ExecutePredicate(_))
|
||||
| (FailedTo::FindAttribute, FailedTo::FindAttribute)
|
||||
| (FailedTo::InitDatabase, FailedTo::InitDatabase)
|
||||
| (FailedTo::LoadFromDB, FailedTo::LoadFromDB)
|
||||
| (FailedTo::LockCatalog, FailedTo::LockCatalog)
|
||||
@@ -74,6 +77,7 @@ impl std::fmt::Display for FailedTo {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
FailedTo::FindAttribute => write!(f, "Failed to find given attribute"),
|
||||
FailedTo::InitDatabase => write!(f, "Failed to initialize the database"),
|
||||
FailedTo::LoadFromDB => write!(f, "Failed to load data from the database"),
|
||||
FailedTo::LockCatalog => {
|
||||
|
||||
@@ -48,7 +48,7 @@ impl From<Item> for Entity {
|
||||
impl From<Entity> for Item {
|
||||
fn from(entity: Entity) -> Self {
|
||||
Self {
|
||||
id: entity.id(),
|
||||
id: entity.id().to_string(),
|
||||
first_seen: entity.ref_date.expect("ref date is always present"),
|
||||
name: entity.unwrap("name").expect("name is always present"),
|
||||
price: entity.unwrap("price").expect("price is always present"),
|
||||
|
||||
Reference in New Issue
Block a user