Skip to content

Instantly share code, notes, and snippets.

@jamesmunns
Created June 2, 2022 11:27
Show Gist options
  • Save jamesmunns/26763aa0efa47ce0508ff62d7818aafc to your computer and use it in GitHub Desktop.
Save jamesmunns/26763aa0efa47ce0508ff62d7818aafc to your computer and use it in GitHub Desktop.
// Serde Data Model Types (and friends)
pub enum SDMTy {
Bool,
I8,
I16,
I32,
I64,
I128,
U8,
U16,
U32,
U64,
U128,
F32,
F64,
Char,
String,
ByteArray,
Option(&'static NamedType),
Unit,
UnitStruct,
UnitVariant,
NewtypeStruct(&'static NamedType),
NewtypeVariant(&'static NamedType),
Seq(&'static NamedType),
Tuple(&'static [&'static NamedType]),
TupleStruct(&'static [&'static NamedType]),
TupleVariant(&'static [&'static NamedType]),
Map { key: &'static NamedType, val: &'static NamedType },
Struct(&'static [&'static NamedValue]),
StructVariant(&'static [&'static NamedValue]),
// But also
Usize,
Isize,
Enum(&'static [&'static NamedVariant]),
}
pub struct NamedType {
tyname: &'static str,
ty: &'static SDMTy,
}
pub struct NamedValue {
valname: &'static str,
ty: &'static NamedType,
}
pub struct NamedVariant {
valname: &'static str,
ty: &'static SDMTy,
}
#[derive(Serialize, Deserialize)]
enum Inner {
Alpha,
Beta,
Gamma,
Delta((i32, i16)),
}
const U8_Schema: NamedType = NamedType { tyname: "u8", ty: &SDMTy::U8 };
const U32_Schema: NamedType = NamedType { tyname: "u32", ty: &SDMTy::U32 };
const U64_Schema: NamedType = NamedType { tyname: "u64", ty: &SDMTy::U64 };
const I16_Schema: NamedType = NamedType { tyname: "i16", ty: &SDMTy::I16 };
const I32_Schema: NamedType = NamedType { tyname: "i32", ty: &SDMTy::I32 };
const Inner_Schema: NamedType = NamedType {
tyname: "Inner",
ty: &SDMTy::Enum(&[
&NamedVariant { valname: "Alpha", ty: &SDMTy::UnitVariant },
&NamedVariant { valname: "Beta", ty: &SDMTy::UnitVariant },
&NamedVariant { valname: "Gamma", ty: &SDMTy::UnitVariant },
&NamedVariant { valname: "Delta", ty: &SDMTy::TupleVariant(&[
&I32_Schema,
&I16_Schema,
]) },
]),
};
#[derive(Serialize, Deserialize)]
struct Outer {
a: u32,
b: u64,
c: u8,
d: Inner,
e: Vec<u8>,
}
const Outer_Schema: NamedType = NamedType {
tyname: "Outer",
ty: &SDMTy::Struct(&[
&NamedValue { valname: "a", ty: &U32_Schema },
&NamedValue { valname: "b", ty: &U64_Schema },
&NamedValue { valname: "c", ty: &U8_Schema },
&NamedValue { valname: "d", ty: &Inner_Schema },
&NamedValue { valname: "e", ty: &NamedType {
tyname: "Vec<u8>",
ty: &SDMTy::Seq(&U8_Schema),
}}
]),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment