diff options
| author | tv <tv@krebsco.de> | 2026-11-28 22:11:12 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 00:22:34 +0000 |
| commit | 479246f98be360b5943458bd58d07e6d46f81fd0 (patch) | |
| tree | 0931722a9151e5f65180e8260f9e3faf7780c1ab | |
| parent | fdfe968d7abb6470e65253a720f87b82f5d5e388 (diff) | |
config: what a configuration imports by, in a crate of its own
Importers and placeholders (env:, file:, cmd:, ${…}, +json, $$, the
tables) move from config::import to the crate config, crates/config, as
its module import, as they were, public: the crate is for what a
program's configuration is made of, from its files and its arguments,
and other programs make theirs with it.
The repository is a workspace of the node and that crate, both default
members, so that cargo quick, cargo sim and clippy take in both.
| -rw-r--r-- | Cargo.lock | 9 | ||||
| -rw-r--r-- | Cargo.toml | 9 | ||||
| -rw-r--r-- | crates/config/Cargo.toml | 14 | ||||
| -rw-r--r-- | crates/config/src/import.rs (renamed from src/config/import.rs) | 32 | ||||
| -rw-r--r-- | crates/config/src/lib.rs | 6 | ||||
| -rw-r--r-- | docs/architecture.md | 2 | ||||
| -rw-r--r-- | src/config/mod.rs | 3 |
7 files changed, 55 insertions, 20 deletions
@@ -932,6 +932,14 @@ dependencies = [ ] [[package]] +name = "config" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde_json", +] + +[[package]] name = "console-api" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1019,6 +1027,7 @@ dependencies = [ "blake3", "bytes", "clap", + "config", "console-subscriber", "dashmap", "ed25519-dalek", @@ -1,3 +1,9 @@ +# The node, and the libraries it is made with that other programs use too. Tests, +# checks and builds take them all in unless told otherwise. +[workspace] +members = ["crates/config"] +default-members = [".", "crates/config"] + [package] name = "continuum" version = "0.0.1" @@ -41,6 +47,9 @@ tor = [ most = ["dashboard", "federation", "libp2p", "mycelium", "tor"] [dependencies] +# What a configuration is made of, from files and arguments: a crate of its own, for +# other programs to make theirs alike. +config = { path = "crates/config" } anyhow = "1.0" # The tor feature: arti's crates are released together, at the same version. arti-client = { version = "0.48", optional = true, features = ["onion-service-client", "onion-service-service", "static-sqlite"] } diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml new file mode 100644 index 0000000..090ee26 --- /dev/null +++ b/crates/config/Cargo.toml @@ -0,0 +1,14 @@ +# What a configuration is made of, from files and arguments (`src/lib.rs`): what +# continuum's is made with, as a library for other programs to make theirs alike. +[package] +name = "config" +version = "0.1.0" +edition = "2024" + +# Its doc comments are prose, not examples to test. +[lib] +doctest = false + +[dependencies] +anyhow = "1.0" +serde_json = "1.0" diff --git a/src/config/import.rs b/crates/config/src/import.rs index b50521a..886caf2 100644 --- a/src/config/import.rs +++ b/crates/config/src/import.rs @@ -1,5 +1,6 @@ //! Importers: values taken from elsewhere instead of written out, so a configuration -//! can say where a value comes from without holding it (secrets, host facts). +//! can say where a value comes from without holding it (secrets, host facts): as +//! continuum's does (its `docs/configuration.md`, Importing values). //! //! * `env:NAME`: an environment variable; //! * `file:PATH`: a file's contents; @@ -25,13 +26,13 @@ use anyhow::{Context, Result, anyhow, bail}; use serde_json::{Map as JsonMap, Value as JsonValue}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum Format { +pub enum Format { Text, Json, } #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum Source { +pub enum Source { Env(String), File(PathBuf), Shell { @@ -50,20 +51,20 @@ pub(crate) enum Source { } #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Importer { - pub(crate) source: Source, - pub(crate) format: Format, +pub struct Importer { + pub source: Source, + pub format: Format, } /// What a command-line value is: imported, or literal. #[derive(Debug, PartialEq, Eq)] -pub(crate) enum Raw<'a> { +pub enum Raw<'a> { Import(Importer), Literal(&'a str), } /// Reads a command-line value. -pub(crate) fn parse_argument(raw: &str) -> Raw<'_> { +pub fn parse_argument(raw: &str) -> Raw<'_> { if let Some(rest) = raw.strip_prefix("literal:") { return Raw::Literal(rest); } @@ -96,7 +97,7 @@ fn literal_or_interpolated(raw: &str) -> Raw<'_> { /// `text` as an importer if it holds a `$` (a placeholder, `$$`, or a mistake that /// expanding it reports), read in `dir`; None if it is plain text. -pub(crate) fn interpolated(text: &str, dir: Option<&Path>) -> Option<Importer> { +pub fn interpolated(text: &str, dir: Option<&Path>) -> Option<Importer> { text.contains('$').then(|| Importer { source: Source::Template { template: text.to_owned(), @@ -107,10 +108,7 @@ pub(crate) fn interpolated(text: &str, dir: Option<&Path>) -> Option<Importer> { } /// Reads an importer table from a file in `dir`; None if `object` isn't one. -pub(crate) fn parse_table( - object: &JsonMap<String, JsonValue>, - dir: &Path, -) -> Result<Option<Importer>> { +pub fn parse_table(object: &JsonMap<String, JsonValue>, dir: &Path) -> Result<Option<Importer>> { let sources = ["env", "file", "command"] .into_iter() .filter(|key| object.contains_key(*key)) @@ -171,7 +169,7 @@ pub(crate) fn parse_table( impl Importer { /// Shown instead of the value (which may be secret), e.g. `env:HOSTNAME`. - pub(crate) fn describe(&self) -> String { + pub fn describe(&self) -> String { let format = match self.format { Format::Text => "", Format::Json => "+json", @@ -187,12 +185,12 @@ impl Importer { /// Whether this is text with placeholders (`interpolated`) rather than an importer /// of the whole value: its result is shown, the importers' are not. - pub(crate) fn is_interpolation(&self) -> bool { + pub fn is_interpolation(&self) -> bool { matches!(self.source, Source::Template { .. }) } /// The imported content: text, or JSON. - pub(crate) fn fetch(&self) -> Result<Imported> { + pub fn fetch(&self) -> Result<Imported> { let text = match &self.source { Source::Env(name) => std::env::var(name).with_context(|| { format!("environment variable {name} is not set (or not UTF-8)") @@ -223,7 +221,7 @@ impl Importer { } #[derive(Debug, Clone, PartialEq)] -pub(crate) enum Imported { +pub enum Imported { Text(String), Json(JsonValue), } diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs new file mode 100644 index 0000000..654c475 --- /dev/null +++ b/crates/config/src/lib.rs @@ -0,0 +1,6 @@ +//! What a program's configuration is made of, from its files and its arguments: a +//! library, which continuum's configuration is made with, and other programs' may be. +//! +//! * [`import`]: values taken from elsewhere instead of written out. + +pub mod import; diff --git a/docs/architecture.md b/docs/architecture.md index f462fe7..69b0cc3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -305,7 +305,7 @@ Roughly by size and by how many rules interact there: | [`swarm/peer/`](../src/swarm/peer/) + [`sync.rs`](../src/swarm/sync.rs) + [`source.rs`](../src/swarm/source.rs) | 2,300 | The protocol state machine: handshake, liveness, sync sessions with deadlines, who syncs with whom while catching up. Timing-dependent; tested on the simulated network ([`simtest/`](../src/simtest/)). | | [`state/`](../src/state/) | 1,600 | `AppState` and its write helpers ([`mod.rs`](../src/state/mod.rs)), `PeerState` ([`peer.rs`](../src/state/peer.rs)), `Timing`, reads ([`read.rs`](../src/state/read.rs)) and the `Host` impl ([`host.rs`](../src/state/host.rs)). A hub, not a layer. | | [`transport/`](../src/transport/) | 11,500 | libp2p ([`libp2p/`](../src/transport/libp2p/), 4,400: addresses, relays, hole punching, which paths to keep), the paths and identity layers (2,300: measuring, failing over, proving node IDs), Mycelium and its daemon ([`mycelium/`](../src/transport/mycelium/), 1,200), Tor (700), the wire format ([`wire.rs`](../src/transport/wire.rs), derived), the simulator. The transports are self-contained behind the `Transport` trait; libp2p's choice of addresses is the subtle part. | -| [`config/`](../src/config/) | 2,800 | Layered sources, importers and placeholders, conflicts, the report. Self-contained. | +| [`config/`](../src/config/), [`crates/config/`](../crates/config/) | 3,300, 500 | Layered sources, conflicts, the report; importers and placeholders, in a crate of their own (`config`), which other programs use too. Self-contained. | | [`document/store.rs`](../src/document/store.rs) | 1,200 | Conflict-aware JSON over Automerge. Subtle (merged maps, canonical writes), well covered by the rest relying on it. | | [`swarm/topology/`](../src/swarm/topology/), [`blob.rs`](../src/swarm/blob.rs) | 1,500 | Pure algorithms with tests; the topology hashes each member once, then scores the N² pairs by cheap mixing. | | [`api/`](../src/api/) | 3,600 | Handlers; the Prometheus rendering is the longest file. The dashboard's page ([`dashboard.html`](../src/api/dashboard/dashboard.html), 2,000 more) is one file of HTML, CSS and script. | diff --git a/src/config/mod.rs b/src/config/mod.rs index cfc2c76..fd1f8e1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -11,7 +11,6 @@ //! into [`Settings`] and checked. mod file; -mod import; use std::collections::BTreeMap; use std::net::SocketAddr; @@ -25,7 +24,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use tracing::{debug, info, warn}; -use import::{Imported, Raw}; +use ::config::import::{self, Imported, Raw}; /// How an option's value is written as text (command line, text imports). #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
