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 | |
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')
-rw-r--r-- | src/lib.rs | 21 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/sqlite.rs | 10 | ||||
-rw-r--r-- | src/yaml.rs | 9 |
4 files changed, 18 insertions, 24 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#" diff --git a/src/main.rs b/src/main.rs index 0ac5a26..0ec2724 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn main() { let mut yaml_data = yaml::YamlLoader::load_from_str(&text_data).unwrap(); - yaqlite::insert(&mut dbconn, "people", &mut yaml_data); + yaqlite::insert(&mut dbconn, "people", &mut yaml_data).unwrap(); dbg!(yaml_data); diff --git a/src/sqlite.rs b/src/sqlite.rs index 1e3aeec..ba3c19d 100644 --- a/src/sqlite.rs +++ b/src/sqlite.rs @@ -1,16 +1,8 @@ use rusqlite; -use thiserror; use std::collections::HashMap; -#[derive(thiserror::Error, Debug)] -pub enum SqliteError { - #[error("SQL error")] - Sqlite(#[from] rusqlite::Error), -} - - /// Get the fundamental SQLite datatype for a given type name. /// /// Use the SQLite rules for type affinity described in: @@ -51,7 +43,7 @@ pub struct Zero; pub fn get_column_names( dbconn: &rusqlite::Connection, table_name: &str, -) -> Result<HashMap<String, Zero>, SqliteError> { +) -> Result<HashMap<String, Zero>, crate::Error> { let mut column_names = HashMap::new(); let mut stmt = dbconn.prepare( diff --git a/src/yaml.rs b/src/yaml.rs index b7cecad..0acaf59 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -9,13 +9,6 @@ mod sql; pub use sql::*; -#[derive(thiserror::Error, Debug)] -pub enum YamlError { - #[error("SQL error")] - Sqlite(#[from] rusqlite::Error), -} - - // TODO: Separate functions to get a list of YAML hashes, and insert hashes into // the database. pub fn extract( @@ -23,7 +16,7 @@ pub fn extract( tx: &rusqlite::Transaction, table_name: &str, table_columns: &HashMap<String, crate::sqlite::Zero>, -) -> Result<(), YamlError> { +) -> Result<(), crate::Error> { match doc { yaml::Yaml::Array(ref mut array) => { for yaml_value in array { |