From 747a8d36ed2ff4f15b8f72d2d5725ed3eec4ea40 Mon Sep 17 00:00:00 2001 From: gwenn <45554+gwenn@users.noreply.github.com> Date: Wed, 19 Dec 2018 21:58:33 +0100 Subject: [PATCH] Err partial eq (#452) Impl PartialEq for Error --- src/error.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 25 ++++++++++++------------- src/statement.rs | 5 +++-- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/error.rs b/src/error.rs index 4bcfdf5..432de18 100644 --- a/src/error.rs +++ b/src/error.rs @@ -96,6 +96,46 @@ pub enum Error { UnwindingPanic, } +impl PartialEq for Error { + fn eq(&self, other: &Error) -> bool { + match (self, other) { + (Error::SqliteFailure(e1, s1), Error::SqliteFailure(e2, s2)) => e1 == e2 && s1 == s2, + (Error::SqliteSingleThreadedMode, Error::SqliteSingleThreadedMode) => true, + (Error::IntegralValueOutOfRange(i1, n1), Error::IntegralValueOutOfRange(i2, n2)) => { + i1 == i2 && n1 == n2 + } + (Error::Utf8Error(e1), Error::Utf8Error(e2)) => e1 == e2, + (Error::NulError(e1), Error::NulError(e2)) => e1 == e2, + (Error::InvalidParameterName(n1), Error::InvalidParameterName(n2)) => n1 == n2, + (Error::InvalidPath(p1), Error::InvalidPath(p2)) => p1 == p2, + (Error::ExecuteReturnedResults, Error::ExecuteReturnedResults) => true, + (Error::QueryReturnedNoRows, Error::QueryReturnedNoRows) => true, + (Error::InvalidColumnIndex(i1), Error::InvalidColumnIndex(i2)) => i1 == i2, + (Error::InvalidColumnName(n1), Error::InvalidColumnName(n2)) => n1 == n2, + (Error::InvalidColumnType(i1, t1), Error::InvalidColumnType(i2, t2)) => { + i1 == i2 && t1 == t2 + } + (Error::StatementChangedRows(n1), Error::StatementChangedRows(n2)) => n1 == n2, + #[cfg(feature = "functions")] + ( + Error::InvalidFunctionParameterType(i1, t1), + Error::InvalidFunctionParameterType(i2, t2), + ) => i1 == i2 && t1 == t2, + #[cfg(feature = "vtab")] + ( + Error::InvalidFilterParameterType(i1, t1), + Error::InvalidFilterParameterType(i2, t2), + ) => i1 == i2 && t1 == t2, + (Error::InvalidQuery, Error::InvalidQuery) => true, + #[cfg(feature = "vtab")] + (Error::ModuleError(s1), Error::ModuleError(s2)) => s1 == s2, + #[cfg(feature = "functions")] + (Error::UnwindingPanic, Error::UnwindingPanic) => true, + (_, _) => false, + } + } +} + impl From for Error { fn from(err: str::Utf8Error) -> Error { Error::Utf8Error(err) diff --git a/src/lib.rs b/src/lib.rs index cf239c0..a4e7591 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,8 +148,8 @@ pub type Result = result::Result; pub trait OptionalExtension { /// Converts a `Result` into a `Result>`. /// - /// By default, Rusqlite treats 0 rows being returned from a query that is expected to return 1 - /// row as an error. This method will + /// By default, Rusqlite treats 0 rows being returned from a query that is + /// expected to return 1 row as an error. This method will /// handle that error, and give you back an `Option` instead. fn optional(self) -> Result>; } @@ -392,8 +392,9 @@ impl Connection { /// If the query returns more than one row, all rows except the first are /// ignored. /// - /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, - /// you can call `.optional()` on the result of this to get a `Result>`. + /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the + /// query truly is optional, you can call `.optional()` on the result of + /// this to get a `Result>`. /// /// # Failure /// @@ -415,8 +416,9 @@ impl Connection { /// If the query returns more than one row, all rows except the first are /// ignored. /// - /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, - /// you can call `.optional()` on the result of this to get a `Result>`. + /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the + /// query truly is optional, you can call `.optional()` on the result of + /// this to get a `Result>`. /// /// # Failure /// @@ -1277,9 +1279,8 @@ mod test { fn test_execute_select() { let db = checked_memory_handle(); let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err(); - match err { - Error::ExecuteReturnedResults => (), - _ => panic!("Unexpected error: {}", err), + if err != Error::ExecuteReturnedResults { + panic!("Unexpected error: {}", err); } } @@ -1408,16 +1409,14 @@ mod test { fn test_optional() { let db = checked_memory_handle(); - let result: Result = - db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0)); + let result: Result = db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0)); let result = result.optional(); match result.unwrap() { None => (), _ => panic!("Unexpected result"), } - let result: Result = - db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0)); + let result: Result = db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0)); let result = result.optional(); match result.unwrap() { Some(1) => (), diff --git a/src/statement.rs b/src/statement.rs index 58d290d..e2445a9 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -377,8 +377,9 @@ impl<'conn> Statement<'conn> { /// If the query returns more than one row, all rows except the first are /// ignored. /// - /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, - /// you can call `.optional()` on the result of this to get a `Result>`. + /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the + /// query truly is optional, you can call `.optional()` on the result of + /// this to get a `Result>`. /// /// # Failure ///