English
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 flagsflatten/skip/always);decode_struct/encode_structwork from it. The table is built by#[derive(Options)]fromours-codec-derive, re-exported asours_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 atypefield (inbounds, outbounds, DNS servers, rule actions): sing-box's two-pass decoding (badjson.UnmarshallExcluded).goany— what the round trip "Goany→ JSON" does to a JSON value (numbers become float64 and so on):round_trip,badjson,merge.strconv::parse_uint— parsing numbers like Go'sstrconv.types— Rust types for sing-box value types (the correspondence table is inlibs/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-jsonandours-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).