Skip to content

Commit

Permalink
model module part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
CodesInChaos authored and tailhook committed Aug 19, 2020
1 parent fc9884b commit eb212eb
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 39 deletions.
18 changes: 9 additions & 9 deletions edgedb-protocol/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use snafu::{ensure, OptionExt, ResultExt};

use crate::descriptors::{self, Descriptor, TypePos};
use crate::errors::{self, CodecError, DecodeError, EncodeError};
use crate::value::{self, Value};
use crate::json;
use crate::value::Value;
use crate::model;

pub mod raw;

Expand Down Expand Up @@ -433,7 +433,7 @@ impl Codec for Duration {
let months = buf.get_u32();
ensure!(months == 0 && days == 0, errors::NonZeroReservedBytes);
Ok(Value::Duration(
value::Duration { micros }))
model::Duration { micros }))
}
fn encode(&self, buf: &mut BytesMut, val: &Value)
-> Result<(), EncodeError>
Expand Down Expand Up @@ -745,7 +745,7 @@ impl Codec for Decimal {
for _ in 0..ndigits {
digits.push(buf.get_u16());
}
Ok(Value::Decimal(value::Decimal {
Ok(Value::Decimal(model::Decimal {
negative, weight, decimal_digits, digits,
}))
}
Expand Down Expand Up @@ -786,7 +786,7 @@ impl Codec for BigInt {
for _ in 0..ndigits {
digits.push(buf.get_u16());
}
Ok(Value::BigInt(value::BigInt {
Ok(Value::BigInt(model::BigInt {
negative, weight, digits,
}))
}
Expand Down Expand Up @@ -878,7 +878,7 @@ impl Codec for LocalDatetime {
ensure!(buf.remaining() >= 8, errors::Underflow);
let micros = buf.get_i64();
Ok(Value::LocalDatetime(
value::LocalDatetime { micros }))
model::LocalDatetime { micros }))
}
fn encode(&self, buf: &mut BytesMut, val: &Value)
-> Result<(), EncodeError>
Expand All @@ -897,7 +897,7 @@ impl Codec for LocalDate {
fn decode(&self, buf: &mut Cursor<Buf>) -> Result<Value, DecodeError> {
ensure!(buf.remaining() >= 4, errors::Underflow);
let days = buf.get_i32();
Ok(Value::LocalDate(value::LocalDate { days }))
Ok(Value::LocalDate(model::LocalDate { days }))
}
fn encode(&self, buf: &mut BytesMut, val: &Value)
-> Result<(), EncodeError>
Expand All @@ -917,7 +917,7 @@ impl Codec for LocalTime {
ensure!(buf.remaining() >= 8, errors::Underflow);
let micros = buf.get_i64();
ensure!(micros >= 0 && micros < 86400_000_000, errors::InvalidDate);
Ok(Value::LocalTime(value::LocalTime { micros }))
Ok(Value::LocalTime(model::LocalTime { micros }))
}
fn encode(&self, buf: &mut BytesMut, val: &Value)
-> Result<(), EncodeError>
Expand All @@ -934,7 +934,7 @@ impl Codec for LocalTime {

impl Codec for Json {
fn decode(&self, buf: &mut Cursor<Buf>) -> Result<Value, DecodeError> {
let json: json::Json = raw::RawCodec::decode_raw(buf)?;
let json: model::Json = raw::RawCodec::decode_raw(buf)?;
Ok(Value::Json(json.into()))
}
fn encode(&self, buf: &mut BytesMut, val: &Value)
Expand Down
5 changes: 2 additions & 3 deletions edgedb-protocol/src/codec/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::io::Cursor;
use std::str;

use bytes::{Bytes, Buf};
use uuid::Uuid;

use crate::errors::{self, DecodeError};
use crate::json::Json;
use crate::model::{Json, Uuid};
use snafu::{ResultExt, ensure};


Expand All @@ -32,7 +31,7 @@ impl RawCodec for Json {
.context(errors::InvalidUtf8)?
.to_owned();
buf.advance(val.len());
Ok(Json(val))
Ok(Json::new_unchecked(val))
}
}

Expand Down
3 changes: 0 additions & 3 deletions edgedb-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
mod encoding;
mod common;
mod bignum;
mod time;
mod json;
pub mod client_message;
pub mod server_message;
pub mod errors;
Expand Down
7 changes: 3 additions & 4 deletions edgedb-protocol/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// turn these into sub-modules later
use crate::time;
use crate::bignum;
use crate::json;
mod bignum;
mod time;
mod json;

pub use self::time::{ LocalDatetime, LocalDate, LocalTime, Duration };
pub use self::bignum:: {BigInt, Decimal};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::Decimal;
use crate::model::OutOfRangeError;

impl std::convert::TryFrom<bigdecimal::BigDecimal> for Decimal {
type Error = crate::value::OutOfRange;
type Error = OutOfRangeError;
fn try_from(dec: bigdecimal::BigDecimal) -> Result<Decimal, Self::Error> {
use num_traits::{ToPrimitive, Zero};
use std::convert::TryInto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::BigInt;
use crate::model::OutOfRangeError;

impl std::convert::TryFrom<num_bigint::BigInt> for BigInt {
type Error = crate::value::OutOfRange;
type Error = OutOfRangeError;
fn try_from(v: num_bigint::BigInt) -> Result<BigInt, Self::Error> {
use num_traits::{ToPrimitive, Zero};
use std::convert::TryInto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#[derive(Debug, Clone)]
pub struct Json(pub(crate) String);
pub struct Json(String);

impl Json {
pub(crate) fn new_unchecked(value: String) -> Json {
Json(value)
}
}

impl AsRef<str> for Json {
fn as_ref(&self) -> &str {
Expand Down
26 changes: 13 additions & 13 deletions edgedb-protocol/src/time.rs → edgedb-protocol/src/model/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ mod test {
#[cfg(feature = "chrono")]
mod chrono_interop {
use super::{LocalDate, LocalDatetime, LocalTime};
use crate::value::OutOfRange;
use crate::model::OutOfRangeError;
use chrono::naive::{NaiveDate, NaiveDateTime, NaiveTime};

impl std::convert::TryInto<NaiveDateTime> for &LocalDatetime {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_into(self) -> Result<NaiveDateTime, Self::Error> {
NaiveDateTime::from_timestamp_opt(self.micros/1000_000,
((self.micros % 1000_000)*1000) as u32)
.ok_or(OutOfRange)
.ok_or(OutOfRangeError)
}
}

impl std::convert::TryFrom<&NaiveDateTime> for LocalDatetime {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_from(d: &NaiveDateTime)
-> Result<LocalDatetime, Self::Error>
{
Expand All @@ -109,29 +109,29 @@ mod chrono_interop {
Ok(LocalDatetime {
micros: secs.checked_mul(1_000_000)
.and_then(|x| x.checked_add(micros as i64))
.ok_or(OutOfRange)?,
.ok_or(OutOfRangeError)?,
})
}
}

impl std::convert::TryFrom<&NaiveDate> for LocalDate {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_from(d: &NaiveDate) -> Result<LocalDate, Self::Error>
{
let days = chrono::Datelike::num_days_from_ce(d);
Ok(LocalDate {
days: days.checked_sub(730120)
.ok_or(OutOfRange)?,
.ok_or(OutOfRangeError)?,
})
}
}

impl std::convert::TryInto<NaiveDate> for &LocalDate {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_into(self) -> Result<NaiveDate, Self::Error> {
self.days.checked_add(730120)
.and_then(NaiveDate::from_num_days_from_ce_opt)
.ok_or(OutOfRange)
.ok_or(OutOfRangeError)
}
}

Expand All @@ -154,21 +154,21 @@ mod chrono_interop {
}

impl std::convert::TryInto<NaiveDateTime> for LocalDatetime {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_into(self) -> Result<NaiveDateTime, Self::Error> {
(&self).try_into()
}
}

impl std::convert::TryInto<NaiveDate> for LocalDate {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_into(self) -> Result<NaiveDate, Self::Error> {
(&self).try_into()
}
}

impl std::convert::TryFrom<NaiveDate> for LocalDate {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_from(d: NaiveDate) -> Result<LocalDate, Self::Error>
{
std::convert::TryFrom::try_from(&d)
Expand All @@ -182,7 +182,7 @@ mod chrono_interop {
}

impl std::convert::TryFrom<NaiveDateTime> for LocalDatetime {
type Error = OutOfRange;
type Error = OutOfRangeError;
fn try_from(d: NaiveDateTime)
-> Result<LocalDatetime, Self::Error>
{
Expand Down
3 changes: 1 addition & 2 deletions edgedb-protocol/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::io::Cursor;

use bytes::{Bytes, Buf};
use snafu::{Snafu, ensure};
use uuid::Uuid;

use crate::errors::{self, DecodeError};
use crate::codec::raw::RawCodec;
use crate::codec;
use crate::descriptors::{Descriptor, TypePos};
use crate::json::Json;
use crate::model::{Json, Uuid};


#[derive(Snafu, Debug)]
Expand Down
3 changes: 1 addition & 2 deletions edgedb-protocol/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::time::SystemTime;
use crate::codec::{NamedTupleShape, ObjectShape, EnumValue};
// turn this private later
pub(crate) use crate::model::{ BigInt, Decimal, LocalDatetime, LocalDate, LocalTime, Duration, Uuid, OutOfRangeError as OutOfRange };
use crate::model::{ BigInt, Decimal, LocalDatetime, LocalDate, LocalTime, Duration, Uuid };

#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Expand Down

0 comments on commit eb212eb

Please sign in to comment.