Skip to content

ours-codec ​

libs/codec — decoding and encoding configuration values: the traits an options type implements, their derive and shared value types (durations, lists, addresses).

Why a separate codec and not serde ​

Protocol options are compatible with sing-box, so decoding repeats Go's encoding/json in the form sing-box uses it, with its peculiarities: case-insensitive keys, repeated keys merge, null means "leave as is". The context may demand exact names — the native YAML loader does this (Cx::exact_names, see Configuration). An error carries a path (Cx::path) and an offset in the source text.

The API in outline ​

  • Decode/Encode — the value traits; decode_new, decode_array.
  • Fields, FieldTable, FieldEntry — a structure's field table (the JSON name, the flags flatten/skip/always); decode_struct/encode_struct work from it. The table is built by #[derive(Options)] from ours-codec-derive, re-exported as ours_codec::Options.
  • References, Reference, RefKind, RefTarget — fields that refer to the tags of other components (reference = "kind"); the kernel builds the dependency graph from them.
  • Error, Result, Cx — the decoding error and context.
  • typed — objects with a type field (inbounds, outbounds, DNS servers, rule actions): sing-box's two-pass decoding (badjson.UnmarshallExcluded).
  • goany — what the round trip "Go any → JSON" does to a JSON value (numbers become float64 and so on): round_trip, badjson, merge.
  • strconv::parse_uint — parsing numbers like Go's strconv.
  • types — Rust types for sing-box value types (the correspondence table is in libs/codec/src/types/mod.rs): Duration, Listable<T>, IpAddr/IpPrefix/Addr/ Prefixable, ByteSlice, RawJson/AnyValue, TypedMap, User, HttpHeader, size formats (byteformats).
  • section(text, key) — decode one section of a JSON document (used by tests).

Who uses it ​

ours-api (the contract's option types), ours-kernel, ours-native-config, ours-dns-types, ours-http, ours-relay, ours-quic, the binary and practically all modules — every module's options type derives Options.

Specifics ​

  • Depends only on ours-json and ours-codec-derive.
  • pub mod __private — re-exports for the code the derive generates; not for direct use.

Tests ​

Unit tests in src/ and libs/codec/tests/framework.rs — decoding behaviour checked against sing-box 1.14.4 (key case, flatten, null, errors).

GPL-3.0-or-later