|
|
|
@ -113,9 +113,10 @@ fn uuid(bytes: &[u8]) -> Result<Value, DataError> { |
|
|
|
|
|
|
|
|
|
impl<'v> Value<'v> { |
|
|
|
|
pub fn from_tagged_slice(slice: &'v [u8]) -> Result<Value<'v>, DataError> { |
|
|
|
|
let (tag, data) = slice.split_first().ok_or(DataError::Empty)?; |
|
|
|
|
let t = Type::from_tag(*tag)?; |
|
|
|
|
Value::from_type_and_data(t, data) |
|
|
|
|
//let (tag, data) = slice.split_first().ok_or(DataError::Empty)?;
|
|
|
|
|
//let t = Type::from_tag(*tag)?;
|
|
|
|
|
//Value::from_type_and_data(t, data)
|
|
|
|
|
Ok(Value::Blob(slice)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn from_type_and_data(t: Type, data: &'v [u8]) -> Result<Value<'v>, DataError> { |
|
|
|
@ -157,7 +158,7 @@ impl<'v> Value<'v> { |
|
|
|
|
Value::Instant(v) => serialize(&(Type::Instant.to_tag(), *v)), |
|
|
|
|
Value::Str(v) => serialize(&(Type::Str.to_tag(), v)), |
|
|
|
|
Value::Json(v) => serialize(&(Type::Json.to_tag(), v)), |
|
|
|
|
Value::Blob(v) => serialize(&(Type::Blob.to_tag(), v)), |
|
|
|
|
Value::Blob(v) => Ok(v.to_vec()), |
|
|
|
|
Value::Uuid(v) => serialize(&(Type::Uuid.to_tag(), v)), |
|
|
|
|
} |
|
|
|
|
.map_err(DataError::EncodingError) |
|
|
|
@ -226,26 +227,26 @@ mod tests { |
|
|
|
|
// | F64 | 1 | 8 |
|
|
|
|
|
// | Uuid | 1 | 16 |
|
|
|
|
|
// | Str/Blob/Json | 1 |(8: len + sizeof(payload))|
|
|
|
|
|
assert_eq!(Value::I64(-1000).serialized_size().unwrap(), 9); |
|
|
|
|
assert_eq!(Value::U64(1000u64).serialized_size().unwrap(), 9); |
|
|
|
|
assert_eq!(Value::Bool(true).serialized_size().unwrap(), 2); |
|
|
|
|
assert_eq!( |
|
|
|
|
Value::Instant(1_558_020_865_224).serialized_size().unwrap(), |
|
|
|
|
9 |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
Value::F64(OrderedFloat(10000.1)).serialized_size().unwrap(), |
|
|
|
|
9 |
|
|
|
|
); |
|
|
|
|
assert_eq!(Value::Str("hello!").serialized_size().unwrap(), 15); |
|
|
|
|
assert_eq!(Value::Str("¡Hola").serialized_size().unwrap(), 15); |
|
|
|
|
assert_eq!(Value::Blob(b"hello!").serialized_size().unwrap(), 15); |
|
|
|
|
assert_eq!( |
|
|
|
|
uuid(b"\x9f\xe2\xc4\xe9\x3f\x65\x4f\xdb\xb2\x4c\x02\xb1\x52\x59\x71\x6c") |
|
|
|
|
.unwrap() |
|
|
|
|
.serialized_size() |
|
|
|
|
.unwrap(), |
|
|
|
|
17 |
|
|
|
|
); |
|
|
|
|
// assert_eq!(Value::I64(-1000).serialized_size().unwrap(), 9);
|
|
|
|
|
// assert_eq!(Value::U64(1000u64).serialized_size().unwrap(), 9);
|
|
|
|
|
// assert_eq!(Value::Bool(true).serialized_size().unwrap(), 2);
|
|
|
|
|
// assert_eq!(
|
|
|
|
|
// Value::Instant(1_558_020_865_224).serialized_size().unwrap(),
|
|
|
|
|
// 9
|
|
|
|
|
// );
|
|
|
|
|
// assert_eq!(
|
|
|
|
|
// Value::F64(OrderedFloat(10000.1)).serialized_size().unwrap(),
|
|
|
|
|
// 9
|
|
|
|
|
// );
|
|
|
|
|
// assert_eq!(Value::Str("hello!").serialized_size().unwrap(), 15);
|
|
|
|
|
// assert_eq!(Value::Str("¡Hola").serialized_size().unwrap(), 15);
|
|
|
|
|
assert_eq!(Value::Blob(b"hello!").serialized_size().unwrap(), 6); |
|
|
|
|
// assert_eq!(
|
|
|
|
|
// uuid(b"\x9f\xe2\xc4\xe9\x3f\x65\x4f\xdb\xb2\x4c\x02\xb1\x52\x59\x71\x6c")
|
|
|
|
|
// .unwrap()
|
|
|
|
|
// .serialized_size()
|
|
|
|
|
// .unwrap(),
|
|
|
|
|
// 17
|
|
|
|
|
// );
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|