#[macro_use]
extern crate amplify;
#[macro_use]
extern crate strict_types;
use std::io;
use strict_encoding::{
DecodeError, StrictDecode, StrictEncode, StrictType, TypedRead, TypedWrite, STRICT_TYPES_LIB,
};
use strict_types::stl::std_stl;
use strict_types::{Dependency, LibBuilder, TypeLib};
const LIB: &str = "Test";
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Default)]
pub struct Void;
impl StrictType for Void {
const STRICT_LIB_NAME: &'static str = LIB;
}
impl StrictEncode for Void {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> { Ok(writer) }
}
impl StrictDecode for Void {
fn strict_decode(_reader: &mut impl TypedRead) -> Result<Self, DecodeError> { Ok(Void) }
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB, tags = repr, into_u8, try_from_u8)]
#[repr(u8)]
pub enum Prim {
#[default]
A = 1,
B = 2,
}
#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB, tags = order)]
pub enum Message {
Init(u8),
Ping,
Pong { len: u8, nonce: Void },
Connect { host: Option<u8>, port: u16 },
}
impl Default for Message {
fn default() -> Self {
Message::Pong {
len: 0,
nonce: Void,
}
}
}
#[derive(Clone, Eq, PartialEq, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB)]
pub struct TypeA(u8, u16);
#[derive(Clone, Eq, PartialEq, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB)]
pub struct TypeB {
pub one: TypeA,
pub two: TypeA,
}
#[derive(Clone, Eq, PartialEq, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB)]
pub struct Complex {
pub a: TypeA,
pub b: TypeB,
pub prim: Prim,
pub msg: Message,
}
#[test]
fn serialize() {
let std = std_stl();
let builder =
LibBuilder::new(libname!(STRICT_TYPES_LIB), [std.to_dependency()]).transpile::<TypeLib>();
let lib = builder.compile().unwrap();
let imports = bset! {
Dependency::with(lib.id(), lib.name)
};
let builder = LibBuilder::new(libname!(LIB), imports).transpile::<Complex>();
let lib = builder.compile_symbols().unwrap();
println!("{}", lib);
}