Skip to main content
Version: 0.17.0

Adding a Config Setting

This page describes the snakeway-conf configuration subsystem. The subsystem is built on the confval crate (span-first parsing, validation, and lowering). For the generic primitives see the confval docs. This page is the applied, Snakeway-specific recipe.

How the subsystem works​

For background, read the confval pipeline doc page to become familiar with the overall configuration subsystem architecture.

In Snakeway, this is the orchestration code that implements the confval pipeline:

FileRole
crates/snakeway-conf/src/loader.rsEntry point: discovers files, parses, validates, gates, lowers
crates/snakeway-conf/src/validation/validate.rsvalidate_spec: runs every validator, appending to the Report
crates/snakeway-conf/src/lower.rslower_configs: assembles the final RuntimeConfig
crates/snakeway-conf/src/validation/error.rsConfigError, including SemanticValidationFailed

The two-type system: spec vs config​

Every setting exists in two parallel structs:

  • Spec (crates/snakeway-conf/src/types/specification/) derives confval::Spec and Serialize. It is populated by parse_hcl. Every field is wrapped in Located<T> so it has its source span. No Deserialize, no Origin field.
  • Config (crates/snakeway-conf/src/types/runtime/) derives confval::Config (and serde). It is the resolved, executable form, produced by lowering. It uses fully typed values (SocketAddr, IpNet, runtime enums, narrowed integer widths).

The conversion is generated by #[derive(confval::Config)], declared on the Config struct with #[confval(lower_from = FooSpec)]. The with conversion functions live in the runtime file alongside the Config struct.

Type selection principle​

Spec types use the rawest type that parses infallibly from HCL, wrapped in Located. That means Located<String>, Located<i64>, Located<bool>, Located<PathBuf>, Vec<Located<String>>, and the Option<...> forms. The structural parsers never reject a value for a semantic reason, so a port of 99999 or a strategy of "failovr" parses fine and is caught by validation with a span, alongside every other problem.

Keyword fields are Located<String> in specs. Closed sets (load-balancing strategies, log levels) are declared once with confval::keyword_enum! in types/keyword.rs, which generates the enum, its keyword list, and the conversions each pipeline stage uses. Spec validation checks the string against the generated keyword_set(), lowering narrows through the generated TryFrom via narrow::keyword, and the runtime config holds the enum. Spec fields stay Located<String>, so the enum appears only from lowering onward.

Config types use the fully parsed, typed form. IpNet, Method, HeaderName, SocketAddr, runtime enums, exact integer widths. Downstream code never re-parses a value it received from config.

Lowering is the typing boundary. Validation checks values (read-only, collecting errors) and lowering produces the typed value. Because the gate runs first, narrowing conversions inside lowering are safe. A failure there means a validation rule is missing, not bad operator input, and it still reports with a span rather than panicking.

Where settings appear in HCL​

  • Server-level: snakeway.hcl, inside server { ... }.
  • Ingress-level: any file matched by include.ingresses, at the top level or inside bind { ... }, services = [ { ... } ], or static_files = [ { ... } ].
  • Device-level: any file matched by include.devices, inside the named device block (for example identity_device { ... }). Device spec structs live in crates/snakeway-conf/src/types/specification/device/ and are validated in crates/snakeway-conf/src/validation/single_file/device.rs.