mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Allow optionally passing an Error parameter to rusqlite::Result
(#678)
This commit is contained in:
parent
498b8550ae
commit
d35dd0e99f
12
src/lib.rs
12
src/lib.rs
@ -204,7 +204,7 @@ macro_rules! named_params {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A typedef of the result returned by many methods.
|
/// A typedef of the result returned by many methods.
|
||||||
pub type Result<T> = result::Result<T, Error>;
|
pub type Result<T, E = Error> = result::Result<T, E>;
|
||||||
|
|
||||||
/// See the [method documentation](#tymethod.optional).
|
/// See the [method documentation](#tymethod.optional).
|
||||||
pub trait OptionalExtension<T> {
|
pub trait OptionalExtension<T> {
|
||||||
@ -602,11 +602,11 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||||
/// or if the underlying SQLite call fails.
|
/// or if the underlying SQLite call fails.
|
||||||
pub fn query_row_and_then<T, E, P, F>(&self, sql: &str, params: P, f: F) -> result::Result<T, E>
|
pub fn query_row_and_then<T, E, P, F>(&self, sql: &str, params: P, f: F) -> Result<T, E>
|
||||||
where
|
where
|
||||||
P: IntoIterator,
|
P: IntoIterator,
|
||||||
P::Item: ToSql,
|
P::Item: ToSql,
|
||||||
F: FnOnce(&Row<'_>) -> result::Result<T, E>,
|
F: FnOnce(&Row<'_>) -> Result<T, E>,
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
{
|
{
|
||||||
let mut stmt = self.prepare(sql)?;
|
let mut stmt = self.prepare(sql)?;
|
||||||
@ -647,7 +647,7 @@ impl Connection {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if the underlying SQLite call fails.
|
/// Will return `Err` if the underlying SQLite call fails.
|
||||||
pub fn close(self) -> std::result::Result<(), (Connection, Error)> {
|
pub fn close(self) -> Result<(), (Connection, Error)> {
|
||||||
self.flush_prepared_statement_cache();
|
self.flush_prepared_statement_cache();
|
||||||
let r = self.db.borrow_mut().close();
|
let r = self.db.borrow_mut().close();
|
||||||
r.map_err(move |err| (self, err))
|
r.map_err(move |err| (self, err))
|
||||||
@ -1492,7 +1492,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for CustomError {
|
impl fmt::Display for CustomError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> ::std::result::Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
CustomError::SomeError => write!(f, "my custom error"),
|
CustomError::SomeError => write!(f, "my custom error"),
|
||||||
CustomError::Sqlite(ref se) => write!(f, "my custom error: {}", se),
|
CustomError::Sqlite(ref se) => write!(f, "my custom error: {}", se),
|
||||||
@ -1519,7 +1519,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomResult<T> = ::std::result::Result<T, CustomError>;
|
type CustomResult<T> = Result<T, CustomError>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_and_then() {
|
fn test_query_and_then() {
|
||||||
|
10
src/row.rs
10
src/row.rs
@ -1,6 +1,6 @@
|
|||||||
use fallible_iterator::FallibleIterator;
|
use fallible_iterator::FallibleIterator;
|
||||||
use fallible_streaming_iterator::FallibleStreamingIterator;
|
use fallible_streaming_iterator::FallibleStreamingIterator;
|
||||||
use std::{convert, result};
|
use std::convert;
|
||||||
|
|
||||||
use super::{Error, Result, Statement};
|
use super::{Error, Result, Statement};
|
||||||
use crate::types::{FromSql, FromSqlError, ValueRef};
|
use crate::types::{FromSql, FromSqlError, ValueRef};
|
||||||
@ -56,7 +56,7 @@ impl<'stmt> Rows<'stmt> {
|
|||||||
/// `FallibleStreamingIterator`).
|
/// `FallibleStreamingIterator`).
|
||||||
pub fn and_then<F, T, E>(self, f: F) -> AndThenRows<'stmt, F>
|
pub fn and_then<F, T, E>(self, f: F) -> AndThenRows<'stmt, F>
|
||||||
where
|
where
|
||||||
F: FnMut(&Row<'_>) -> result::Result<T, E>,
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
||||||
{
|
{
|
||||||
AndThenRows { rows: self, map: f }
|
AndThenRows { rows: self, map: f }
|
||||||
}
|
}
|
||||||
@ -143,7 +143,7 @@ pub struct AndThenRows<'stmt, F> {
|
|||||||
|
|
||||||
impl<'stmt, T, E, F> AndThenRows<'stmt, F>
|
impl<'stmt, T, E, F> AndThenRows<'stmt, F>
|
||||||
where
|
where
|
||||||
F: FnMut(&Row<'_>) -> result::Result<T, E>,
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(rows: Rows<'stmt>, f: F) -> AndThenRows<'stmt, F> {
|
pub(crate) fn new(rows: Rows<'stmt>, f: F) -> AndThenRows<'stmt, F> {
|
||||||
AndThenRows { rows, map: f }
|
AndThenRows { rows, map: f }
|
||||||
@ -153,9 +153,9 @@ where
|
|||||||
impl<T, E, F> Iterator for AndThenRows<'_, F>
|
impl<T, E, F> Iterator for AndThenRows<'_, F>
|
||||||
where
|
where
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
F: FnMut(&Row<'_>) -> result::Result<T, E>,
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
||||||
{
|
{
|
||||||
type Item = result::Result<T, E>;
|
type Item = Result<T, E>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let map = &mut self.map;
|
let map = &mut self.map;
|
||||||
|
@ -3,7 +3,7 @@ use std::os::raw::{c_int, c_void};
|
|||||||
#[cfg(feature = "array")]
|
#[cfg(feature = "array")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::slice::from_raw_parts;
|
use std::slice::from_raw_parts;
|
||||||
use std::{convert, fmt, mem, ptr, result, str};
|
use std::{convert, fmt, mem, ptr, str};
|
||||||
|
|
||||||
use super::ffi;
|
use super::ffi;
|
||||||
use super::{len_as_c_int, str_for_sqlite, str_to_cstring};
|
use super::{len_as_c_int, str_for_sqlite, str_to_cstring};
|
||||||
@ -284,7 +284,7 @@ impl Statement<'_> {
|
|||||||
P: IntoIterator,
|
P: IntoIterator,
|
||||||
P::Item: ToSql,
|
P::Item: ToSql,
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
F: FnMut(&Row<'_>) -> result::Result<T, E>,
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
||||||
{
|
{
|
||||||
let rows = self.query(params)?;
|
let rows = self.query(params)?;
|
||||||
Ok(AndThenRows::new(rows, f))
|
Ok(AndThenRows::new(rows, f))
|
||||||
@ -335,7 +335,7 @@ impl Statement<'_> {
|
|||||||
) -> Result<AndThenRows<'_, F>>
|
) -> Result<AndThenRows<'_, F>>
|
||||||
where
|
where
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
F: FnMut(&Row<'_>) -> result::Result<T, E>,
|
F: FnMut(&Row<'_>) -> Result<T, E>,
|
||||||
{
|
{
|
||||||
let rows = self.query_named(params)?;
|
let rows = self.query_named(params)?;
|
||||||
Ok(AndThenRows::new(rows, f))
|
Ok(AndThenRows::new(rows, f))
|
||||||
|
@ -24,7 +24,6 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::os::raw::c_int;
|
use std::os::raw::c_int;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::result;
|
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use crate::ffi;
|
use crate::ffi;
|
||||||
@ -70,7 +69,7 @@ struct CSVTab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CSVTab {
|
impl CSVTab {
|
||||||
fn reader(&self) -> result::Result<csv::Reader<File>, csv::Error> {
|
fn reader(&self) -> Result<csv::Reader<File>, csv::Error> {
|
||||||
csv::ReaderBuilder::new()
|
csv::ReaderBuilder::new()
|
||||||
.has_headers(self.has_headers)
|
.has_headers(self.has_headers)
|
||||||
.delimiter(self.delimiter)
|
.delimiter(self.delimiter)
|
||||||
|
Loading…
Reference in New Issue
Block a user