Skip to main content

armour_core/
typ.rs

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/**
20named structs, unnamed structs and tuples
21если нет названий полей в структуре, то это неименованная структура или кортеж.
22*/
23#[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    /// i64 unix timestamp milliseconds
69    Datetime,
70    /// u64 unix timestamp milliseconds
71    Timestamp,
72    /// rust_decimal - 96 bit repr
73    Decimal,
74    /// u32 big endian id type
75    Id32,
76    /// u64 big endian id type
77    Id64,
78    /// Fuid
79    Fuid,
80    /// LowId
81    LowId,
82    // массив байт
83    Bytes,
84    // массив байт фиксированного размера
85    /// array bytes with bytes count in scheme
86    ArrayBytes(u32),
87    Array(u32, &'static Typ),
88    // Вектор или массив значений одного типа
89    Vec(&'static Typ),
90    Optional(&'static Typ),
91    SimpleEnum(SimpleEnumType),
92    Struct(StructType),
93    Enum(EnumType),
94    Void,
95    /// Rust binary JSON representation
96    #[serde(rename = "Json")]
97    RustJson,
98    /// vector of bytes, representing json string
99    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}