Skip to content

Codec ​

Code: libs/codec (the ours-codec crate), libs/codec-derive (ours-codec-derive).

The codec translates the ours_json::Node tree into option types and back. The option types are the same structures as in sing-box (InboundTLSOptions, DialerOptions, RuleSet …): the native YAML has no option types of its own; the loader and modules bring it to a node these types understand. That is why the codec repeats the behaviour of Go's encoding/json, while native strictness is switched on by context flags.

Traits ​

rust
trait Decode { fn decode(&mut self, node: &Node, cx: &mut Cx) -> Result<()>; ... }
trait Encode { fn encode(&self) -> Value; fn is_empty_value(&self) -> bool { false } }
trait References { fn references(&self, out: &mut Vec<Reference>) {} }
trait Fields { fn field_table() -> &'static FieldTable; ... }   // from derive
  • Decode decodes into an existing value: as in Go, a repeated key merges with the result of the previous one, null leaves the value as it is. decode_new — from Default.
  • Encode writes what json.Marshal would write; is_empty_value — omitempty. It is Encode that gives the canonical form.
  • References collects the names of other components at any depth (references).
  • Fields — the structure's table of JSON fields; it is used by decode_struct/encode_struct, by the checks "the field belongs to the dial block" (dialer_members) and by JSON Schema.

The Cx context ​

FieldDefaultMeaning
disallow_unknowntruean unknown field is the error unknown field `x` on the key, with the closest name and the list of fields
exact_namesfalsethe field name only exact; otherwise Go case folding (fold_name)
pathemptykeys and indices for the error text (with_key, with_index)

Decoded::of and the modules' own readers (e.g. strict() in the router and DNS syntax) set exact_names = true: in the native format a field has one spelling. with_disallow repeats what in Go is decided per Unmarshal call: a nested hand-written decoder may accept unknown fields.

The field table ​

FieldTable::build::<T>() expands nested (flatten) structures applying Go's rule: the shallowest field wins, two fields at the same depth hide each other. lookup looks for the exact name, then the folded one; lookup_in(key, cx) takes exact_names into account. names() — the JSON names of the fields.

Derive #[derive(Options)] ​

Generates Fields, Decode, Encode, References for a structure with named fields (generic ones are not supported). The #[config(...)] attributes:

AttributeWhereMeaning
no_decode, no_encodestructurewrite Decode/Encode by hand; the field table is generated anyway
rename = "name"fieldthe JSON name, if it differs from the field name
flattenfielda Go embedded structure, its fields are lifted
skipfieldGo json:"-"; references in it are still collected
alwaysfieldwithout omitempty
reference = "kind"fieldcomponent names: inbound, outbound, dns_server, rule_set, network_namespace, http_client

Value types ​

ours_codec::types — counterparts of Go types: Listable<T> (one value or a list), Duration, Addr, Prefix, Prefixable, IpAddr, IpPrefix, IpAddrPort, Bytes, MemoryBytes, NetworkBytes, ByteSlice (base64), RawJson, AnyValue, TypedMap<K, V>, User. The table of correspondences is in the doc comment of types/mod.rs. badoption addresses treat null as an error, netip ones — as "leave untouched".

sing-box typed objects ​

ours_codec::typed repeats sing-box's two-pass reading (badjson.UnmarshallExcluded): the base fields (type, tag) with an ordinary Unmarshal, the rest through a round trip "into any and back" (goany::badjson: numbers become float64, duplicate keys collapse) and strictly. The native loader does not write the type and name into the object; these paths are used to read rule-set files in sing-box's source format (parse_rule_set in modules/routing/router/src/options/rule_set.rs, the ordinary Go mode Cx::new()) and JSON in compatibility tests. strconv — parsing numbers from strings by Go's rules.

Errors ​

ours_codec::Error { path, offset, message }. offset is the byte offset of the node in the source text (from the span), from which the loader computes line:column; the loader drops path. The json: prefix in the messages of Go-compatible errors is cut off (errors).

Test helpers ​

ours_codec::section::<T>(json, key) and entities::<T>(json, key) read a member of a sing-box JSON document — for tests of a single protocol without the loader.

GPL-3.0-or-later