use std::hash::{Hash, Hasher};
use serde::{Serialize, Serializer, ser::SerializeMap};
use xxhash_rust::xxh3::Xxh3;
pub type NamedField = (&'static str, Typ);
pub type Arr<T> = &'static [T];
pub type Str = &'static str;
pub type Map<T> = Arr<(u8, T)>;
#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
#[serde(tag = "type", content = "data")]
pub enum Fields {
Named(Arr<NamedField>),
Unnamed(Arr<Typ>),
}
#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
pub struct StructType {
pub name: &'static str,
pub fields: Fields,
}
fn variants<S, T>(variants: Map<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
{
let mut map = serializer.serialize_map(Some(variants.len()))?;
for (key, value) in variants {
map.serialize_entry(key, value)?;
}
map.end()
}
#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
pub struct EnumType {
pub name: Str,
#[serde(serialize_with = "variants")]
pub variants: Map<NamedField>,
}
#[derive(PartialEq, Eq, Clone, Serialize, Debug, Copy, Hash)]
pub struct SimpleEnumType {
pub name: Str,
#[serde(serialize_with = "variants")]
pub variants: Map<Str>,
}
#[derive(PartialEq, Clone, Serialize, Debug, Copy, Hash)]
#[serde(tag = "type", content = "data")]
pub enum Typ {
Bool,
U8,
U16,
U32,
U64,
I32,
I64,
F32,
F64,
Str,
Datetime,
Timestamp,
Decimal,
Id32,
Id64,
Fuid,
LowId,
Bytes,
ArrayBytes(u32),
Array(u32, &'static Typ),
Vec(&'static Typ),
Optional(&'static Typ),
SimpleEnum(SimpleEnumType),
Struct(StructType),
Enum(EnumType),
Void,
#[serde(rename = "Json")]
RustJson,
JsonBytes,
Custom(&'static str, &'static [Typ]),
}
impl Typ {
pub fn h(&self) -> u64 {
let mut hasher = Xxh3::new();
self.hash(&mut hasher);
hasher.finish()
}
}