English
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/Encodeare written by hand; the field table is still generated, and the hand-written code callsdecode_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 tagjson:"-";always— a field withoutomitempty;reference = "kind"— a field with tags of other components, like the Go tagreference:"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.