Skip to content

Add a top-level section ​

A section is a top-level YAML key (ntp, log, http_clients, subscriptions…) that a module decodes. The model is modules/system/ntp: the ntp section, its schema and a unit that builds, starts and, on reload, keeps or replaces the component.

1. The section decoder ​

Provide::Section { key, kind, decode }, where decode: fn(&Node, &dyn Scope) -> ours_codec::Result<Decoded> (ours_api::config::SectionDecoder). The native notation may differ from sing-box — the decoder translates it into sing-box options and decodes them (modules/system/ntp/src/lib.rs, fn section):

  • config::members(node, "ntp") — the object's members;
  • config::split_address(&mut fields, "server", "server_port", true) — host:port;
  • config::take, config::rename — the dial block and renames (interface → bind_interface);
  • scope.any(&value, "detour") — check a reference to an outbound;
  • config::Decoded::of::<NTPOptions>(&node) — decode with exact member names.

kind: Some(ComponentKind::Ntp) — the whole section is one component of this kind for the reload plan: Decoders::decode_section calls .whole(kind) on what the decoder returned. A section of several components gives None and names them itself (.each(kind, name) — each list element is a component, like http_clients). A kind without its own variant is ComponentKind::Other(name); a section that applies in place can still be a component, as adblock is, and its unit decides how to apply the change.

A key may be compound: ours-mod-router decodes route.rules and route.default_http_client separately from the rest of route.

2. The schema ​

Provide::Schema { name: "<key>", schema } — the JSON Schema part for this section (modules/system/ntp/src/options.rs, fn schema: the fields from the #[derive(Options)] table without enabled, server_port and the dialer members, plus a reference to dial).

3. The unit ​

A section does nothing by itself: it is taken by the module's unit — Provide::Unit(|| Box::new(Unit)), impl ours_api::Unit (see Kernel):

  • build(&mut Build) — before the components are built, opening nothing (a failed reload has nothing to roll back): build.section::<NTPOptions>("ntp"), the context build.context(); publish what the components need into build.services.insert(...); take someone else's — build.services.require::<T>()?. Return a Stage.
  • Stage::commit — the configuration has gone live: start the new, stop the replaced.
  • close — the instance is stopping.
  • Reuse on reload: build.plan.change(&ComponentId::singleton(ComponentKind::Ntp)) — None if the plan did not change the section (Ntp::reusable).

4. Outside the module ​

  1. The loader. The list of top-level sections is not derived from the modules:
    • TOP_LEVEL in crates/native-config/src/entity.rs sets the accepted keys and their schema order.
    • The loader decodes sections in phases. Add the key to the phase whose inputs the decoder reads:
      • before the entities: the key list in crates/native-config/src/entity.rs that starts with log (log, certificate, ntp, network_namespaces, http_clients, adblock);
      • after the entities and their checks: the list with cache, reload, runtime, admin in the same file;
      • route and route.rules are decoded in rules.rs, dns in dns.rs.
  2. The binary. Feature and LIST — module.md. If the section is set but the module is not built in, the loader answers `<key>` is not built in (feature `<module>`) — the name is taken from the build.rs table.

5. Tests ​

modules/system/ntp/tests/instance.rs — the section in a live instance: the component is built and starts with the instance and stops with it; a reload without changes keeps the same Arc, a change of interval replaces it (reloaded.plan.changes contains ComponentKind::Ntp). Plus a loader test for notation errors (crates/native-config/tests/load.rs, schema.rs).

GPL-3.0-or-later