From 91dc30b04d34f3e99099f93b15e56ccf47202704 Mon Sep 17 00:00:00 2001 From: gwenn Date: Mon, 30 May 2016 21:20:07 +0200 Subject: [PATCH] Simplify InvalidType --- src/error.rs | 10 ++++------ src/functions.rs | 2 +- src/lib.rs | 2 +- src/types/from_sql.rs | 2 +- src/types/serde_json.rs | 2 +- src/types/value_ref.rs | 8 ++++---- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/error.rs b/src/error.rs index f480a42..9274217 100644 --- a/src/error.rs +++ b/src/error.rs @@ -55,7 +55,7 @@ pub enum Error { /// Error when an SQLite value is requested, but the type of the result cannot be converted to the /// requested Rust type. - InvalidType(Type), + InvalidType, /// Error when a query that was expected to insert one row did not insert any or insert many. StatementChangedRows(c_int), @@ -111,9 +111,7 @@ impl fmt::Display for Error { Error::InvalidColumnType(i, ref t) => { write!(f, "Invalid column type {} at index: {}", t, i) } - Error::InvalidType(ref t) => { - write!(f, "Invalid type {}", t) - } + Error::InvalidType => write!(f, "Invalid type"), Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i), Error::StatementFailedToInsertRow => write!(f, "Statement failed to insert new row"), @@ -147,7 +145,7 @@ impl error::Error for Error { Error::InvalidColumnIndex(_) => "invalid column index", Error::InvalidColumnName(_) => "invalid column name", Error::InvalidColumnType(_, _) => "invalid column type", - Error::InvalidType(_) => "invalid type", + Error::InvalidType => "invalid type", Error::StatementChangedRows(_) => "query inserted zero or more than one row", Error::StatementFailedToInsertRow => "statement failed to insert new row", @@ -173,7 +171,7 @@ impl error::Error for Error { Error::InvalidColumnIndex(_) | Error::InvalidColumnName(_) | Error::InvalidColumnType(_, _) | - Error::InvalidType(_) | + Error::InvalidType | Error::InvalidPath(_) | Error::StatementChangedRows(_) | Error::StatementFailedToInsertRow => None, diff --git a/src/functions.rs b/src/functions.rs index 8065e1a..55e02b2 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -218,7 +218,7 @@ impl<'a> Context<'a> { let arg = self.args[idx]; let value = unsafe { ValueRef::from_value(arg) }; FromSql::column_result(value).map_err(|err| match err { - Error::InvalidType(t) => Error::InvalidFunctionParameterType(idx, t), + Error::InvalidType => Error::InvalidFunctionParameterType(idx, value.data_type()), _ => err, }) } diff --git a/src/lib.rs b/src/lib.rs index 72e9a1f..35cfca7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1067,7 +1067,7 @@ impl<'a, 'stmt> Row<'a, 'stmt> { let idx = try!(idx.idx(self.stmt)); let value = unsafe { ValueRef::new(&self.stmt.stmt, idx) }; FromSql::column_result(value).map_err(|err| match err { - Error::InvalidType(t) => Error::InvalidColumnType(idx, t), + Error::InvalidType => Error::InvalidColumnType(idx, value.data_type()), _ => err, }) } diff --git a/src/types/from_sql.rs b/src/types/from_sql.rs index 1eba548..99b3c1f 100644 --- a/src/types/from_sql.rs +++ b/src/types/from_sql.rs @@ -24,7 +24,7 @@ impl FromSql for f64 { match value { ValueRef::Integer(i) => Ok(i as f64), ValueRef::Real(f) => Ok(f), - _ => Err(Error::InvalidType(value.data_type())), + _ => Err(Error::InvalidType), } } } diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index 06d8855..c2c818d 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -23,7 +23,7 @@ impl FromSql for Value { match value { ValueRef::Text(ref s) => serde_json::from_str(s), ValueRef::Blob(ref b) => serde_json::from_slice(b), - _ => return Err(Error::InvalidType(value.data_type())), + _ => return Err(Error::InvalidType), } .map_err(|err| Error::FromSqlConversionFailure(Box::new(err))) } diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index ad5b058..e399bba 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -38,7 +38,7 @@ impl<'a> ValueRef<'a> { pub fn as_i64(&self) -> Result { match *self { ValueRef::Integer(i) => Ok(i), - _ => Err(Error::InvalidType(self.data_type())), + _ => Err(Error::InvalidType), } } @@ -47,7 +47,7 @@ impl<'a> ValueRef<'a> { pub fn as_f64(&self) -> Result { match *self { ValueRef::Real(f) => Ok(f), - _ => Err(Error::InvalidType(self.data_type())), + _ => Err(Error::InvalidType), } } @@ -56,7 +56,7 @@ impl<'a> ValueRef<'a> { pub fn as_str(&self) -> Result<&str> { match *self { ValueRef::Text(ref t) => Ok(t), - _ => Err(Error::InvalidType(self.data_type())), + _ => Err(Error::InvalidType), } } @@ -65,7 +65,7 @@ impl<'a> ValueRef<'a> { pub fn as_blob(&self) -> Result<&[u8]> { match *self { ValueRef::Blob(ref b) => Ok(b), - _ => Err(Error::InvalidType(self.data_type())), + _ => Err(Error::InvalidType), } } }