oxidef_compact1 0.1.0-alpha.1

Oxidef is an experimental interface definition language and serialisation scheme for efficient and strongly-typed payloads.
Documentation
use core::fmt::Debug;

use crate::{
    codec::Compact1Codec,
    decoder::Decoder,
    encoder::{Encoder, ImprovidentVec},
};

#[track_caller]
fn expect_roundtrip<T: Compact1Codec + PartialEq + Debug>(t: T) {
    let mut encoder = Encoder::new(ImprovidentVec::default());
    t.encode(&mut encoder).unwrap();

    let bytes = encoder.finish();

    let mut decoder = Decoder::new(&bytes[..]);
    let t_roundtripped = T::decode(&mut decoder).unwrap();

    assert_eq!(t, t_roundtripped);
}

#[test]
fn test_roundtrip_bool() {
    expect_roundtrip(true);
    expect_roundtrip(false);
}

#[test]
fn test_roundtrip_u8() {
    expect_roundtrip(0u8);
    expect_roundtrip(42u8);
}

#[test]
fn test_roundtrip_u16() {
    expect_roundtrip(0u16);
    expect_roundtrip(1337u16);
}

// TODO roundtrip iX, x32, x64, x128

#[test]
fn test_roundtrip_option() {
    expect_roundtrip(None::<u8>);
    expect_roundtrip(Some(42u8));
    expect_roundtrip(Some(true));
    expect_roundtrip(Some(false));
    expect_roundtrip(Some(None::<u8>));
    expect_roundtrip(Some(Some(None::<u8>)));
    expect_roundtrip(Some(Some(Some(Some(Some(Some(Some(Some(None::<u8>)))))))));
}

#[test]
fn test_roundtrip_js_int() {
    expect_roundtrip(js_int::Int::new(42));
    expect_roundtrip(js_int::Int::new(-42));
    expect_roundtrip(js_int::Int::new(js_int::MAX_SAFE_INT));
    expect_roundtrip(js_int::Int::new(js_int::MIN_SAFE_INT));
}

#[test]
fn test_roundtrip_js_uint() {
    expect_roundtrip(js_int::UInt::new(42));
    expect_roundtrip(js_int::UInt::new(js_int::MAX_SAFE_UINT));
}