From 5ec86bac7fa4b5b7fcbb43a1b8f579021db76557 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Sat, 4 Oct 2025 07:03:08 +0200 Subject: [PATCH] feat: add error handling to catalog persist function --- .../heave/src/fun/sqlite_persist_catalog.rs | 47 +++++++++++++------ 01.workspace/heave/src/str/catalog.rs | 5 +- 01.workspace/heave/src/str/failed_to.rs | 4 ++ .../heave/src/tst/rusty_budger_use.rs | 3 +- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/01.workspace/heave/src/fun/sqlite_persist_catalog.rs b/01.workspace/heave/src/fun/sqlite_persist_catalog.rs index 0ddb044..2ff1a16 100644 --- a/01.workspace/heave/src/fun/sqlite_persist_catalog.rs +++ b/01.workspace/heave/src/fun/sqlite_persist_catalog.rs @@ -26,33 +26,50 @@ const INSERT_ATTRIBUTE_STATEMENT_TEMPLATE: &str = r#" VALUES (?1, ?2, ?3); "#; -fn write_attribute(attribute: &Attribute, entity: &Entity, transaction: &rusqlite::Transaction) { +fn write_attribute( + attribute: &Attribute, + entity: &Entity, + transaction: &rusqlite::Transaction, +) -> result::Result<(), FailedTo> { let column = column(&attribute.value); let attribute_values = (&attribute.id, &entity.id, &attribute.value.to_string()); let insert_attribute_statement = INSERT_ATTRIBUTE_STATEMENT_TEMPLATE.replace("{column}", column); - let _ = transaction.execute(&insert_attribute_statement, attribute_values); + transaction + .execute(&insert_attribute_statement, attribute_values) + .map_err(|_| FailedTo::ExecuteSQLiteStatement)?; + Ok(()) } -fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) { +fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> { let entity_id = [&entity.id]; let entity_values = (&entity.id, &entity.class, entity.ref_date); - let _ = transaction.execute(DELETE_ENTITY_STATEMENT, entity_id); - let _ = transaction.execute(INSERT_ENTITY_STATEMENT, entity_values); - for (_key, attribute) in entity.attributes.iter() { - write_attribute(attribute, entity, transaction); + transaction + .execute(DELETE_ENTITY_STATEMENT, entity_id) + .map_err(|_| FailedTo::ExecuteSQLiteStatement)?; + transaction + .execute(INSERT_ENTITY_STATEMENT, entity_values) + .map_err(|_| FailedTo::ExecuteSQLiteStatement)?; + for attribute in entity.attributes.values() { + write_attribute(attribute, entity, transaction)?; } + Ok(()) } -pub fn run(path: &path::Path, catalog: &Catalog) { - let mut connection = Connection::open(path).unwrap(); - let transaction = connection.transaction().unwrap(); - for (_key, entity) in catalog +pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo> { + let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?; + let transaction = connection + .transaction() + .map_err(|_| FailedTo::BeginSQLiteTransaction)?; + for entity in catalog .items - .iter() - .filter(|item| item.1.state == EntityState::New) + .values() + .filter(|item| item.state == EntityState::New) { - write_entity(entity, &transaction); + write_entity(entity, &transaction)?; } - let _ = transaction.commit(); + transaction + .commit() + .map_err(|_| FailedTo::CommitSQLiteTransaction)?; + Ok(()) } diff --git a/01.workspace/heave/src/str/catalog.rs b/01.workspace/heave/src/str/catalog.rs index b0cbf4e..363786b 100644 --- a/01.workspace/heave/src/str/catalog.rs +++ b/01.workspace/heave/src/str/catalog.rs @@ -139,9 +139,10 @@ impl O { } /// Persists the current state of the catalog to the database. - pub fn persist(&self) { + pub fn persist(&self) -> result::Result<(), FailedTo> { let path = path::Path::new(&self.path); - sqlite::persist::catalog(path, self); + sqlite::persist::catalog(path, self).map_err(|_| FailedTo::PersistCatalog)?; + Ok(()) } /// Loads an entity by its ID from the database into the catalog. diff --git a/01.workspace/heave/src/str/failed_to.rs b/01.workspace/heave/src/str/failed_to.rs index 06e4074..e85e38c 100644 --- a/01.workspace/heave/src/str/failed_to.rs +++ b/01.workspace/heave/src/str/failed_to.rs @@ -1,6 +1,10 @@ #[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)] pub enum E { + BeginSQLiteTransaction, + CommitSQLiteTransaction, ExecuteSQLiteBatch, + ExecuteSQLiteStatement, InitDatabase, OpenSQLiteConnection, + PersistCatalog, } diff --git a/01.workspace/heave/src/tst/rusty_budger_use.rs b/01.workspace/heave/src/tst/rusty_budger_use.rs index ad5a97c..2c2b7d3 100644 --- a/01.workspace/heave/src/tst/rusty_budger_use.rs +++ b/01.workspace/heave/src/tst/rusty_budger_use.rs @@ -129,6 +129,7 @@ mod tests { catalog.insert_many(operations); catalog.insert_many(categories); catalog.insert_many(relations); - catalog.persist(); + let result = catalog.persist(); + assert!(result.is_ok()); } }