Err partial eq (#452)

Impl PartialEq for Error
This commit is contained in:
gwenn 2018-12-19 21:58:33 +01:00 committed by GitHub
parent 7619f638a8
commit 747a8d36ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 15 deletions

View File

@ -96,6 +96,46 @@ pub enum Error {
UnwindingPanic, 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<str::Utf8Error> for Error { impl From<str::Utf8Error> for Error {
fn from(err: str::Utf8Error) -> Error { fn from(err: str::Utf8Error) -> Error {
Error::Utf8Error(err) Error::Utf8Error(err)

View File

@ -148,8 +148,8 @@ pub type Result<T> = result::Result<T, Error>;
pub trait OptionalExtension<T> { pub trait OptionalExtension<T> {
/// Converts a `Result<T>` into a `Result<Option<T>>`. /// Converts a `Result<T>` into a `Result<Option<T>>`.
/// ///
/// By default, Rusqlite treats 0 rows being returned from a query that is expected to return 1 /// By default, Rusqlite treats 0 rows being returned from a query that is
/// row as an error. This method will /// expected to return 1 row as an error. This method will
/// handle that error, and give you back an `Option<T>` instead. /// handle that error, and give you back an `Option<T>` instead.
fn optional(self) -> Result<Option<T>>; fn optional(self) -> Result<Option<T>>;
} }
@ -392,8 +392,9 @@ impl Connection {
/// If the query returns more than one row, all rows except the first are /// If the query returns more than one row, all rows except the first are
/// ignored. /// ignored.
/// ///
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`. /// query truly is optional, you can call `.optional()` on the result of
/// this to get a `Result<Option<T>>`.
/// ///
/// # Failure /// # Failure
/// ///
@ -415,8 +416,9 @@ impl Connection {
/// If the query returns more than one row, all rows except the first are /// If the query returns more than one row, all rows except the first are
/// ignored. /// ignored.
/// ///
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`. /// query truly is optional, you can call `.optional()` on the result of
/// this to get a `Result<Option<T>>`.
/// ///
/// # Failure /// # Failure
/// ///
@ -1277,9 +1279,8 @@ mod test {
fn test_execute_select() { fn test_execute_select() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err(); let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err();
match err { if err != Error::ExecuteReturnedResults {
Error::ExecuteReturnedResults => (), panic!("Unexpected error: {}", err);
_ => panic!("Unexpected error: {}", err),
} }
} }
@ -1408,16 +1409,14 @@ mod test {
fn test_optional() { fn test_optional() {
let db = checked_memory_handle(); let db = checked_memory_handle();
let result: Result<i64> = let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0));
db.query_row("SELECT 1 WHERE 0 <> 0", NO_PARAMS, |r| r.get(0));
let result = result.optional(); let result = result.optional();
match result.unwrap() { match result.unwrap() {
None => (), None => (),
_ => panic!("Unexpected result"), _ => panic!("Unexpected result"),
} }
let result: Result<i64> = let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0));
db.query_row("SELECT 1 WHERE 0 == 0", NO_PARAMS, |r| r.get(0));
let result = result.optional(); let result = result.optional();
match result.unwrap() { match result.unwrap() {
Some(1) => (), Some(1) => (),

View File

@ -377,8 +377,9 @@ impl<'conn> Statement<'conn> {
/// If the query returns more than one row, all rows except the first are /// If the query returns more than one row, all rows except the first are
/// ignored. /// ignored.
/// ///
/// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the query truly is optional, /// Returns `Err(QueryReturnedNoRows)` if no results are returned. If the
/// you can call `.optional()` on the result of this to get a `Result<Option<T>>`. /// query truly is optional, you can call `.optional()` on the result of
/// this to get a `Result<Option<T>>`.
/// ///
/// # Failure /// # Failure
/// ///