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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::prelude::*;

#[macro_use]
macro_rules! impl_type_id {
    ($T:ident, [$($name:ident: $i:expr,)+]) => {
        impl_type_id_inner!($T, [
            Void: 0,
            Tuple2: 100,
            Tuple3: 101,
            Tuple4: 102,
            Tuple5: 103,
            Tuple6: 104,
            Tuple7: 105,
            Tuple8: 106,
            TupleN: 107,
            Obj0: 108,
            Obj1: 109,
            Obj2: 110,
            Obj3: 111,
            Obj4: 112,
            Obj5: 113,
            Obj6: 114,
            Obj7: 115,
            Obj8: 116,
            ObjN: 117,
            $($name: $i,)+
        ]);
    };
}

macro_rules! impl_type_id_inner {
    ($T:ident, [$($name:ident: $i:expr,)+]) => {
        #[derive(PartialEq, Eq, Debug, Copy, Clone)]
        pub enum $T {
            $($name),+
        }

        impl TypeId for $T {
            fn void() -> Self where Self: Sized {
                $T::Void
            }
        }

        impl TryFrom<u8> for $T {
            type Error = ReadError;
            fn try_from(value: u8) -> ReadResult<Self> {
                Ok(match value {
                    $($i => $T::$name,)+
                    _ => return Err(ReadError::InvalidFormat(InvalidFormat::UnrecognizedTypeId)),
                })
            }
        }

        impl From<$T> for u8 {
            fn from(value: $T) -> Self {
                match value {
                    $($T::$name => $i,)+
                }
            }
        }

        impl $T {
            fn read_next(bytes: &[u8], offset: &mut usize) -> ReadResult<Self> {
                let next = bytes.get(*offset).ok_or_else(|| ReadError::InvalidFormat(InvalidFormat::EndOfFile))?;
                *offset += 1;
                (*next).try_into()
            }
        }
    }
}

mod root_branch;
pub use root_branch::*;

mod array_branch;
pub use array_branch::*;

pub type Ident<'a> = &'a str;

#[cfg(feature = "read")]
#[inline]
pub fn read_ident<'a>(bytes: &'a [u8], offset: &mut usize) -> ReadResult<Ident<'a>> {
    read_str(bytes, offset)
}

#[cfg(feature = "write")]
#[inline]
pub fn write_ident(value: &str, bytes: &mut Vec<u8>) {
    write_str(value, bytes)
}

pub trait TypeId: Copy + Into<u8> + PartialEq + std::fmt::Debug {
    fn void() -> Self
    where
        Self: Sized;
}

// TODO: Finish or get rid of this mod
//mod visitor;

// TODO: There are conceptually 4 pieces which are intermingled in this code.
// 1: The actual 'object model' that TreeBuf uses. Eg:
//     Root values, array values,
// 2: Allowable conversions and downcasts inherant to the object model
// 3: The binary representation of the object model
// 4: The conversion of the object model to Rust data types, like Vec, u8, etc.
//
// It would be good to split these, but not at the cost of performance.
// Doing so would go a long way in guiding a port to another language.
//
// A purist architecture would do each step separately...
// 1: Convert Rust types into Object Model
// 2: Write Object model
// - and in reverse for deserialize
//
// Note that the object model may be just defined in terms of eg: Number, where Number is the sum type of F64, u64, and i64 with downcasts.

#[cfg(feature = "read")]
pub fn read_root(bytes: &[u8]) -> ReadResult<DynRootBranch<'_>> {
    if bytes.len() == 0 {
        return Ok(DynRootBranch::Void);
    }
    let mut lens = bytes.len() - 1;
    let mut offset = 0;
    read_next_root(bytes, &mut offset, &mut lens)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::{TryFrom, TryInto};
    fn convert_all<T: TryFrom<u8> + Into<u8>>() {
        for i in 0..=255u8 {
            let v: Result<T, _> = i.try_into();
            if let Ok(v) = v {
                debug_assert_eq!(i, v.into());
            }
        }
    }

    #[cfg(feature = "read")]
    #[test]
    fn all_root_type_ids() {
        convert_all::<RootTypeId>();
    }

    #[cfg(feature = "read")]
    #[test]
    fn all_array_type_ids() {
        convert_all::<ArrayTypeId>();
    }
}