1use std::hash::{Hash, Hasher};
2
3use serde::{Serialize, Serializer, ser::SerializeMap};
4use xxhash_rust::xxh3::Xxh3;
5
6pub type NamedField = (&'static str, Typ);
7
8pub type Arr<T> = &'static [T];
9pub type Str = &'static str;
10pub type Map<T> = Arr<(u8, T)>;
11
12#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
13#[serde(tag = "type", content = "data")]
14pub enum Fields {
15 Named(Arr<NamedField>),
16 Unnamed(Arr<Typ>),
17}
18
19#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
24pub struct StructType {
25 pub name: &'static str,
26 pub fields: Fields,
27}
28
29fn variants<S, T>(variants: Map<T>, serializer: S) -> Result<S::Ok, S::Error>
30where
31 S: Serializer,
32 T: Serialize,
33{
34 let mut map = serializer.serialize_map(Some(variants.len()))?;
35 for (key, value) in variants {
36 map.serialize_entry(key, value)?;
37 }
38 map.end()
39}
40
41#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
42pub struct EnumType {
43 pub name: Str,
44 #[serde(serialize_with = "variants")]
45 pub variants: Map<NamedField>,
46}
47
48#[derive(PartialEq, Eq, Clone, Serialize, Debug, Copy, Hash)]
49pub struct SimpleEnumType {
50 pub name: Str,
51 #[serde(serialize_with = "variants")]
52 pub variants: Map<Str>,
53}
54
55#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
56#[serde(tag = "type", content = "data")]
57pub enum Typ {
58 Bool,
59 U8,
60 U16,
61 U32,
62 U64,
63 I32,
64 I64,
65 F32,
66 F64,
67 Str,
68 Datetime,
70 Timestamp,
72 Decimal,
74 Id32,
76 Id64,
78 Fuid,
80 LowId,
82 Bytes,
84 ArrayBytes(u32),
87 Array(u32, &'static Typ),
88 Vec(&'static Typ),
90 Optional(&'static Typ),
91 SimpleEnum(SimpleEnumType),
92 Struct(StructType),
93 Enum(EnumType),
94 Void,
95 #[serde(rename = "Json")]
97 RustJson,
98 JsonBytes,
100 Custom(&'static str, &'static [Typ]),
101}
102
103impl Typ {
104 pub fn h(&self) -> u64 {
105 let mut hasher = Xxh3::new();
106 self.hash(&mut hasher);
107 hasher.finish()
108 }
109}