#encoding #protocol #xdr #rfc4506 #serialization

bin+lib xdrgen

XDR codec generator from specification. Designed for use with xdr-codec.

23 releases

Uses old Rust 2015

0.4.4 Aug 2, 2017
0.4.3 Jul 18, 2017
0.4.2 Jun 20, 2017
0.4.0 Mar 3, 2017
0.1.7 Nov 11, 2015

#7 in #xdr

Download history 26/week @ 2023-11-02 15/week @ 2023-11-09 14/week @ 2023-11-16 37/week @ 2023-11-23 87/week @ 2023-11-30 58/week @ 2023-12-07 36/week @ 2023-12-14 60/week @ 2023-12-21 31/week @ 2023-12-28 11/week @ 2024-01-04 13/week @ 2024-01-11 55/week @ 2024-01-18 32/week @ 2024-01-25 32/week @ 2024-02-01 10/week @ 2024-02-08 228/week @ 2024-02-15

323 downloads per month
Used in 2 crates

MIT/Apache

135KB
3K SLoC

Rust XDR library

Build Status Crates.io Coverage Status

This crate provides xdrgen, which takes an XDR specification in a .x file, and produces Rust code to serialize and deserialize the specified types. It is intended to be used in conjunction with xdr-codec.

The syntax of the .x file follows RFC4506. This has type definitions for XDR but does not include RPC protocol specifications. Correspondingly, xdrgen does not support auto-generation of RPC clients/servers.

Changes in 0.4.0

  • Now uses the quote package, so it will work on stable Rust
  • Detects the use of Rust keywords in XDR specifications, and appends a _ to them.

Usage

Usage is straightforward. You can generate the Rust code from a spec a build.rs:

extern crate xdrgen;

fn main() {
    xdrgen::compile("src/simple.x").expect("xdrgen simple.x failed");
}

This code can then be included into a module:

mod simple {
    use xdr_codec;

    #[allow(dead_code)]
    include!(concat!(env!("OUT_DIR"), "/simple_xdr.rs"));
}

Once you have this, you can call mytype.pack(&mut output), and let mything: MyThing = xdr_codec::unpack(&mut input)?;.

The serializers require your types to implement the Pack and Unpack traits, and generate code to write to std::io::Write implementation, and read from std::io::Read.

All types and fields are generated public, so you can control their access outside your module or crate. If your spec references other types which are not defined within the spec, then you can define them within the module as well, either by aliasing them with other defined types, or implementing the Pack and Unpack traits yourself.

Use can use xdr-codec's XdrRecordReader and XdrRecordWriter types as IO filters that implement XDR-RPC record marking.

More documentation for xdrgen here. See the documentation for xdr-codec for more details about using the generated types and code.

Limitations

There are currently a few limitations:

  • The generated code uses identifiers as specified in the .x file, so the Rust code will not use normal formatting conventions.
  • Generated code follows no formatting convention - use rustfmt if desired.
  • XDR has discriminated unions, which are a good match for Rust enums. However, it also supports a default case if an unknown discriminator is encountered. This crate supports this for unpacking, but not for packing, as Rust does not allow enums to have unknown values.
  • The generated code uses #[derive(Debug, Clone, ...)] to generate implementations for common traits. However, rustc only supports #[derive] on fixed-size arrays with 0..32 elements; if you have an array larger than this, the generated code will fail to compile. Right now, the only workaround is to manually implement Pack and Unpack for such types. (TODO: add an option to omit derived traits.)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~8MB
~164K SLoC