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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use ordered_float::OrderedFloat;

pub mod convenience;

pub mod conversions;
pub use conversions::ConversionError;

pub mod dict;
pub use dict::{Dict, DictKey};

mod util {
    #[cfg(target_pointer_width = "32")]
    #[path = "small_boxed_slice_32bit.rs"]
    pub mod small_boxed_slice;
    #[cfg(target_pointer_width = "64")]
    pub mod small_boxed_slice;
    pub mod small_boxed_string;
    pub mod trunc_8_bytes;
}

use util::small_boxed_slice::SmallBoxedSlice;
use util::small_boxed_string::SmallBoxedStr;

mod io {
    pub mod read;
    pub mod read_async;
    pub mod write;
}
pub use io::read::ReadError;

pub mod json;
pub mod serde;

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(align(16))]
pub enum Litl {
    String(SmallBoxedStr),
    Number(OrderedFloat<f64>),
    Array(SmallBoxedSlice<Litl>),
    Dict(Dict),
    Bool(bool),
    Null,
    TaggedData(TaggedData),
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum TaggedData {
    Leaf (SmallBoxedSlice<u8>),
    // inner must be TaggedData again, but for ease of reference, we use Litl
    Nested {
        tag: SmallBoxedStr,
        inner: Box<Litl>,
    },
}

#[rustfmt::skip]
#[allow(clippy::unusual_byte_groupings)]
mod consts {
    pub const FALSE_VALUE: u8 = 0b_0000_0000; // 0x00
    pub const TRUE_VALUE:  u8 = 0b_0000_0001; // 0x01
    pub const NULL_VALUE:  u8 = 0b_0000_0010; // 0x02
    // ASCII invisible          0b_0000_****; // 0x0*
    pub const TAG_HEADER:  u8 = 0b_0001_0000; // 0x1* ASCII control char range
    // ASCII visible            0b_0***_****;
    pub const STR_HEADER:  u8 = 0b_1000_0000; // 0x8*
    pub const NUM_HEADER:  u8 = 0b_1001_0000; // 0x9*
    pub const ARR_HEADER:  u8 = 0b_1010_0000; // 0xA*
    pub const DICT_HEADER: u8 = 0b_1011_0000; // 0xB*
    // 2 byte UTF8 point start  0b_110*_****; // 0xC* or 0xD*
    // 3 byte UTF8 point start  0b_1110_****; // 0xE*
    // 4 byte UTF8 point start  0b_1111_0***; // 0xF*
    // RESERVED                 0b_1111_1***; // 0xF*

    pub const HEADER_MASK: u8 = 0xF0;
}

#[cfg(test)]
mod tests {
    use log::info;

    use crate::Litl;

    #[test]
    fn sizes() {
        info!("{}", std::mem::align_of::<Litl>());
        info!("{}", std::mem::size_of::<Litl>());
    }
}

#[macro_export]
macro_rules! impl_debug_as_litl {
    ($t:ty) => {
        impl std::fmt::Debug for $t {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Debug::fmt(&litl::Litl::from_se(self), f)
            }
        }
    };
}