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
use crate::serde::DatumType;
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};

mod deser;
mod ser;
mod serde_test;
pub use deser::*;
pub use ser::*;

#[derive(PartialEq, Eq, Debug)]
pub enum Datum {
    I64(i64),
    Bytes(Vec<u8>),
    Str(String),
    Tuple(Vec<Datum>),
}
impl PartialOrd for Datum {
    fn partial_cmp(&self, other: &Datum) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Datum {
    fn cmp(&self, other: &Datum) -> Ordering {
        match (self, other) {
            (Self::Bytes(slf), Self::Bytes(oth)) => slf.cmp(oth),
            (Self::I64(slf), Self::I64(oth)) => slf.cmp(oth),
            (Self::Str(slf), Self::Str(oth)) => slf.cmp(oth),
            (Self::Tuple(slf), Self::Tuple(oth)) => slf.cmp(oth),
            _ => DatumType::from(self).cmp(&DatumType::from(other)),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum OptDatum<T> {
    Tombstone,
    Some(T),
}
impl<T> From<Option<T>> for OptDatum<T> {
    fn from(opt: Option<T>) -> Self {
        match opt {
            None => OptDatum::Tombstone,
            Some(t) => OptDatum::Some(t),
        }
    }
}
impl<T> From<OptDatum<T>> for Option<T> {
    fn from(optdat: OptDatum<T>) -> Option<T> {
        match optdat {
            OptDatum::Tombstone => None,
            OptDatum::Some(t) => Some(t),
        }
    }
}