diff options
author | Teddy Wing | 2022-03-13 03:29:00 +0100 |
---|---|---|
committer | Teddy Wing | 2022-03-13 03:29:00 +0100 |
commit | 2acd2e9876bb6450db0f3b3713b975311c0f5322 (patch) | |
tree | 8ced1e2f7c5054c1e9969ee3af2a527cdc38b2bf /src/lib.rs | |
parent | cb3ab314425b29aa881597796d2c7deecfba025c (diff) | |
download | yaqlite-2acd2e9876bb6450db0f3b3713b975311c0f5322.tar.bz2 |
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 21 |
1 files changed, 15 insertions, 6 deletions
@@ -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#" |