Merge remote-tracking branch 'upstream/master' into time-fmt

This commit is contained in:
gwenn
2021-07-30 21:24:51 +02:00
39 changed files with 4641 additions and 3205 deletions

View File

@@ -15,15 +15,17 @@ pub enum FromSqlError {
/// requested type.
OutOfRange(i64),
/// `feature = "i128_blob"` Error returned when reading an `i128` from a
/// Error returned when reading an `i128` from a
/// blob with a size other than 16. Only available when the `i128_blob`
/// feature is enabled.
#[cfg(feature = "i128_blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
InvalidI128Size(usize),
/// `feature = "uuid"` Error returned when reading a `uuid` from a blob with
/// Error returned when reading a `uuid` from a blob with
/// a size other than 16. Only available when the `uuid` feature is enabled.
#[cfg(feature = "uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
InvalidUuidSize(usize),
/// An error case available for implementors of the [`FromSql`] trait.
@@ -176,6 +178,7 @@ impl FromSql for Vec<u8> {
}
#[cfg(feature = "i128_blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
impl FromSql for i128 {
#[inline]
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
@@ -192,6 +195,7 @@ impl FromSql for i128 {
}
#[cfg(feature = "uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
impl FromSql for uuid::Uuid {
#[inline]
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {

View File

@@ -77,14 +77,18 @@ pub use self::value_ref::ValueRef;
use std::fmt;
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
mod chrono;
mod from_sql;
#[cfg(feature = "serde_json")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_json")))]
mod serde_json;
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
mod time;
mod to_sql;
#[cfg(feature = "url")]
#[cfg_attr(docsrs, doc(cfg(feature = "url")))]
mod url;
mod value;
mod value_ref;

View File

@@ -17,12 +17,8 @@ impl ToSql for Value {
impl FromSql for Value {
#[inline]
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Text(s) => serde_json::from_slice(s),
ValueRef::Blob(b) => serde_json::from_slice(b),
_ => return Err(FromSqlError::InvalidType),
}
.map_err(|err| FromSqlError::Other(Box::new(err)))
let bytes = value.as_bytes()?;
serde_json::from_slice(bytes).map_err(|err| FromSqlError::Other(Box::new(err)))
}
}

View File

@@ -16,13 +16,15 @@ pub enum ToSqlOutput<'a> {
/// An owned SQLite-representable value.
Owned(Value),
/// `feature = "blob"` A BLOB of the given length that is filled with
/// A BLOB of the given length that is filled with
/// zeroes.
#[cfg(feature = "blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "blob")))]
ZeroBlob(i32),
/// `feature = "array"`
#[cfg(feature = "array")]
#[cfg_attr(docsrs, doc(cfg(feature = "array")))]
Array(Array),
}
@@ -70,9 +72,11 @@ from_value!(Vec<u8>);
// `i128` needs in `Into<Value>`, but it's probably fine for the moment, and not
// worth adding another case to Value.
#[cfg(feature = "i128_blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
from_value!(i128);
#[cfg(feature = "uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
from_value!(uuid::Uuid);
impl ToSql for ToSqlOutput<'_> {
@@ -162,9 +166,11 @@ to_sql_self!(f32);
to_sql_self!(f64);
#[cfg(feature = "i128_blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
to_sql_self!(i128);
#[cfg(feature = "uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
to_sql_self!(uuid::Uuid);
macro_rules! to_sql_self_fallible(

View File

@@ -41,6 +41,7 @@ impl From<isize> for Value {
}
#[cfg(feature = "i128_blob")]
#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
impl From<i128> for Value {
#[inline]
fn from(i: i128) -> Value {
@@ -54,6 +55,7 @@ impl From<i128> for Value {
}
#[cfg(feature = "uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
impl From<uuid::Uuid> for Value {
#[inline]
fn from(id: uuid::Uuid) -> Value {

View File

@@ -77,6 +77,16 @@ impl<'a> ValueRef<'a> {
_ => Err(FromSqlError::InvalidType),
}
}
/// Returns the byte slice that makes up this ValueRef if it's either
/// [`ValueRef::Blob`] or [`ValueRef::Text`].
#[inline]
pub fn as_bytes(&self) -> FromSqlResult<&'a [u8]> {
match self {
ValueRef::Text(s) | ValueRef::Blob(s) => Ok(s),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl From<ValueRef<'_>> for Value {