diff options
| author | tv <tv@krebsco.de> | 2026-12-02 16:24:19 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-02 17:08:52 +0000 |
| commit | 2be9ca2b75d099a9ca04078f2182acda074ed270 (patch) | |
| tree | acd772e1873e566e2022f068628179bbd53eb8e9 /crates/config/src/file.rs | |
| parent | 1de11da61a106187748c4b79e1d7b63e3b9d172f (diff) | |
config: a configuration names importers of its own
[meta.importer] in a file, or --meta-importer-NAME IMPORTER, names an
importer: NAME:REST, { NAME = "REST" } and a placeholder of that name
are then that importer, with REST as its variable `name`. So what would
be built in for one tool, pass:NAME, is written by who uses it, once:
pass = { command = ["pass", "show", "$(var:name)"] }.
A name counts in every file and argument, wherever it is given; of two
of one name, the later source's. What follows the name goes in as every
variable does, as data. One named may be defined through others, not
through itself. `meta` at the top of a configuration file is what the
configuration says about itself. The argument is spelled as every
option's, the table's keys joined with `-`; as its name is the
configuration's, it is taken off the command line before the command
reads it.
Diffstat (limited to 'crates/config/src/file.rs')
| -rw-r--r-- | crates/config/src/file.rs | 104 |
1 files changed, 94 insertions, 10 deletions
diff --git a/crates/config/src/file.rs b/crates/config/src/file.rs index b365623..1c88c15 100644 --- a/crates/config/src/file.rs +++ b/crates/config/src/file.rs @@ -7,6 +7,12 @@ //! file's own values win over theirs, and later includes over earlier ones. A cycle is //! an error. //! +//! `[meta.importer]` names importers (`import::Importers`): `NAME = { command = […] }` +//! makes `NAME:REST` and `{ NAME = "REST" }` that importer, with `REST` as its variable +//! `name`. A name counts in every file and argument of the configuration, wherever it +//! is given; of two of one name, the later source's: a file's own over its includes', +//! a later file's over an earlier one's, an argument's over them all. +//! //! `unset = [options]` sets options back to their defaults, overriding includes and //! earlier files; in JSON, `null` does the same for one option. @@ -16,22 +22,96 @@ use anyhow::{Context, Result, anyhow, bail}; use serde_json::{Map as JsonMap, Value as JsonValue}; use crate::import; -use crate::layer::{Entry, Layer, Overlay, Provenance, overlay, resolve_file_value, unset_entry}; +use crate::layer::{ + Entry, Layer, Overlay, Provenance, overlay, resolve_file_value_in, unset_entry, +}; use crate::option::Schema; +/// Configuration about the configuration: what a file says under `meta`. +const META: &str = "meta"; +/// Under `meta`: the importers the file names. +const IMPORTERS: &str = "importer"; const INCLUDE: &str = "include"; /// Undoing a value set before: the key `unset` in a file, and the argument `--unset`. pub const UNSET: &str = "unset"; /// Gone: optional includes are entries of `include`, so the order is the list's. const INCLUDE_OPTIONAL: &str = "include-optional"; -/// The settings `path` makes of the options of `schema`, its includes resolved: a key -/// naming none of them is an error. +/// The importers `path` names (`[meta.importer]`), its includes' first, its own over +/// theirs. +pub fn importers(path: &Path) -> Result<import::Importers> { + importers_inner(path, &mut Vec::new()) +} + +fn importers_inner(path: &Path, loading: &mut Vec<PathBuf>) -> Result<import::Importers> { + let canonical = path + .canonicalize() + .with_context(|| format!("configuration file {}", path.display()))?; + if loading.contains(&canonical) { + bail!("{} includes itself (through {:?})", path.display(), loading); + } + let dir = canonical + .parent() + .map(Path::to_owned) + .unwrap_or_else(|| PathBuf::from("/")); + let mut root = parse(&canonical)?; + let includes = take_includes(&mut root, &canonical)?; + let mut named = import::Importers::new(); + loading.push(canonical.clone()); + for (include, required) in includes { + let include = dir.join(include); + if !required && !include.exists() { + continue; + } + named.extend( + importers_inner(&include, loading) + .with_context(|| format!("included from {}", canonical.display()))?, + ); + } + loading.pop(); + named + .extend(take_meta(&mut root, &dir).with_context(|| format!("in {}", canonical.display()))?); + Ok(named) +} + +/// What `root` says under `meta`, taken out of it: the importers it names. +fn take_meta(root: &mut JsonMap<String, JsonValue>, dir: &Path) -> Result<import::Importers> { + let Some(meta) = root.remove(META) else { + return Ok(import::Importers::new()); + }; + let JsonValue::Object(mut meta) = meta else { + bail!("meta must be a table"); + }; + let importers = match meta.remove(IMPORTERS) { + None => import::Importers::new(), + Some(JsonValue::Object(table)) => import::named_in_table(&table, dir)?, + Some(other) => bail!("meta.importer must be a table, not {other}"), + }; + if let Some(unknown) = meta.keys().next() { + bail!("unknown key {unknown:?} in meta"); + } + Ok(importers) +} + +/// The settings `path` makes of the options of `schema`, as `load_in`, by the +/// importers it names itself. pub fn load(path: &Path, schema: &Schema) -> Result<Layer> { - load_inner(path, schema, &mut Vec::new()) + load_in(path, schema, &importers(path)?) } -fn load_inner(path: &Path, schema: &Schema, loading: &mut Vec<PathBuf>) -> Result<Layer> { +/// The settings `path` makes of the options of `schema`, its includes resolved: a key +/// naming none of them is an error. `named` are the importers the configuration +/// names, here and elsewhere. +pub fn load_in(path: &Path, schema: &Schema, named: &import::Importers) -> Result<Layer> { + load_inner(path, schema, named, &mut Vec::new()) +} + +fn load_inner( + path: &Path, + schema: &Schema, + named: &import::Importers, + loading: &mut Vec<PathBuf>, +) -> Result<Layer> { let canonical = path .canonicalize() .with_context(|| format!("configuration file {}", path.display()))?; @@ -51,6 +131,8 @@ fn load_inner(path: &Path, schema: &Schema, loading: &mut Vec<PathBuf>) -> Resul ); } let includes = take_includes(&mut root, &canonical)?; + // Read apart, for every file and argument (`importers`). + take_meta(&mut root, &dir).with_context(|| format!("in {}", canonical.display()))?; let unsets = take_paths(&mut root, UNSET, &canonical)?; loading.push(canonical.clone()); @@ -62,7 +144,7 @@ fn load_inner(path: &Path, schema: &Schema, loading: &mut Vec<PathBuf>) -> Resul tracing::debug!(file = %include.display(), "Optional configuration file is missing; skipped"); continue; } - let included = load_inner(&include, schema, loading) + let included = load_inner(&include, schema, named, loading) .with_context(|| format!("included from {}", canonical.display()))?; for (name, entry) in included { overlay(&mut layer, name, entry, Overlay::IncludeOverInclude, schema); @@ -88,13 +170,14 @@ fn load_inner(path: &Path, schema: &Schema, loading: &mut Vec<PathBuf>) -> Resul } let mut own = Vec::new(); - flatten(&root, "", &dir, &mut own).with_context(|| format!("in {}", canonical.display()))?; + flatten(&root, "", &dir, named, &mut own) + .with_context(|| format!("in {}", canonical.display()))?; for (name, value) in own { let spec = schema.spec(&name).ok_or_else(|| match schema.gone(&name) { Some(why) => anyhow!("option {name:?} in {} is gone: {why}", canonical.display()), None => anyhow!("unknown option {name:?} in {}", canonical.display()), })?; - let (value, sources) = resolve_file_value(spec, value, &dir) + let (value, sources) = resolve_file_value_in(spec, value, &dir, named) .with_context(|| format!("option {name} in {}", canonical.display()))?; let entry = Entry { origin: sources.origin( @@ -214,6 +297,7 @@ fn flatten( table: &JsonMap<String, JsonValue>, prefix: &str, dir: &Path, + named: &import::Importers, out: &mut Vec<(String, JsonValue)>, ) -> Result<()> { for (key, value) in table { @@ -223,8 +307,8 @@ fn flatten( format!("{prefix}-{key}") }; match value { - JsonValue::Object(nested) if import::parse_table(nested, dir)?.is_none() => { - flatten(nested, &name, dir, out)? + JsonValue::Object(nested) if import::parse_table_in(nested, dir, named)?.is_none() => { + flatten(nested, &name, dir, named, out)? } _ => out.push((name, value.clone())), } |
