Skip to content

Commit fe14c82

Browse files
committed
Remove Error::InvalidColumn
Change get_opt to return an Option<Result<T>> as its name would hint
1 parent 7482f6b commit fe14c82

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ pub enum Error {
278278
/// An attempt was made to convert between incompatible Rust and Postgres
279279
/// types.
280280
WrongType(Type),
281-
/// An attempt was made to read from a column that does not exist.
282-
InvalidColumn,
283281
/// An error converting between Postgres and Rust types.
284282
Conversion(Box<error::Error + Sync + Send>),
285283
}
@@ -292,7 +290,6 @@ impl fmt::Display for Error {
292290
Error::IoError(ref err) => write!(fmt, ": {}", err),
293291
Error::WrongType(ref ty) => write!(fmt, ": saw type {:?}", ty),
294292
Error::Conversion(ref err) => write!(fmt, ": {}", err),
295-
_ => Ok(()),
296293
}
297294
}
298295
}
@@ -303,7 +300,6 @@ impl error::Error for Error {
303300
Error::DbError(_) => "Error reported by Postgres",
304301
Error::IoError(_) => "Error communicating with the server",
305302
Error::WrongType(_) => "Unexpected type",
306-
Error::InvalidColumn => "Invalid column",
307303
Error::Conversion(_) => "Error converting between Postgres and Rust types",
308304
}
309305
}

src/rows.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ impl<'a> Row<'a> {
190190
T: FromSql
191191
{
192192
match self.get_inner(&idx) {
193-
Ok(ok) => ok,
194-
Err(err) => panic!("error retrieving column {:?}: {:?}", idx, err),
193+
Some(Ok(ok)) => ok,
194+
Some(Err(err)) => panic!("error retrieving column {:?}: {:?}", idx, err),
195+
None => panic!("no such column {:?}"),
195196
}
196197
}
197198

@@ -200,29 +201,35 @@ impl<'a> Row<'a> {
200201
/// A field can be accessed by the name or index of its column, though
201202
/// access by index is more efficient. Rows are 0-indexed.
202203
///
203-
/// Returns an `Error` value if the index does not reference a column or
204-
/// the return type is not compatible with the Postgres type.
205-
pub fn get_opt<I, T>(&self, idx: I) -> Result<T>
204+
/// Returns `None` if the index does not reference a column, `Some(Err(..))`
205+
/// if there was an error converting the result value, and `Some(Ok(..))`
206+
/// on success.
207+
pub fn get_opt<I, T>(&self, idx: I) -> Option<Result<T>>
206208
where I: RowIndex,
207209
T: FromSql
208210
{
209211
self.get_inner(&idx)
210212
}
211213

212-
fn get_inner<I, T>(&self, idx: &I) -> Result<T>
214+
fn get_inner<I, T>(&self, idx: &I) -> Option<Result<T>>
213215
where I: RowIndex,
214216
T: FromSql
215217
{
216-
let idx = try!(idx.idx(self.stmt).ok_or(Error::InvalidColumn));
218+
let idx = match idx.idx(self.stmt) {
219+
Some(idx) => idx,
220+
None => return None
221+
};
222+
217223
let ty = self.stmt.columns()[idx].type_();
218224
if !<T as FromSql>::accepts(ty) {
219-
return Err(Error::WrongType(ty.clone()));
225+
return Some(Err(Error::WrongType(ty.clone())));
220226
}
221227
let conn = self.stmt.conn().conn.borrow();
222-
match self.data[idx] {
228+
let value = match self.data[idx] {
223229
Some(ref data) => FromSql::from_sql(ty, &mut &**data, &SessionInfo::new(&*conn)),
224230
None => FromSql::from_sql_null(ty, &SessionInfo::new(&*conn)),
225-
}
231+
};
232+
Some(value)
226233
}
227234

228235
/// Retrieves the specified field as a raw buffer of Postgres data.

tests/test.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ fn test_get_named_err() {
495495
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
496496
let result = or_panic!(stmt.query(&[]));
497497

498-
match result.iter().next().unwrap().get_opt::<&str, i32>("asdf") {
499-
Err(Error::InvalidColumn) => {}
498+
match result.iter().next().unwrap().get_opt::<_, i32>("asdf") {
499+
None => {}
500500
res => panic!("unexpected result {:?}", res),
501501
};
502502
}
@@ -507,8 +507,8 @@ fn test_get_was_null() {
507507
let stmt = or_panic!(conn.prepare("SELECT NULL::INT as id"));
508508
let result = or_panic!(stmt.query(&[]));
509509

510-
match result.iter().next().unwrap().get_opt::<usize, i32>(0) {
511-
Err(Error::Conversion(..)) => {}
510+
match result.iter().next().unwrap().get_opt::<_, i32>(0) {
511+
Some(Err(Error::Conversion(..))) => {}
512512
res => panic!("unexpected result {:?}", res),
513513
};
514514
}
@@ -519,8 +519,8 @@ fn test_get_off_by_one() {
519519
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
520520
let result = or_panic!(stmt.query(&[]));
521521

522-
match result.iter().next().unwrap().get_opt::<usize, i32>(1) {
523-
Err(Error::InvalidColumn) => {}
522+
match result.iter().next().unwrap().get_opt::<_, i32>(1) {
523+
None => {}
524524
res => panic!("unexpected result {:?}", res),
525525
};
526526
}
@@ -936,9 +936,10 @@ fn test_get_opt_wrong_type() {
936936
let stmt = conn.prepare("SELECT 1::INT").unwrap();
937937
let res = stmt.query(&[]).unwrap();
938938
match res.iter().next().unwrap().get_opt::<_, String>(0) {
939-
Ok(_) => panic!("unexpected success"),
940-
Err(Error::WrongType(Type::Int4)) => {}
941-
Err(e) => panic!("unexpected error {}", e),
939+
Some(Ok(_)) => panic!("unexpected success"),
940+
Some(Err(Error::WrongType(Type::Int4))) => {}
941+
Some(Err(e)) => panic!("unexpected error {}", e),
942+
None => panic!("unexpected None"),
942943
}
943944
}
944945

tests/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ fn test_pg_database_datname() {
193193
let result = or_panic!(stmt.query(&[]));
194194

195195
let next = result.iter().next().unwrap();
196-
or_panic!(next.get_opt::<usize, String>(0));
197-
or_panic!(next.get_opt::<&str, String>("datname"));
196+
or_panic!(next.get_opt::<_, String>(0).unwrap());
197+
or_panic!(next.get_opt::<_, String>("datname").unwrap());
198198
}
199199

200200
#[test]

0 commit comments

Comments
 (0)