cargo fmt

This commit is contained in:
Vlad Stepanov
2020-03-01 00:05:14 +03:00
parent 084add9cdb
commit 1975486baa
5 changed files with 55 additions and 57 deletions

View File

@@ -1,7 +1,7 @@
use std::convert::TryFrom;
use byteorder::{ByteOrder, LittleEndian};
use time::{Date, Time, PrimitiveDateTime, OffsetDateTime, UtcOffset};
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
use crate::decode::{Decode, DecodeError};
use crate::encode::Encode;
@@ -127,12 +127,7 @@ impl Encode<MySql> for PrimitiveDateTime {
fn size_hint(&self) -> usize {
// to save space the packet can be compressed:
match (
self.hour(),
self.minute(),
self.second(),
self.nanosecond(),
) {
match (self.hour(), self.minute(), self.second(), self.nanosecond()) {
// if hour, minutes, seconds and micro_seconds are all 0,
// length is 4 and no other field is sent
(0, 0, 0, 0) => 5,
@@ -177,7 +172,8 @@ fn decode_date(buf: &[u8]) -> Result<Date, DecodeError> {
LittleEndian::read_u16(buf) as i32,
buf[2] as u8,
buf[3] as u8,
).map_err(|e| DecodeError::Message(Box::new(format!("Error while decoding Date: {}", e))))
)
.map_err(|e| DecodeError::Message(Box::new(format!("Error while decoding Date: {}", e))))
}
fn encode_time(time: &Time, include_micros: bool, buf: &mut Vec<u8>) {
@@ -202,12 +198,8 @@ fn decode_time(len: u8, mut buf: &[u8]) -> Result<Time, DecodeError> {
0
};
Time::try_from_hms_micro(
hour,
minute,
seconds,
micros as u32,
).map_err(|e| DecodeError::Message(Box::new(format!("Time out of range for MySQL: {}", e))))
Time::try_from_hms_micro(hour, minute, seconds, micros as u32)
.map_err(|e| DecodeError::Message(Box::new(format!("Time out of range for MySQL: {}", e))))
}
#[cfg(test)]
@@ -218,28 +210,19 @@ fn test_encode_date_time() {
let mut buf = Vec::new();
// test values from https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
let date = PrimitiveDateTime::new(
date!(2010-10-17),
time!(19:27:30.000001),
);
let date = PrimitiveDateTime::new(date!(2010 - 10 - 17), time!(19:27:30.000001));
Encode::<MySql>::encode(&date, &mut buf);
assert_eq!(*buf, [11, 218, 7, 10, 17, 19, 27, 30, 1, 0, 0, 0]);
buf.clear();
let date = PrimitiveDateTime::new(
date!(2010-10-17),
time!(19:27:30),
);
let date = PrimitiveDateTime::new(date!(2010 - 10 - 17), time!(19:27:30));
Encode::<MySql>::encode(&date, &mut buf);
assert_eq!(*buf, [7, 218, 7, 10, 17, 19, 27, 30]);
buf.clear();
let date = PrimitiveDateTime::new(
date!(2010-10-17),
time!(00:00:00),
);
let date = PrimitiveDateTime::new(date!(2010 - 10 - 17), time!(00:00:00));
Encode::<MySql>::encode(&date, &mut buf);
assert_eq!(*buf, [4, 218, 7, 10, 17]);
}
@@ -263,7 +246,7 @@ fn test_decode_date_time() {
#[test]
fn test_encode_date() {
let mut buf = Vec::new();
let date: Date = date!(2010-10-17);
let date: Date = date!(2010 - 10 - 17);
Encode::<MySql>::encode(&date, &mut buf);
assert_eq!(*buf, [4, 218, 7, 10, 17]);
}
@@ -272,5 +255,5 @@ fn test_encode_date() {
fn test_decode_date() {
let buf = [4, 218, 7, 10, 17];
let date = <Date as Decode<MySql>>::decode(&buf).unwrap();
assert_eq!(date, date!(2010-10-17));
assert_eq!(date, date!(2010 - 10 - 17));
}

View File

@@ -1,7 +1,7 @@
use std::convert::TryInto;
use std::mem;
use time::{date, offset, Date, Time, PrimitiveDateTime, OffsetDateTime, NumericalDuration};
use time::{date, offset, Date, NumericalDuration, OffsetDateTime, PrimitiveDateTime, Time};
use crate::decode::{Decode, DecodeError};
use crate::encode::Encode;
@@ -10,7 +10,7 @@ use crate::postgres::types::PgTypeInfo;
use crate::postgres::Postgres;
use crate::types::HasSqlType;
const POSTGRES_EPOCH: PrimitiveDateTime = date!(2000-1-1).midnight();
const POSTGRES_EPOCH: PrimitiveDateTime = date!(2000 - 1 - 1).midnight();
impl HasSqlType<Time> for Postgres {
fn type_info() -> PgTypeInfo {
@@ -77,7 +77,8 @@ fn from_nanoseconds_since_midnight(mut microsecond: u64) -> Result<Time, DecodeE
(microsecond / 1_000_000 / 60 % 60) as u8,
(microsecond / 1_000_000 % 60) as u8,
(microsecond % 1_000_000) as u32,
).map_err(|e| DecodeError::Message(Box::new(format!("Time out of range for Postgres: {}", e))))
)
.map_err(|e| DecodeError::Message(Box::new(format!("Time out of range for Postgres: {}", e))))
}
impl Decode<Postgres> for Time {
@@ -104,13 +105,13 @@ impl Decode<Postgres> for Date {
fn decode(raw: &[u8]) -> Result<Self, DecodeError> {
let n: i32 = Decode::<Postgres>::decode(raw)?;
Ok(date!(2000-1-1) + (n as i64).days())
Ok(date!(2000 - 1 - 1) + (n as i64).days())
}
}
impl Encode<Postgres> for Date {
fn encode(&self, buf: &mut Vec<u8>) {
let days: i32 = (*self - date!(2000-1-1))
let days: i32 = (*self - date!(2000 - 1 - 1))
.whole_days()
.try_into()
// TODO: How does Diesel handle this?
@@ -185,9 +186,8 @@ fn test_encode_datetime() {
buf.clear();
// some random date
let date = PrimitiveDateTime::new(date!(2019-12-11), time!(11:01:05));
let expected = (date - POSTGRES_EPOCH)
.whole_microseconds() as i64;
let date = PrimitiveDateTime::new(date!(2019 - 12 - 11), time!(11:01:05));
let expected = (date - POSTGRES_EPOCH).whole_microseconds() as i64;
Encode::<Postgres>::encode(&date, &mut buf);
assert_eq!(buf, expected.to_be_bytes());
buf.clear();
@@ -197,18 +197,26 @@ fn test_encode_datetime() {
fn test_decode_datetime() {
let buf = [0u8; 8];
let date: PrimitiveDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2000-01-01), time!(00:00:00)));
assert_eq!(
date,
PrimitiveDateTime::new(date!(2000 - 01 - 01), time!(00:00:00))
);
let buf = 3_600_000_000i64.to_be_bytes();
let date: PrimitiveDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2000-01-01), time!(01:00:00)));
assert_eq!(
date,
PrimitiveDateTime::new(date!(2000 - 01 - 01), time!(01:00:00))
);
let buf = 629_377_265_000_000i64.to_be_bytes();
let date: PrimitiveDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2019-12-11), time!(11:01:05)));
assert_eq!(
date,
PrimitiveDateTime::new(date!(2019 - 12 - 11), time!(11:01:05))
);
}
#[test]
fn test_encode_offsetdatetime() {
let mut buf = Vec::new();
@@ -224,11 +232,9 @@ fn test_encode_offsetdatetime() {
buf.clear();
// some random date in MSK
let date = PrimitiveDateTime::new(
date!(2019-12-11), time!(11:01:05)
).assume_offset(offset!(+3));
let expected = (date - POSTGRES_EPOCH.assume_utc())
.whole_microseconds() as i64;
let date =
PrimitiveDateTime::new(date!(2019 - 12 - 11), time!(11:01:05)).assume_offset(offset!(+3));
let expected = (date - POSTGRES_EPOCH.assume_utc()).whole_microseconds() as i64;
Encode::<Postgres>::encode(&date, &mut buf);
assert_eq!(buf, expected.to_be_bytes());
buf.clear();
@@ -238,33 +244,42 @@ fn test_encode_offsetdatetime() {
fn test_decode_offsetdatetime() {
let buf = [0u8; 8];
let date: OffsetDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2000-01-01), time!(00:00:00)).assume_utc());
assert_eq!(
date,
PrimitiveDateTime::new(date!(2000 - 01 - 01), time!(00:00:00)).assume_utc()
);
let buf = 3_600_000_000i64.to_be_bytes();
let date: OffsetDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2000-01-01), time!(01:00:00)).assume_utc());
assert_eq!(
date,
PrimitiveDateTime::new(date!(2000 - 01 - 01), time!(01:00:00)).assume_utc()
);
let buf = 629_377_265_000_000i64.to_be_bytes();
let date: OffsetDateTime = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, PrimitiveDateTime::new(date!(2019-12-11), time!(11:01:05)).assume_utc());
assert_eq!(
date,
PrimitiveDateTime::new(date!(2019 - 12 - 11), time!(11:01:05)).assume_utc()
);
}
#[test]
fn test_encode_date() {
let mut buf = Vec::new();
let date = date!(2000-1-1);
let date = date!(2000 - 1 - 1);
Encode::<Postgres>::encode(&date, &mut buf);
assert_eq!(buf, [0; 4]);
buf.clear();
let date = date!(2001-1-1);
let date = date!(2001 - 1 - 1);
Encode::<Postgres>::encode(&date, &mut buf);
// 2000 was a leap year
assert_eq!(buf, 366i32.to_be_bytes());
buf.clear();
let date = date!(2019-12-11);
let date = date!(2019 - 12 - 11);
Encode::<Postgres>::encode(&date, &mut buf);
assert_eq!(buf, 7284i32.to_be_bytes());
buf.clear();
@@ -274,13 +289,13 @@ fn test_encode_date() {
fn test_decode_date() {
let buf = [0; 4];
let date: Date = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, date!(2000-01-01));
assert_eq!(date, date!(2000 - 01 - 01));
let buf = 366i32.to_be_bytes();
let date: Date = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, date!(2001-01-01));
assert_eq!(date, date!(2001 - 01 - 01));
let buf = 7284i32.to_be_bytes();
let date: Date = Decode::<Postgres>::decode(&buf).unwrap();
assert_eq!(date, date!(2019-12-11));
assert_eq!(date, date!(2019 - 12 - 11));
}

View File

@@ -17,7 +17,7 @@ pub mod chrono {
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub mod time {
pub use time::{Date, Time, PrimitiveDateTime, OffsetDateTime, UtcOffset};
pub use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
}
pub trait TypeInfo: Debug + Display + Clone {

View File

@@ -1,4 +1,4 @@
use sqlx::types::time::{OffsetDateTime, Date, Time, UtcOffset};
use sqlx::types::time::{Date, OffsetDateTime, Time, UtcOffset};
use sqlx::{mysql::MySqlConnection, Connection, Row};
async fn connect() -> anyhow::Result<MySqlConnection> {

View File

@@ -1,4 +1,4 @@
use sqlx::types::time::{OffsetDateTime, Date, Time, UtcOffset};
use sqlx::types::time::{Date, OffsetDateTime, Time, UtcOffset};
use sqlx::{Connection, PgConnection, Row};
async fn connect() -> anyhow::Result<PgConnection> {