@ -11,9 +11,9 @@
use ordered_float ::OrderedFloat ;
use bincode ::{
Infinite ,
deserialize ,
serialize ,
Infinite ,
} ;
use uuid ::{
@ -30,15 +30,15 @@ use error::DataError;
#[ repr(u8) ]
#[ derive(Debug, PartialEq, Eq) ]
pub enum Type {
Bool = 1 ,
U64 = 2 ,
I64 = 3 ,
F64 = 4 ,
Instant = 5 , // Millisecond-precision timestamp.
Uuid = 6 ,
Str = 7 ,
Json = 8 ,
Blob = 9 ,
Bool = 1 ,
U64 = 2 ,
I64 = 3 ,
F64 = 4 ,
Instant = 5 , // Millisecond-precision timestamp.
Uuid = 6 ,
Str = 7 ,
Json = 8 ,
Blob = 9 ,
}
/// We use manual tagging, because <https://github.com/serde-rs/serde/issues/610>.
@ -70,15 +70,15 @@ impl Type {
impl ::std ::fmt ::Display for Type {
fn fmt ( & self , f : & mut ::std ::fmt ::Formatter ) -> Result < ( ) , ::std ::fmt ::Error > {
f . write_str ( match * self {
Type ::Bool = > "bool" ,
Type ::U64 = > "u64" ,
Type ::I64 = > "i64" ,
Type ::F64 = > "f64" ,
Type ::Bool = > "bool" ,
Type ::U64 = > "u64" ,
Type ::I64 = > "i64" ,
Type ::F64 = > "f64" ,
Type ::Instant = > "instant" ,
Type ::Uuid = > "uuid" ,
Type ::Str = > "str" ,
Type ::Json = > "json" ,
Type ::Blob = > "blob" ,
Type ::Uuid = > "uuid" ,
Type ::Str = > "str" ,
Type ::Json = > "json" ,
Type ::Blob = > "blob" ,
} )
}
}
@ -89,7 +89,7 @@ pub enum Value<'s> {
U64 ( u64 ) ,
I64 ( i64 ) ,
F64 ( OrderedFloat < f64 > ) ,
Instant ( i64 ) , // Millisecond-precision timestamp.
Instant ( i64 ) , // Millisecond-precision timestamp.
Uuid ( & ' s UuidBytes ) ,
Str ( & ' s str ) ,
Json ( & ' s str ) ,
@ -103,10 +103,10 @@ enum OwnedValue {
U64 ( u64 ) ,
I64 ( i64 ) ,
F64 ( f64 ) ,
Instant ( i64 ) , // Millisecond-precision timestamp.
Instant ( i64 ) , // Millisecond-precision timestamp.
Uuid ( Uuid ) ,
Str ( String ) ,
Json ( String ) , // TODO
Json ( String ) , // TODO
Blob ( Vec < u8 > ) ,
}
@ -123,7 +123,10 @@ impl<'s> Value<'s> {
let ( tag , data ) = slice . split_first ( ) . ok_or ( DataError ::Empty ) ? ;
let t = Type ::from_tag ( * tag ) ? ;
if t = = expected {
return Err ( DataError ::UnexpectedType { expected : expected , actual : t } ) ;
return Err ( DataError ::UnexpectedType {
expected : expected ,
actual : t ,
} ) ;
}
Value ::from_type_and_data ( t , data )
}
@ -136,68 +139,43 @@ impl<'s> Value<'s> {
fn from_type_and_data ( t : Type , data : & ' s [ u8 ] ) -> Result < Value < ' s > , DataError > {
if t = = Type ::Uuid {
return deserialize ( data ) . map_err ( | e | DataError ::DecodingError { value_type : t , err : e } )
. map ( uuid ) ? ;
return deserialize ( data )
. map_err ( | e | DataError ::DecodingError {
value_type : t ,
err : e ,
} )
. map ( uuid ) ? ;
}
match t {
Type ::Bool = > {
deserialize ( data ) . map ( Value ::Bool )
} ,
Type ::U64 = > {
deserialize ( data ) . map ( Value ::U64 )
} ,
Type ::I64 = > {
deserialize ( data ) . map ( Value ::I64 )
} ,
Type ::F64 = > {
deserialize ( data ) . map ( OrderedFloat ) . map ( Value ::F64 )
} ,
Type ::Instant = > {
deserialize ( data ) . map ( Value ::Instant )
} ,
Type ::Str = > {
deserialize ( data ) . map ( Value ::Str )
} ,
Type ::Json = > {
deserialize ( data ) . map ( Value ::Json )
} ,
Type ::Blob = > {
deserialize ( data ) . map ( Value ::Blob )
} ,
Type ::Bool = > deserialize ( data ) . map ( Value ::Bool ) ,
Type ::U64 = > deserialize ( data ) . map ( Value ::U64 ) ,
Type ::I64 = > deserialize ( data ) . map ( Value ::I64 ) ,
Type ::F64 = > deserialize ( data ) . map ( OrderedFloat ) . map ( Value ::F64 ) ,
Type ::Instant = > deserialize ( data ) . map ( Value ::Instant ) ,
Type ::Str = > deserialize ( data ) . map ( Value ::Str ) ,
Type ::Json = > deserialize ( data ) . map ( Value ::Json ) ,
Type ::Blob = > deserialize ( data ) . map ( Value ::Blob ) ,
Type ::Uuid = > {
// Processed above to avoid verbose duplication of error transforms.
unreachable! ( )
} ,
} . map_err ( | e | DataError ::DecodingError { value_type : t , err : e } )
} . map_err ( | e | DataError ::DecodingError {
value_type : t ,
err : e ,
} )
}
pub fn to_bytes ( & self ) -> Result < Vec < u8 > , DataError > {
match self {
& Value ::Bool ( ref v ) = > {
serialize ( & ( Type ::Bool . to_tag ( ) , * v ) , Infinite )
} ,
& Value ::U64 ( ref v ) = > {
serialize ( & ( Type ::U64 . to_tag ( ) , * v ) , Infinite )
} ,
& Value ::I64 ( ref v ) = > {
serialize ( & ( Type ::I64 . to_tag ( ) , * v ) , Infinite )
} ,
& Value ::F64 ( ref v ) = > {
serialize ( & ( Type ::F64 . to_tag ( ) , v . 0 ) , Infinite )
} ,
& Value ::Instant ( ref v ) = > {
serialize ( & ( Type ::Instant . to_tag ( ) , * v ) , Infinite )
} ,
& Value ::Str ( ref v ) = > {
serialize ( & ( Type ::Str . to_tag ( ) , v ) , Infinite )
} ,
& Value ::Json ( ref v ) = > {
serialize ( & ( Type ::Json . to_tag ( ) , v ) , Infinite )
} ,
& Value ::Blob ( ref v ) = > {
serialize ( & ( Type ::Blob . to_tag ( ) , v ) , Infinite )
} ,
& Value ::Bool ( ref v ) = > serialize ( & ( Type ::Bool . to_tag ( ) , * v ) , Infinite ) ,
& Value ::U64 ( ref v ) = > serialize ( & ( Type ::U64 . to_tag ( ) , * v ) , Infinite ) ,
& Value ::I64 ( ref v ) = > serialize ( & ( Type ::I64 . to_tag ( ) , * v ) , Infinite ) ,
& Value ::F64 ( ref v ) = > serialize ( & ( Type ::F64 . to_tag ( ) , v . 0 ) , Infinite ) ,
& Value ::Instant ( ref v ) = > serialize ( & ( Type ::Instant . to_tag ( ) , * v ) , Infinite ) ,
& Value ::Str ( ref v ) = > serialize ( & ( Type ::Str . to_tag ( ) , v ) , Infinite ) ,
& Value ::Json ( ref v ) = > serialize ( & ( Type ::Json . to_tag ( ) , v ) , Infinite ) ,
& Value ::Blob ( ref v ) = > serialize ( & ( Type ::Blob . to_tag ( ) , v ) , Infinite ) ,
& Value ::Uuid ( ref v ) = > {
// Processed above to avoid verbose duplication of error transforms.
serialize ( & ( Type ::Uuid . to_tag ( ) , v ) , Infinite )