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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::{collections::BTreeMap, fmt, str::FromStr};

use serde::Deserialize;

use crate::AnyScalar;

/// Reference to a Type
#[derive(Debug, Clone, PartialEq)]
pub enum TypeRef {
    /// A well-known type
    WellKnown(WellKnownTypeRef),
    /// A named type
    Named(String),
    /// A type that depends on an expression
    Dynamic {
        /// Expression to switch on
        switch_on: AnyScalar,
        /// Types depending on the value of the expression
        cases: BTreeMap<AnyScalar, TypeRef>,
    },
}

struct TypeRefVisitor;

impl<'de> serde::de::Visitor<'de> for TypeRefVisitor {
    type Value = TypeRef;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "string or {{ switch-on, cases }}")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        match WellKnownTypeRef::from_str(v) {
            Ok(w) => Ok(TypeRef::WellKnown(w)),
            Err(()) => Ok(TypeRef::Named(v.to_string())),
        }
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let mut switch_on = None;
        let mut cases = None;

        while let Some(key) = map.next_key::<&str>()? {
            match key {
                "switch-on" => {
                    switch_on = Some(map.next_value()?);
                }
                "cases" => {
                    cases = Some(map.next_value()?);
                }
                _ => return Err(serde::de::Error::custom("Invalid field")),
            }
        }

        Ok(TypeRef::Dynamic {
            switch_on: switch_on.unwrap(),
            cases: cases.unwrap(),
        })
    }
}

impl<'de> Deserialize<'de> for TypeRef {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(TypeRefVisitor)
    }
}

/// Endian Specification
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EndianSpec {
    /// Unspecified
    Implicit,
    /// Always Little Endian
    Little,
    /// Always Big Endian
    Big,
}

/// Integer byte width specification
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum IntTypeRef {
    /// 8 bit integer
    Int1,
    /// 16 bit integer
    Int2(EndianSpec),
    /// 32 bit integer
    Int4(EndianSpec),
    /// 64 bit integer
    Int8(EndianSpec),
}

impl IntTypeRef {
    /// Get the number of bytes for values of this type
    pub fn bytes(&self) -> usize {
        match self {
            IntTypeRef::Int1 => 1,
            IntTypeRef::Int2(_) => 2,
            IntTypeRef::Int4(_) => 4,
            IntTypeRef::Int8(_) => 8,
        }
    }

    /// Get the endian spec for this int type ref
    pub fn endian(&self) -> EndianSpec {
        match self {
            IntTypeRef::Int1 => EndianSpec::Implicit,
            IntTypeRef::Int2(e) | IntTypeRef::Int4(e) | IntTypeRef::Int8(e) => *e,
        }
    }
}

/// Float byte width specification
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FloatTypeRef {
    /// 32 bit IEEE floating point number (single precision)
    Float4(EndianSpec),
    /// 64 bit IEEE floating point number (double precision)
    Float8(EndianSpec),
}

impl FloatTypeRef {
    /// Get the number of bytes
    pub fn bytes(&self) -> usize {
        match self {
            FloatTypeRef::Float4(_) => 4,
            FloatTypeRef::Float8(_) => 8,
        }
    }

    /// Get the endian spec for this float type ref
    pub fn endian(&self) -> EndianSpec {
        match self {
            FloatTypeRef::Float4(e) | FloatTypeRef::Float8(e) => *e,
        }
    }
}

/// Well known [TypeRef]s
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WellKnownTypeRef {
    /// unsigned integer
    Unsigned(IntTypeRef),
    /// signed integer
    Signed(IntTypeRef),
    /// floating point number
    Float(FloatTypeRef),
    /// String
    Str,
    /// Nul-Terminated String
    StrZ,
}

impl FromStr for WellKnownTypeRef {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "u1" => Self::Unsigned(IntTypeRef::Int1),
            "u2" => Self::Unsigned(IntTypeRef::Int2(EndianSpec::Implicit)),
            "u2le" => Self::Unsigned(IntTypeRef::Int2(EndianSpec::Little)),
            "u2be" => Self::Unsigned(IntTypeRef::Int2(EndianSpec::Big)),
            "u4" => Self::Unsigned(IntTypeRef::Int4(EndianSpec::Implicit)),
            "u4le" => Self::Unsigned(IntTypeRef::Int4(EndianSpec::Little)),
            "u4be" => Self::Unsigned(IntTypeRef::Int4(EndianSpec::Big)),
            "u8" => Self::Unsigned(IntTypeRef::Int8(EndianSpec::Implicit)),
            "u8le" => Self::Unsigned(IntTypeRef::Int8(EndianSpec::Little)),
            "u8be" => Self::Unsigned(IntTypeRef::Int8(EndianSpec::Big)),

            "s1" => Self::Signed(IntTypeRef::Int1),
            "s2" => Self::Signed(IntTypeRef::Int2(EndianSpec::Implicit)),
            "s2le" => Self::Signed(IntTypeRef::Int2(EndianSpec::Little)),
            "s2be" => Self::Signed(IntTypeRef::Int2(EndianSpec::Big)),
            "s4" => Self::Signed(IntTypeRef::Int4(EndianSpec::Implicit)),
            "s4le" => Self::Signed(IntTypeRef::Int4(EndianSpec::Little)),
            "s4be" => Self::Signed(IntTypeRef::Int4(EndianSpec::Big)),
            "s8" => Self::Signed(IntTypeRef::Int8(EndianSpec::Implicit)),
            "s8le" => Self::Signed(IntTypeRef::Int8(EndianSpec::Little)),
            "s8be" => Self::Signed(IntTypeRef::Int8(EndianSpec::Big)),

            "f4" => Self::Float(FloatTypeRef::Float4(EndianSpec::Implicit)),
            "f4le" => Self::Float(FloatTypeRef::Float4(EndianSpec::Little)),
            "f4be" => Self::Float(FloatTypeRef::Float4(EndianSpec::Big)),
            "f8" => Self::Float(FloatTypeRef::Float8(EndianSpec::Implicit)),
            "f8le" => Self::Float(FloatTypeRef::Float8(EndianSpec::Little)),
            "f8be" => Self::Float(FloatTypeRef::Float8(EndianSpec::Big)),

            "str" => Self::Str,
            "strz" => Self::StrZ,
            _ => return Err(()),
        })
    }
}