1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{AttrVal, LogicalTime, Nanoseconds, TimelineId};
use minicbor::{data::Tag, decode, encode, Decode, Decoder, Encode, Encoder};
use uuid::Uuid;
pub const TAG_NS: Tag = Tag::Unassigned(40000);
pub const TAG_LOGICAL_TIME: Tag = Tag::Unassigned(40001);
pub const TAG_TIMELINE_ID: Tag = Tag::Unassigned(40002);
impl Encode for Nanoseconds {
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
e.tag(TAG_NS)?.u64(self.get_raw())?;
Ok(())
}
}
impl Encode for LogicalTime {
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
e.tag(TAG_LOGICAL_TIME)?.encode(self.get_raw())?;
Ok(())
}
}
impl Encode for TimelineId {
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
e.tag(TAG_TIMELINE_ID)?.bytes(self.get_raw().as_bytes())?;
Ok(())
}
}
impl Encode for AttrVal {
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
match self {
AttrVal::String(s) => {
e.str(s.as_str())?;
}
AttrVal::Integer(i) => {
e.i64(*i)?;
}
AttrVal::BigInt(bi) => {
if **bi >= 0i128 {
e.tag(Tag::PosBignum)?.bytes(&bi.to_be_bytes())?;
} else {
e.tag(Tag::NegBignum)?.bytes(&((-1 - **bi).to_be_bytes()))?;
}
}
AttrVal::Float(f) => {
e.f64(**f)?;
}
AttrVal::Bool(b) => {
e.bool(*b)?;
}
AttrVal::Timestamp(ns) => {
ns.encode(e)?;
}
AttrVal::LogicalTime(lt) => {
lt.encode(e)?;
}
AttrVal::TimelineId(tid) => {
tid.encode(e)?;
}
}
Ok(())
}
}
impl<'b> Decode<'b> for TimelineId {
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
let t = d.tag()?;
if t != TAG_TIMELINE_ID {
return Err(decode::Error::Message("Expected TAG_TIMELINE_ID"));
}
Uuid::from_slice(d.bytes()?)
.map(Into::into)
.map_err(|_uuid_err| decode::Error::Message("Error decoding uuid for TimelineId"))
}
}