Skip to content

ours-codec-derive ​

libs/codec-derive — the #[derive(Options)] proc-macro for the option structures of ours-codec. Used only through the re-export ours_codec::Options.

What it generates ​

For a structure — Fields (the field table), Decode, Encode and References.

Structure attributes, #[config(...)]:

  • no_decode / no_encode — Decode/Encode are written by hand; the field table is still generated, and the hand-written code calls decode_struct/encode_struct.

Field attributes, #[config(...)]:

  • rename = "name" — the name in JSON, if it differs from the field name;
  • flatten — a Go embedded structure: its fields are lifted into this one;
  • skip — a field with the Go tag json:"-";
  • always — a field without omitempty;
  • reference = "kind" — a field with tags of other components, like the Go tag reference:"kind".

The generated References walks all fields, including skip.

Example ​

modules/protocols/socks/src/options.rs:

rust
#[derive(Options, Clone, Debug, Default, PartialEq)]
pub struct SOCKSOutboundOptions {
    #[config(flatten)]
    pub dialer: DialerOptions,
    #[config(flatten)]
    pub server: ServerOptions,
    pub version: SocksVersion,
    // ...
}

Specifics ​

  • Dependencies — proc-macro2, quote, syn.
  • The field table is needed not only by the codec: the native format's JSON Schema takes block members from it (ours_config::schema::fields_of, crates/config/src/schema.rs).

Tests ​

None of its own; the derive is checked by the tests of ours-codec (libs/codec/tests/framework.rs) and by all crates that use it.

GPL-3.0-or-later