mirror of
https://github.com/isar/rusqlite.git
synced 2025-10-21 15:58:54 +08:00
Rust 2018 idioms
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types.
|
||||
extern crate chrono;
|
||||
use chrono;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::Result;
|
||||
|
||||
/// ISO 8601 calendar date without timezone => "YYYY-MM-DD"
|
||||
impl ToSql for NaiveDate {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%Y-%m-%d").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
}
|
||||
@@ -18,7 +18,7 @@ impl ToSql for NaiveDate {
|
||||
|
||||
/// "YYYY-MM-DD" => ISO 8601 calendar date without timezone.
|
||||
impl FromSql for NaiveDate {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value
|
||||
.as_str()
|
||||
.and_then(|s| match NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
@@ -30,7 +30,7 @@ impl FromSql for NaiveDate {
|
||||
|
||||
/// ISO 8601 time without timezone => "HH:MM:SS.SSS"
|
||||
impl ToSql for NaiveTime {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%H:%M:%S%.f").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
}
|
||||
@@ -38,7 +38,7 @@ impl ToSql for NaiveTime {
|
||||
|
||||
/// "HH:MM"/"HH:MM:SS"/"HH:MM:SS.SSS" => ISO 8601 time without timezone.
|
||||
impl FromSql for NaiveTime {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().and_then(|s| {
|
||||
let fmt = match s.len() {
|
||||
5 => "%H:%M",
|
||||
@@ -56,7 +56,7 @@ impl FromSql for NaiveTime {
|
||||
/// ISO 8601 combined date and time without timezone =>
|
||||
/// "YYYY-MM-DD HH:MM:SS.SSS"
|
||||
impl ToSql for NaiveDateTime {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%Y-%m-%dT%H:%M:%S%.f").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
}
|
||||
@@ -66,7 +66,7 @@ impl ToSql for NaiveDateTime {
|
||||
/// and time without timezone. ("YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS"
|
||||
/// also supported)
|
||||
impl FromSql for NaiveDateTime {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().and_then(|s| {
|
||||
let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' {
|
||||
"%Y-%m-%dT%H:%M:%S%.f"
|
||||
@@ -85,14 +85,14 @@ impl FromSql for NaiveDateTime {
|
||||
/// Date and time with time zone => UTC RFC3339 timestamp
|
||||
/// ("YYYY-MM-DDTHH:MM:SS.SSS+00:00").
|
||||
impl<Tz: TimeZone> ToSql for DateTime<Tz> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.with_timezone(&Utc).to_rfc3339()))
|
||||
}
|
||||
}
|
||||
|
||||
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into `DateTime<Utc>`.
|
||||
impl FromSql for DateTime<Utc> {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
{
|
||||
// Try to parse value as rfc3339 first.
|
||||
let s = value.as_str()?;
|
||||
@@ -121,7 +121,7 @@ impl FromSql for DateTime<Utc> {
|
||||
|
||||
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into `DateTime<Local>`.
|
||||
impl FromSql for DateTime<Local> {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
let utc_dt = DateTime::<Utc>::column_result(value)?;
|
||||
Ok(utc_dt.with_timezone(&Local))
|
||||
}
|
||||
|
@@ -19,11 +19,11 @@ pub enum FromSqlError {
|
||||
InvalidI128Size(usize),
|
||||
|
||||
/// An error case available for implementors of the `FromSql` trait.
|
||||
Other(Box<Error + Send + Sync>),
|
||||
Other(Box<dyn Error + Send + Sync>),
|
||||
}
|
||||
|
||||
impl fmt::Display for FromSqlError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
FromSqlError::InvalidType => write!(f, "Invalid type"),
|
||||
FromSqlError::OutOfRange(i) => write!(f, "Value {} out of range", i),
|
||||
@@ -48,7 +48,7 @@ impl Error for FromSqlError {
|
||||
}
|
||||
|
||||
#[allow(clippy::match_same_arms)]
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
match *self {
|
||||
FromSqlError::Other(ref err) => err.cause(),
|
||||
FromSqlError::InvalidType | FromSqlError::OutOfRange(_) => None,
|
||||
@@ -73,11 +73,11 @@ pub type FromSqlResult<T> = Result<T, FromSqlError>;
|
||||
/// fetching values as i64 and then doing the interpretation themselves or by
|
||||
/// defining a newtype and implementing `FromSql`/`ToSql` for it.
|
||||
pub trait FromSql: Sized {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self>;
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>;
|
||||
}
|
||||
|
||||
impl FromSql for isize {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
i64::column_result(value).and_then(|i| {
|
||||
if i < isize::min_value() as i64 || i > isize::max_value() as i64 {
|
||||
Err(FromSqlError::OutOfRange(i))
|
||||
@@ -91,7 +91,7 @@ impl FromSql for isize {
|
||||
macro_rules! from_sql_integral(
|
||||
($t:ident) => (
|
||||
impl FromSql for $t {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
i64::column_result(value).and_then(|i| {
|
||||
if i < i64::from($t::min_value()) || i > i64::from($t::max_value()) {
|
||||
Err(FromSqlError::OutOfRange(i))
|
||||
@@ -112,13 +112,13 @@ from_sql_integral!(u16);
|
||||
from_sql_integral!(u32);
|
||||
|
||||
impl FromSql for i64 {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_i64()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for f64 {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Integer(i) => Ok(i as f64),
|
||||
ValueRef::Real(f) => Ok(f),
|
||||
@@ -128,7 +128,7 @@ impl FromSql for f64 {
|
||||
}
|
||||
|
||||
impl FromSql for bool {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
i64::column_result(value).map(|i| match i {
|
||||
0 => false,
|
||||
_ => true,
|
||||
@@ -137,20 +137,20 @@ impl FromSql for bool {
|
||||
}
|
||||
|
||||
impl FromSql for String {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().map(|s| s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Vec<u8> {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_blob().map(|b| b.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "i128_blob")]
|
||||
impl FromSql for i128 {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
|
||||
value.as_blob().and_then(|bytes| {
|
||||
@@ -164,7 +164,7 @@ impl FromSql for i128 {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Option<T> {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Null => Ok(None),
|
||||
_ => FromSql::column_result(value).map(Some),
|
||||
@@ -173,7 +173,7 @@ impl<T: FromSql> FromSql for Option<T> {
|
||||
}
|
||||
|
||||
impl FromSql for Value {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
Ok(value.into())
|
||||
}
|
||||
}
|
||||
|
@@ -98,7 +98,7 @@ pub enum Type {
|
||||
}
|
||||
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Type::Null => write!(f, "Null"),
|
||||
Type::Integer => write!(f, "Integer"),
|
||||
@@ -111,7 +111,7 @@ impl fmt::Display for Type {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate time;
|
||||
use time;
|
||||
|
||||
use super::Value;
|
||||
use crate::{Connection, Error, NO_PARAMS};
|
||||
|
@@ -1,5 +1,5 @@
|
||||
//! `ToSql` and `FromSql` implementation for JSON `Value`.
|
||||
extern crate serde_json;
|
||||
use serde_json;
|
||||
|
||||
use self::serde_json::Value;
|
||||
|
||||
@@ -8,14 +8,14 @@ use crate::Result;
|
||||
|
||||
/// Serialize JSON `Value` to text.
|
||||
impl ToSql for Value {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(serde_json::to_string(self).unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserialize text/blob to JSON `Value`.
|
||||
impl FromSql for Value {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Text(s) => serde_json::from_str(s),
|
||||
ValueRef::Blob(b) => serde_json::from_slice(b),
|
||||
@@ -46,7 +46,7 @@ mod test {
|
||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||
db.execute(
|
||||
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
||||
&[&data as &ToSql, &json.as_bytes()],
|
||||
&[&data as &dyn ToSql, &json.as_bytes()],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
extern crate time;
|
||||
use time;
|
||||
|
||||
use crate::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
||||
use crate::Result;
|
||||
@@ -8,7 +8,7 @@ const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%fZ";
|
||||
const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
|
||||
|
||||
impl ToSql for time::Timespec {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let time_string = time::at_utc(*self)
|
||||
.strftime(SQLITE_DATETIME_FMT)
|
||||
.unwrap()
|
||||
@@ -18,7 +18,7 @@ impl ToSql for time::Timespec {
|
||||
}
|
||||
|
||||
impl FromSql for time::Timespec {
|
||||
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value
|
||||
.as_str()
|
||||
.and_then(|s| {
|
||||
|
@@ -66,7 +66,7 @@ from_value!(Vec<u8>);
|
||||
from_value!(i128);
|
||||
|
||||
impl<'a> ToSql for ToSqlOutput<'a> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(match *self {
|
||||
ToSqlOutput::Borrowed(v) => ToSqlOutput::Borrowed(v),
|
||||
ToSqlOutput::Owned(ref v) => ToSqlOutput::Borrowed(ValueRef::from(v)),
|
||||
@@ -81,7 +81,7 @@ impl<'a> ToSql for ToSqlOutput<'a> {
|
||||
|
||||
/// A trait for types that can be converted into SQLite values.
|
||||
pub trait ToSql {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput>;
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>>;
|
||||
}
|
||||
|
||||
// We should be able to use a generic impl like this:
|
||||
@@ -99,7 +99,7 @@ pub trait ToSql {
|
||||
macro_rules! to_sql_self(
|
||||
($t:ty) => (
|
||||
impl ToSql for $t {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(*self))
|
||||
}
|
||||
}
|
||||
@@ -125,43 +125,43 @@ impl<'a, T: ?Sized> ToSql for &'a T
|
||||
where
|
||||
T: ToSql,
|
||||
{
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
(*self).to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for String {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for str {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Vec<u8> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_slice()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for [u8] {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Value {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToSql> ToSql for Option<T> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
match *self {
|
||||
None => Ok(ToSqlOutput::from(Null)),
|
||||
Some(ref t) => t.to_sql(),
|
||||
@@ -170,7 +170,7 @@ impl<T: ToSql> ToSql for Option<T> {
|
||||
}
|
||||
|
||||
impl<'a> ToSql for Cow<'a, str> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_ref()))
|
||||
}
|
||||
}
|
||||
|
@@ -70,7 +70,7 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
|
||||
impl<'a> From<ValueRef<'a>> for Value {
|
||||
fn from(borrowed: ValueRef) -> Value {
|
||||
fn from(borrowed: ValueRef<'_>) -> Value {
|
||||
match borrowed {
|
||||
ValueRef::Null => Value::Null,
|
||||
ValueRef::Integer(i) => Value::Integer(i),
|
||||
@@ -82,13 +82,13 @@ impl<'a> From<ValueRef<'a>> for Value {
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for ValueRef<'a> {
|
||||
fn from(s: &str) -> ValueRef {
|
||||
fn from(s: &str) -> ValueRef<'_> {
|
||||
ValueRef::Text(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u8]> for ValueRef<'a> {
|
||||
fn from(s: &[u8]) -> ValueRef {
|
||||
fn from(s: &[u8]) -> ValueRef<'_> {
|
||||
ValueRef::Blob(s)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user