From fedf6b41ae2a7391edabf9a7262104c1c3caaacf Mon Sep 17 00:00:00 2001 From: gwenn Date: Thu, 7 Apr 2022 09:15:55 +0200 Subject: [PATCH 1/3] Use sqlite_error_code where possible --- src/busy.rs | 12 +++++------- src/lib.rs | 12 ++++-------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/busy.rs b/src/busy.rs index b394d01..7297f20 100644 --- a/src/busy.rs +++ b/src/busy.rs @@ -90,7 +90,7 @@ mod test { use std::thread; use std::time::Duration; - use crate::{Connection, Error, ErrorCode, Result, TransactionBehavior}; + use crate::{Connection, ErrorCode, Result, TransactionBehavior}; #[test] fn test_default_busy() -> Result<()> { @@ -101,12 +101,10 @@ mod test { let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?; let db2 = Connection::open(&path)?; let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!()); - match r.unwrap_err() { - Error::SqliteFailure(err, _) => { - assert_eq!(err.code, ErrorCode::DatabaseBusy); - } - err => panic!("Unexpected error {}", err), - } + assert_eq!( + r.unwrap_err().sqlite_error_code(), + Some(ErrorCode::DatabaseBusy) + ); tx1.rollback() } diff --git a/src/lib.rs b/src/lib.rs index 070b1a7..4dbb819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1742,14 +1742,10 @@ mod test { let result: Result> = stmt.query([])?.map(|r| r.get(0)).collect(); - match result.unwrap_err() { - Error::SqliteFailure(err, _) => { - assert_eq!(err.code, ErrorCode::OperationInterrupted); - } - err => { - panic!("Unexpected error {}", err); - } - } + assert_eq!( + result.unwrap_err().sqlite_error_code(), + Some(ErrorCode::OperationInterrupted) + ); Ok(()) } From 214a6244f921db2c30fb76dc1a049b240cc67991 Mon Sep 17 00:00:00 2001 From: gwenn Date: Thu, 7 Apr 2022 09:17:07 +0200 Subject: [PATCH 2/3] Rustfmt --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4dbb819..b2e798f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -384,9 +384,8 @@ impl Connection { /// /// - Open the database for both reading or writing. /// - Create the database if one does not exist at the path. - /// - Allow the filename to be interpreted as a URI (see - /// for - /// details). + /// - Allow the filename to be interpreted as a URI (see + /// for details). /// - Disables the use of a per-connection mutex. /// /// Rusqlite enforces thread-safety at compile time, so additional From ad911efd15eb177dd9829a13d3721968d55aeedd Mon Sep 17 00:00:00 2001 From: gwenn Date: Thu, 7 Apr 2022 09:17:42 +0200 Subject: [PATCH 3/3] Replace .err().unwrap() by .unwrap_err() --- src/lib.rs | 2 +- src/types/mod.rs | 62 +++++++++++++++++------------------------------- 2 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b2e798f..418a1b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1288,7 +1288,7 @@ mod test { let filename = "no_such_file.db"; let result = Connection::open_with_flags(filename, OpenFlags::SQLITE_OPEN_READ_ONLY); assert!(result.is_err()); - let err = result.err().unwrap(); + let err = result.unwrap_err(); if let Error::SqliteFailure(e, Some(msg)) = err { assert_eq!(ErrorCode::CannotOpen, e.code); assert_eq!(ffi::SQLITE_CANTOPEN, e.extended_code); diff --git a/src/types/mod.rs b/src/types/mod.rs index 4e524b2..a5c73d5 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -272,85 +272,67 @@ mod test { // check some invalid types // 0 is actually a blob (Vec) - assert!(is_invalid_column_type( - row.get::<_, c_int>(0).err().unwrap() - )); - assert!(is_invalid_column_type( - row.get::<_, c_int>(0).err().unwrap() - )); + assert!(is_invalid_column_type(row.get::<_, c_int>(0).unwrap_err())); + assert!(is_invalid_column_type(row.get::<_, c_int>(0).unwrap_err())); assert!(is_invalid_column_type(row.get::<_, i64>(0).err().unwrap())); assert!(is_invalid_column_type( - row.get::<_, c_double>(0).err().unwrap() - )); - assert!(is_invalid_column_type( - row.get::<_, String>(0).err().unwrap() + row.get::<_, c_double>(0).unwrap_err() )); + assert!(is_invalid_column_type(row.get::<_, String>(0).unwrap_err())); #[cfg(feature = "time")] assert!(is_invalid_column_type( - row.get::<_, time::OffsetDateTime>(0).err().unwrap() + row.get::<_, time::OffsetDateTime>(0).unwrap_err() )); assert!(is_invalid_column_type( - row.get::<_, Option>(0).err().unwrap() + row.get::<_, Option>(0).unwrap_err() )); // 1 is actually a text (String) - assert!(is_invalid_column_type( - row.get::<_, c_int>(1).err().unwrap() - )); + assert!(is_invalid_column_type(row.get::<_, c_int>(1).unwrap_err())); assert!(is_invalid_column_type(row.get::<_, i64>(1).err().unwrap())); assert!(is_invalid_column_type( - row.get::<_, c_double>(1).err().unwrap() + row.get::<_, c_double>(1).unwrap_err() )); assert!(is_invalid_column_type( - row.get::<_, Vec>(1).err().unwrap() + row.get::<_, Vec>(1).unwrap_err() )); assert!(is_invalid_column_type( - row.get::<_, Option>(1).err().unwrap() + row.get::<_, Option>(1).unwrap_err() )); // 2 is actually an integer + assert!(is_invalid_column_type(row.get::<_, String>(2).unwrap_err())); assert!(is_invalid_column_type( - row.get::<_, String>(2).err().unwrap() + row.get::<_, Vec>(2).unwrap_err() )); assert!(is_invalid_column_type( - row.get::<_, Vec>(2).err().unwrap() - )); - assert!(is_invalid_column_type( - row.get::<_, Option>(2).err().unwrap() + row.get::<_, Option>(2).unwrap_err() )); // 3 is actually a float (c_double) - assert!(is_invalid_column_type( - row.get::<_, c_int>(3).err().unwrap() - )); + assert!(is_invalid_column_type(row.get::<_, c_int>(3).unwrap_err())); assert!(is_invalid_column_type(row.get::<_, i64>(3).err().unwrap())); + assert!(is_invalid_column_type(row.get::<_, String>(3).unwrap_err())); assert!(is_invalid_column_type( - row.get::<_, String>(3).err().unwrap() + row.get::<_, Vec>(3).unwrap_err() )); assert!(is_invalid_column_type( - row.get::<_, Vec>(3).err().unwrap() - )); - assert!(is_invalid_column_type( - row.get::<_, Option>(3).err().unwrap() + row.get::<_, Option>(3).unwrap_err() )); // 4 is actually NULL - assert!(is_invalid_column_type( - row.get::<_, c_int>(4).err().unwrap() - )); + assert!(is_invalid_column_type(row.get::<_, c_int>(4).unwrap_err())); assert!(is_invalid_column_type(row.get::<_, i64>(4).err().unwrap())); assert!(is_invalid_column_type( - row.get::<_, c_double>(4).err().unwrap() + row.get::<_, c_double>(4).unwrap_err() )); + assert!(is_invalid_column_type(row.get::<_, String>(4).unwrap_err())); assert!(is_invalid_column_type( - row.get::<_, String>(4).err().unwrap() - )); - assert!(is_invalid_column_type( - row.get::<_, Vec>(4).err().unwrap() + row.get::<_, Vec>(4).unwrap_err() )); #[cfg(feature = "time")] assert!(is_invalid_column_type( - row.get::<_, time::OffsetDateTime>(4).err().unwrap() + row.get::<_, time::OffsetDateTime>(4).unwrap_err() )); Ok(()) }