#deserialize #serialization #header #seq #endian #meta #kaitai-like

nightly taikai

类似kaitai的序列化和反序列化

1 个不稳定版本

0.1.0 2019年1月3日

#15 in #seq


用于 netlink-wg

GPL-3.0 许可证

65KB
1.5K SLoC

#taikai

kaitai-like 序列化(和反序列化)

运行时要求

  • nom
  • byteorder
  • encoding_rs
  • log

示例

mod test_macro {
    taikai_from_str!(crate::test_macro, "
meta:
endian: be
id: top_level
seq:
- id: foo
    type: header
    # resolves to /top_level/header ──┐
- id: bar       #                     │
    type: body1 #                     │
- id: baz       #                     │
    type: body2 #                     │
types:          #                     │
header:   #     <─────────────────────┘ <─┐
    seq:             #                    |
    - id: i          #                    |
        type: u8     #                    |
body1:               #                    │
    seq:             #                    │
    - id: foo        #                    │
        type: super.header #              │
        # resolves to /top_level/header ──┘
    - id: bar        #                    │
        type: root.header  #              │
        # resolves to /top_level/header ──┘
body2:
    seq:
    - id: foo
        type: header
        # resolves to /top_level/second_level/header ──┐
    types: #                                           │
    header:   #     <──────────────────────────────────┘
        seq:
        - id: j
            type: u16le
");

    #[test]
    fn test_levels() {
        let ctx = Context {};

        let bytes = &[0x01, 0x02, 0x03, 0x04, 0x05];

        let (rest, obj) = TopLevel::read(bytes, &ctx).unwrap();
        assert_eq!(obj, TopLevel {
            foo: __subtypes::Header {i: 0x01},
            bar: __subtypes::Body1 {
                foo: __subtypes::Header {i: 0x02},
                bar: __subtypes::Header {i: 0x03},
            },
            baz: __subtypes::Body2 {
                foo: __subtypes::__subtypes::Header {j: 0x0504}
            },
        });
        assert_eq!(rest.len(), 0);

        let mut out = vec![];
        obj.write(&mut out, &ctx).unwrap();

        assert_eq!(&out[..], bytes);
    }
}

待办

许多内容

  • 使枚举工作
  • 类型切换
  • 实例
  • ...

依赖

~7MB
~196K SLoC