@ -1,4 +1,7 @@
use byteorder ::{ NetworkEndian , ReadBytesExt , WriteBytesExt } ;
use chrono ::DateTime ;
use chrono ::FixedOffset ;
use chrono ::NaiveDateTime ;
use model ::vocab ::rdf ;
use model ::vocab ::xsd ;
use model ::* ;
@ -62,6 +65,8 @@ const TYPE_FLOAT_LITERAL: u8 = 9;
const TYPE_DOUBLE_LITERAL : u8 = 10 ;
const TYPE_INTEGER_LITERAL : u8 = 11 ;
const TYPE_DECIMAL_LITERAL : u8 = 12 ;
const TYPE_DATE_TIME_LITERAL : u8 = 13 ;
const TYPE_NAIVE_DATE_TIME_LITERAL : u8 = 14 ;
pub static ENCODED_DEFAULT_GRAPH : EncodedTerm = EncodedTerm ::DefaultGraph { } ;
pub static ENCODED_EMPTY_SIMPLE_LITERAL : EncodedTerm = EncodedTerm ::SimpleLiteral {
@ -109,6 +114,8 @@ pub enum EncodedTerm {
DoubleLiteral ( OrderedFloat < f64 > ) ,
IntegerLiteral ( i128 ) ,
DecimalLiteral ( Decimal ) ,
DateTime ( DateTime < FixedOffset > ) ,
NaiveDateTime ( NaiveDateTime ) ,
}
impl EncodedTerm {
@ -136,7 +143,9 @@ impl EncodedTerm {
| EncodedTerm ::FloatLiteral ( _ )
| EncodedTerm ::DoubleLiteral ( _ )
| EncodedTerm ::IntegerLiteral ( _ )
| EncodedTerm ::DecimalLiteral ( _ ) = > true ,
| EncodedTerm ::DecimalLiteral ( _ )
| EncodedTerm ::DateTime ( _ )
| EncodedTerm ::NaiveDateTime ( _ ) = > true ,
_ = > false ,
}
}
@ -155,6 +164,9 @@ impl EncodedTerm {
EncodedTerm ::DoubleLiteral ( .. ) = > Some ( ENCODED_XSD_DOUBLE_NAMED_NODE ) ,
EncodedTerm ::IntegerLiteral ( .. ) = > Some ( ENCODED_XSD_INTEGER_NAMED_NODE ) ,
EncodedTerm ::DecimalLiteral ( .. ) = > Some ( ENCODED_XSD_DECIMAL_NAMED_NODE ) ,
EncodedTerm ::DateTime ( .. ) | EncodedTerm ::NaiveDateTime ( .. ) = > {
Some ( ENCODED_XSD_DATE_TIME_NAMED_NODE )
}
_ = > None ,
}
}
@ -174,6 +186,8 @@ impl EncodedTerm {
EncodedTerm ::DoubleLiteral ( _ ) = > TYPE_DOUBLE_LITERAL ,
EncodedTerm ::IntegerLiteral ( _ ) = > TYPE_INTEGER_LITERAL ,
EncodedTerm ::DecimalLiteral ( _ ) = > TYPE_DECIMAL_LITERAL ,
EncodedTerm ::DateTime ( _ ) = > TYPE_DATE_TIME_LITERAL ,
EncodedTerm ::NaiveDateTime ( _ ) = > TYPE_NAIVE_DATE_TIME_LITERAL ,
}
}
}
@ -208,6 +222,18 @@ impl From<Decimal> for EncodedTerm {
}
}
impl From < DateTime < FixedOffset > > for EncodedTerm {
fn from ( value : DateTime < FixedOffset > ) -> Self {
EncodedTerm ::DateTime ( value )
}
}
impl From < NaiveDateTime > for EncodedTerm {
fn from ( value : NaiveDateTime ) -> Self {
EncodedTerm ::NaiveDateTime ( value )
}
}
#[ derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash) ]
pub struct EncodedQuad {
pub subject : EncodedTerm ,
@ -281,6 +307,20 @@ impl<R: Read> TermReader for R {
self . read_exact ( & mut buffer ) ? ;
Ok ( EncodedTerm ::DecimalLiteral ( Decimal ::deserialize ( buffer ) ) )
}
TYPE_DATE_TIME_LITERAL = > Ok ( EncodedTerm ::DateTime ( DateTime ::from_utc (
NaiveDateTime ::from_timestamp_opt (
self . read_i64 ::< NetworkEndian > ( ) ? ,
self . read_u32 ::< NetworkEndian > ( ) ? ,
) . ok_or ( "Invalid date time serialization" ) ? ,
FixedOffset ::east_opt ( self . read_i32 ::< NetworkEndian > ( ) ? )
. ok_or ( "Invalid timezone offset" ) ? ,
) ) ) ,
TYPE_NAIVE_DATE_TIME_LITERAL = > Ok ( EncodedTerm ::NaiveDateTime (
NaiveDateTime ::from_timestamp_opt (
self . read_i64 ::< NetworkEndian > ( ) ? ,
self . read_u32 ::< NetworkEndian > ( ) ? ,
) . ok_or ( "Invalid date time serialization" ) ? ,
) ) ,
_ = > Err ( "the term buffer has an invalid type id" . into ( ) ) ,
}
}
@ -361,6 +401,15 @@ impl<R: Write> TermWriter for R {
EncodedTerm ::DoubleLiteral ( value ) = > self . write_f64 ::< NetworkEndian > ( * value ) ? ,
EncodedTerm ::IntegerLiteral ( value ) = > self . write_i128 ::< NetworkEndian > ( value ) ? ,
EncodedTerm ::DecimalLiteral ( value ) = > self . write_all ( & value . serialize ( ) ) ? ,
EncodedTerm ::DateTime ( value ) = > {
self . write_i64 ::< NetworkEndian > ( value . timestamp ( ) ) ? ;
self . write_u32 ::< NetworkEndian > ( value . timestamp_subsec_nanos ( ) ) ? ;
self . write_i32 ::< NetworkEndian > ( value . timezone ( ) . local_minus_utc ( ) ) ? ;
}
EncodedTerm ::NaiveDateTime ( value ) = > {
self . write_i64 ::< NetworkEndian > ( value . timestamp ( ) ) ? ;
self . write_u32 ::< NetworkEndian > ( value . timestamp_subsec_nanos ( ) ) ? ;
}
}
Ok ( ( ) )
}
@ -450,6 +499,16 @@ impl<S: BytesStore> Encoder<S> {
. to_decimal ( )
. ok_or_else ( | | Error ::from ( "decimal literal without decimal value" ) ) ?
. into ( )
} else if literal . is_date_time_stamp ( ) {
literal
. to_date_time_stamp ( )
. ok_or_else ( | | Error ::from ( "dateTimeStamp literal without dateTimeStamp value" ) ) ?
. into ( )
} else if literal . is_decimal ( ) {
literal
. to_date_time ( )
. ok_or_else ( | | Error ::from ( "dateTime literal without dateTime value" ) ) ?
. into ( )
} else {
EncodedTerm ::TypedLiteral {
value_id : self . encode_str_value ( & literal . value ( ) ) ? ,
@ -530,6 +589,8 @@ impl<S: BytesStore> Encoder<S> {
EncodedTerm ::DoubleLiteral ( value ) = > Ok ( Literal ::from ( * value ) . into ( ) ) ,
EncodedTerm ::IntegerLiteral ( value ) = > Ok ( Literal ::from ( value ) . into ( ) ) ,
EncodedTerm ::DecimalLiteral ( value ) = > Ok ( Literal ::from ( value ) . into ( ) ) ,
EncodedTerm ::DateTime ( value ) = > Ok ( Literal ::from ( value ) . into ( ) ) ,
EncodedTerm ::NaiveDateTime ( value ) = > Ok ( Literal ::from ( value ) . into ( ) ) ,
}
}