From e025f283f59c67c6bbe2f0cb194b912b901b7b29 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sun, 8 Dec 2024 17:02:58 +0100 Subject: [PATCH] Introduce err macro --- src/error.rs | 15 +++++++++++++++ src/inner_connection.rs | 29 ++++++++++------------------- src/lib.rs | 6 ++---- src/limits.rs | 17 ++++------------- src/pragma.rs | 26 +++++--------------------- src/statement.rs | 5 +---- src/vtab/mod.rs | 5 +---- src/vtab/series.rs | 7 ++----- 8 files changed, 40 insertions(+), 70 deletions(-) diff --git a/src/error.rs b/src/error.rs index 38ca8b7..b989adb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -412,6 +412,21 @@ pub fn error_from_sqlite_code(code: c_int, message: Option) -> Error { Error::SqliteFailure(ffi::Error::new(code), message) } +macro_rules! err { + ($code:expr $(,)?) => { + $crate::error::error_from_sqlite_code($code, None) + }; + ($code:expr, $msg:literal $(,)?) => { + $crate::error::error_from_sqlite_code($code, Some(format!($msg))) + }; + ($code:expr, $err:expr $(,)?) => { + $crate::error::error_from_sqlite_code($code, Some(format!($err))) + }; + ($code:expr, $fmt:expr, $($arg:tt)*) => { + $crate::error::error_from_sqlite_code($code, Some(format!($fmt, $($arg)*))) + }; +} + #[cold] pub unsafe fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error { error_from_sqlite_code(code, error_msg(db, code)) diff --git a/src/inner_connection.rs b/src/inner_connection.rs index edfaade..a2371bb 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -90,7 +90,7 @@ impl InnerConnection { let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), z_vfs); if r != ffi::SQLITE_OK { let e = if db.is_null() { - error_from_sqlite_code(r, Some(c_path.to_string_lossy().to_string())) + err!(r, "{}", c_path.to_string_lossy()) } else { let mut e = error_from_handle(db, r); if let Error::SqliteFailure( @@ -101,10 +101,7 @@ impl InnerConnection { Some(msg), ) = e { - e = Error::SqliteFailure( - ffi::Error::new(r), - Some(format!("{msg}: {}", c_path.to_string_lossy())), - ); + e = err!(r, "{msg}: {}", c_path.to_string_lossy()); } ffi::sqlite3_close(db); e @@ -344,14 +341,11 @@ impl InnerConnection { match r { 0 => Ok(false), 1 => Ok(true), - -1 => Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("{db_name:?} is not the name of a database")), - )), - _ => Err(error_from_sqlite_code( - r, - Some("Unexpected result".to_owned()), + -1 => Err(err!( + ffi::SQLITE_MISUSE, + "{db_name:?} is not the name of a database" )), + _ => Err(err!(r, "Unexpected result")), } } @@ -370,14 +364,11 @@ impl InnerConnection { 0 => Ok(super::transaction::TransactionState::None), 1 => Ok(super::transaction::TransactionState::Read), 2 => Ok(super::transaction::TransactionState::Write), - -1 => Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("{db_name:?} is not the name of a valid schema")), - )), - _ => Err(error_from_sqlite_code( - r, - Some("Unexpected result".to_owned()), + -1 => Err(err!( + ffi::SQLITE_MISUSE, + "{db_name:?} is not the name of a valid schema" )), + _ => Err(err!(r, "Unexpected result")), } } diff --git a/src/lib.rs b/src/lib.rs index 07562d8..eca27ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,7 @@ pub use crate::version::*; #[doc(hidden)] pub use rusqlite_macros::__bind; +#[macro_use] mod error; #[cfg(not(feature = "loadable_extension"))] @@ -321,10 +322,7 @@ fn str_for_sqlite(s: &[u8]) -> Result<(*const c_char, c_int, ffi::sqlite3_destru // failed. fn len_as_c_int(len: usize) -> Result { if len >= (c_int::MAX as usize) { - Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_TOOBIG), - None, - )) + Err(err!(ffi::SQLITE_TOOBIG)) } else { Ok(len as c_int) } diff --git a/src/limits.rs b/src/limits.rs index 5107f57..932c8ff 100644 --- a/src/limits.rs +++ b/src/limits.rs @@ -1,6 +1,6 @@ //! Run-Time Limits -use crate::{ffi, Connection, Error, Result}; +use crate::{ffi, Connection, Result}; use std::os::raw::c_int; /// Run-Time limit categories, for use with [`Connection::limit`] and @@ -57,10 +57,7 @@ impl Connection { let c = self.db.borrow(); let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, -1) }; if rc < 0 { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_RANGE), - Some(format!("{limit:?} is invalid")), - )); + return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid")); } Ok(rc) } @@ -71,18 +68,12 @@ impl Connection { #[cfg_attr(docsrs, doc(cfg(feature = "limits")))] pub fn set_limit(&self, limit: Limit, new_val: i32) -> Result { if new_val < 0 { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_RANGE), - Some(format!("{new_val} is invalid")), - )); + return Err(err!(ffi::SQLITE_RANGE, "{new_val} is invalid")); } let c = self.db.borrow_mut(); let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, new_val) }; if rc < 0 { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_RANGE), - Some(format!("{limit:?} is invalid")), - )); + return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid")); } Ok(rc) } diff --git a/src/pragma.rs b/src/pragma.rs index ff9b398..a8a3a43 100644 --- a/src/pragma.rs +++ b/src/pragma.rs @@ -2,7 +2,6 @@ use std::ops::Deref; -use crate::error::Error; use crate::ffi; use crate::types::{ToSql, ToSqlOutput, ValueRef}; use crate::{Connection, DatabaseName, Result, Row}; @@ -35,10 +34,7 @@ impl Sql { self.buf.push_str(keyword); Ok(()) } else { - Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Invalid keyword \"{keyword}\"")), - )) + Err(err!(ffi::SQLITE_MISUSE, "Invalid keyword \"{keyword}\"")) } } @@ -66,24 +62,15 @@ impl Sql { ToSqlOutput::Owned(ref v) => ValueRef::from(v), #[cfg(feature = "blob")] ToSqlOutput::ZeroBlob(_) => { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Unsupported value \"{value:?}\"")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); } #[cfg(feature = "functions")] ToSqlOutput::Arg(_) => { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Unsupported value \"{value:?}\"")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); } #[cfg(feature = "array")] ToSqlOutput::Array(_) => { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Unsupported value \"{value:?}\"")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); } }; match value { @@ -98,10 +85,7 @@ impl Sql { self.push_string_literal(s); } _ => { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Unsupported value \"{value:?}\"")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); } }; Ok(()) diff --git a/src/statement.rs b/src/statement.rs index df4cbb6..76d07b4 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -608,10 +608,7 @@ impl Statement<'_> { } #[cfg(feature = "functions")] ToSqlOutput::Arg(_) => { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("Unsupported value \"{value:?}\"")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "Unsupported value \"{value:?}\"")); } #[cfg(feature = "array")] ToSqlOutput::Array(a) => { diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 72c884f..c23ecfc 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -501,10 +501,7 @@ impl IndexInfo { let idx = constraint_idx as c_int; let collation = unsafe { ffi::sqlite3_vtab_collation(self.0, idx) }; if collation.is_null() { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_MISUSE), - Some(format!("{constraint_idx} is out of range")), - )); + return Err(err!(ffi::SQLITE_MISUSE, "{constraint_idx} is out of range")); } Ok(unsafe { CStr::from_ptr(collation) }.to_str()?) } diff --git a/src/vtab/series.rs b/src/vtab/series.rs index 9a39749..3cd90b7 100644 --- a/src/vtab/series.rs +++ b/src/vtab/series.rs @@ -12,7 +12,7 @@ use crate::vtab::{ eponymous_only_module, Context, IndexConstraintOp, IndexInfo, VTab, VTabConfig, VTabConnection, VTabCursor, Values, }; -use crate::{Connection, Error, Result}; +use crate::{error::error_from_sqlite_code, Connection, Result}; /// Register the `generate_series` module. pub fn load_module(conn: &Connection) -> Result<()> { @@ -108,10 +108,7 @@ unsafe impl<'vtab> VTab<'vtab> for SeriesTab { debug_assert_eq!(Ok("BINARY"), info.collation(*j)); } if !(unusable_mask & !idx_num).is_empty() { - return Err(Error::SqliteFailure( - ffi::Error::new(ffi::SQLITE_CONSTRAINT), - None, - )); + return Err(error_from_sqlite_code(ffi::SQLITE_CONSTRAINT, None)); } if idx_num.contains(QueryPlanFlags::BOTH) { // Both start= and stop= boundaries are available.