From e98f6b7edf25b80f95dcd03ba32dd94d33b4f0a3 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Mon, 16 Mar 2026 10:09:24 +0100 Subject: [PATCH] chore: avoid to panic in case of an entity attribute is not found --- 01.workspace/heave/src/str/entity.rs | 5 +++-- 01.workspace/heave/src/str/failed_to.rs | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/01.workspace/heave/src/str/entity.rs b/01.workspace/heave/src/str/entity.rs index 0049b5d..b4114f1 100644 --- a/01.workspace/heave/src/str/entity.rs +++ b/01.workspace/heave/src/str/entity.rs @@ -141,14 +141,15 @@ impl Entity { /// # Returns /// /// A `Result` 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(&self, id: &str) -> Result where T: TryFrom, { 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`. /// diff --git a/01.workspace/heave/src/str/failed_to.rs b/01.workspace/heave/src/str/failed_to.rs index abfeffa..4ccc8f1 100644 --- a/01.workspace/heave/src/str/failed_to.rs +++ b/01.workspace/heave/src/str/failed_to.rs @@ -13,6 +13,8 @@ pub enum FailedTo { ConvertValue, /// Failed to execute predicate to mutate an item. ExecutePredicate(Vec>), + /// 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 => {