tonlib-tlb-derive 0.1.0

Derive macro for tonlib-core TLB types
Documentation
use tonlib_tlb_derive::TLB;

#[derive(Debug, Clone, PartialEq, Eq, TLB)]
#[tlb(tag = "#ab")]
struct TaggedNamed {
    value: u32,
    enabled: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, TLB)]
struct TupleBody(bool, #[tlb(bit_len = 5)] u8);

#[derive(Debug, Clone, PartialEq, Eq, TLB)]
#[tlb(tag = "$1")]
struct UnitFlag;

mod tagged_named {
    use super::TaggedNamed;
    use tl_core::tlb_types::tlb::TLB;

    #[test]
    fn should_round_trip() {
        let value = TaggedNamed {
            value: 42,
            enabled: true,
        };

        let cell = value.to_cell().unwrap();
        let decoded = TaggedNamed::read(&mut cell.parser()).unwrap();

        assert_eq!(decoded, value);
    }
}

mod tuple_body {
    use super::TupleBody;
    use tl_core::tlb_types::tlb::TLB;

    #[test]
    fn should_round_trip() {
        let value = TupleBody(true, 0b1_1111);

        let cell = value.to_cell().unwrap();
        let decoded = TupleBody::read(&mut cell.parser()).unwrap();

        assert_eq!(decoded, value);
    }
}

mod unit_flag {
    use super::UnitFlag;
    use tl_core::tlb_types::tlb::TLB;

    #[test]
    fn should_round_trip() {
        let value = UnitFlag;

        let cell = value.to_cell().unwrap();
        let decoded = UnitFlag::read(&mut cell.parser()).unwrap();

        assert_eq!(decoded, value);
    }
}

mod v4_wallet_parse {
    use tl_core::tlb_types::tlb::TLB;
    use tl_core::{cell::BagOfCells, TonHash};
    use tonlib_tlb_derive::TLB;

    #[test]
    fn should_parse_v4_wallet_data() {
        #[derive(Debug, Clone, TLB)]
        struct WalletData {
            seqno: u32,
            subwallet_id: u32,
            pub_key: TonHash,
        }

        let data_cell = BagOfCells::parse_hex("b5ee9c7201010101002b0000510000014d29a9a317f387ca788636129ff2e4ff670f0f572b40f3b40ac1ef9cd9ff050a4183981f2440").unwrap().single_root().unwrap();

        let data = WalletData::read(&mut data_cell.parser()).unwrap();

        assert_eq!(data.seqno, 333);
        assert_eq!(data.subwallet_id, 698983191);
        assert_eq!(
            data.pub_key,
            TonHash::from_hex("f387ca788636129ff2e4ff670f0f572b40f3b40ac1ef9cd9ff050a4183981f24")
                .unwrap()
        );
    }
}