From cac890aa06d31ed23722f84ea1079e9289ee2382 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Sat, 4 Oct 2025 07:32:17 +0200 Subject: [PATCH] feat: add error handling for load functions to catalog --- .../heave/src/fun/sqlite_load_attributes.rs | 11 ++++++---- .../heave/src/fun/sqlite_load_by_class.rs | 20 +++++++++++-------- .../heave/src/fun/sqlite_load_by_id.rs | 18 +++++++++-------- 01.workspace/heave/src/str/catalog.rs | 17 ++++++++-------- 01.workspace/heave/src/str/failed_to.rs | 5 +++++ 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/01.workspace/heave/src/fun/sqlite_load_attributes.rs b/01.workspace/heave/src/fun/sqlite_load_attributes.rs index c2b142d..f1cfbce 100644 --- a/01.workspace/heave/src/fun/sqlite_load_attributes.rs +++ b/01.workspace/heave/src/fun/sqlite_load_attributes.rs @@ -6,13 +6,16 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#" WHERE entity_id = ?1; "#; -pub fn run(transaction: &Transaction, entity: &mut Entity) { - let mut select_attributes_statement = transaction.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap(); +pub fn run(transaction: &Transaction, entity: &mut Entity) -> Result<(), FailedTo> { + let mut select_attributes_statement = transaction + .prepare(SELECT_ATTRIBUTE_BY_FK) + .map_err(|_| FailedTo::PrepareSQLiteStatement)?; let attributes = select_attributes_statement .query_map([&entity.id], sqlite::map::row_to_attribute) - .unwrap(); + .map_err(|_| FailedTo::ExecuteSQLiteQuery)?; for attribute in attributes { - let attribute = attribute.unwrap(); + let attribute = attribute.map_err(|_| FailedTo::MapAttribute)?; entity.attributes.insert(attribute.id.clone(), attribute); } + Ok(()) } diff --git a/01.workspace/heave/src/fun/sqlite_load_by_class.rs b/01.workspace/heave/src/fun/sqlite_load_by_class.rs index ee85417..16ec63d 100644 --- a/01.workspace/heave/src/fun/sqlite_load_by_class.rs +++ b/01.workspace/heave/src/fun/sqlite_load_by_class.rs @@ -6,20 +6,24 @@ const SELECT_ENTITY_BY_CLASS: &str = r#" WHERE class = ?1; "#; -pub fn run(path: &path::Path, entity_class: &str) -> Vec { +pub fn run(path: &path::Path, entity_class: &str) -> Result, FailedTo> { let mut entities = Vec::::new(); - let mut connection = Connection::open(path).unwrap(); - let mut transaction = connection.transaction().unwrap(); + let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?; + let mut transaction = connection + .transaction() + .map_err(|_| FailedTo::BeginSQLiteTransaction)?; transaction.set_drop_behavior(DropBehavior::Commit); - let mut statement = transaction.prepare(SELECT_ENTITY_BY_CLASS).unwrap(); + let mut statement = transaction + .prepare(SELECT_ENTITY_BY_CLASS) + .map_err(|_| FailedTo::PrepareSQLiteStatement)?; let result = statement .query_map([entity_class], sqlite::map::row_to_entity) - .unwrap(); + .map_err(|_| FailedTo::ExecuteSQLiteQuery)?; for entity in result { - let mut entity = entity.unwrap(); - sqlite::load::attributes(&transaction, &mut entity); + let mut entity = entity.map_err(|_| FailedTo::MapEntity)?; + sqlite::load::attributes(&transaction, &mut entity)?; entity.state = EntityState::Loaded; entities.push(entity); } - entities + Ok(entities) } diff --git a/01.workspace/heave/src/fun/sqlite_load_by_id.rs b/01.workspace/heave/src/fun/sqlite_load_by_id.rs index 416b6b9..15145b7 100644 --- a/01.workspace/heave/src/fun/sqlite_load_by_id.rs +++ b/01.workspace/heave/src/fun/sqlite_load_by_id.rs @@ -6,17 +6,19 @@ const SELECT_ENTITY_BY_ID: &str = r#" WHERE id = ?1; "#; -pub fn run(path: &path::Path, entity_id: &str) -> Option { - let mut connection = Connection::open(path).unwrap(); - let mut transaction = connection.transaction().unwrap(); +pub fn run(path: &path::Path, entity_id: &str) -> Result, FailedTo> { + let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?; + let mut transaction = connection + .transaction() + .map_err(|_| FailedTo::BeginSQLiteTransaction)?; transaction.set_drop_behavior(DropBehavior::Commit); - let result = transaction + let mut entity = transaction .query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity) - .optional(); - let mut entity = result.unwrap(); + .optional() + .map_err(|_| FailedTo::ExecuteSQLiteQuery)?; if let Some(ref mut entity) = entity { - sqlite::load::attributes(&transaction, entity); + sqlite::load::attributes(&transaction, entity)?; entity.state = EntityState::Loaded; } - entity + Ok(entity) } diff --git a/01.workspace/heave/src/str/catalog.rs b/01.workspace/heave/src/str/catalog.rs index 363786b..51b030f 100644 --- a/01.workspace/heave/src/str/catalog.rs +++ b/01.workspace/heave/src/str/catalog.rs @@ -150,27 +150,26 @@ impl O { /// # Arguments /// /// * `id` - The ID of the entity to load. - pub fn load_by_id(&mut self, id: &str) { + pub fn load_by_id(&mut self, id: &str) -> Result<(), FailedTo> { let path = path::Path::new(&self.path); - let entity = sqlite::load::by_id(path, id); - match entity { - None => (), - Some(entity) => { - self.items.insert(entity.id.clone(), entity); - } + let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?; + if let Some(entity) = entity { + self.items.insert(entity.id.clone(), entity); } + Ok(()) } /// Loads all entities of a specific class from the database into the catalog. - pub fn load_by_class(&mut self) + pub fn load_by_class(&mut self) -> Result<(), FailedTo> where T: EAV, { let class = T::class(); let path = path::Path::new(&self.path); - let entities = sqlite::load::by_class(path, class); + let entities = sqlite::load::by_class(path, class)?; for entity in entities { self.items.insert(entity.id.clone(), entity); } + Ok(()) } } diff --git a/01.workspace/heave/src/str/failed_to.rs b/01.workspace/heave/src/str/failed_to.rs index e85e38c..6de511a 100644 --- a/01.workspace/heave/src/str/failed_to.rs +++ b/01.workspace/heave/src/str/failed_to.rs @@ -3,8 +3,13 @@ pub enum E { BeginSQLiteTransaction, CommitSQLiteTransaction, ExecuteSQLiteBatch, + ExecuteSQLiteQuery, ExecuteSQLiteStatement, InitDatabase, + LoadFromDB, + MapAttribute, + MapEntity, OpenSQLiteConnection, PersistCatalog, + PrepareSQLiteStatement, }