From 2acd2e9876bb6450db0f3b3713b975311c0f5322 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 13 Mar 2022 03:29:00 +0100 Subject: insert(): Remove `unwrap()`s Use a new `yaqlite::Error` type. Remove the other error types and use this main error type everywhere. For now, that seems simpler. The real reason why I centralised on one error type is that I wanted a single `Error::Sqlite` variant for both `rusqlite::Error` and `SqliteError` errors. However, I wasn't sure if it's possible to do that with `thiserror`, and I didn't want to bother having to write my own `std::error::Error` impls. --- src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 111ec3e..f151cf1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,20 +2,29 @@ pub mod sqlite; pub mod yaml; +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("SQL error")] + Sqlite(#[from] rusqlite::Error), +} + + pub fn insert( dbconn: &mut rusqlite::Connection, table_name: &str, data: &mut [yaml_rust::Yaml], -) { - let table_columns = crate::sqlite::get_column_names(&dbconn, table_name).unwrap(); +) -> Result<(), Error> { + let table_columns = crate::sqlite::get_column_names(&dbconn, table_name)?; for mut doc in data { - let tx = dbconn.transaction().unwrap(); + let tx = dbconn.transaction()?; - crate::yaml::extract(&mut doc, &tx, &table_name, &table_columns).unwrap(); + crate::yaml::extract(&mut doc, &tx, &table_name, &table_columns)?; - tx.commit().unwrap(); + tx.commit()?; } + + Ok(()) } @@ -48,7 +57,7 @@ mod tests { let mut data = yaml_rust::YamlLoader::load_from_str(&yaml_str).unwrap(); - insert(&mut conn, "test", &mut data); + insert(&mut conn, "test", &mut data).unwrap(); { let mut stmt = conn.prepare(r#" -- cgit v1.2.3