Skip to main content

stratum_node_config/
lib.rs

1//! Schema, validation, comment-preserving editing and public hashing for a
2//! Stratum node's `stratum.toml`.
3//!
4//! **Pre-release.** This is `0.1.0-alpha`: the API and the file format may
5//! change before 0.1.0.
6//!
7//! # The model
8//!
9//! - **One schema table.** [`schema`] returns the only definition of the
10//!   configuration: every field's dotted path, [`ValueType`], default,
11//!   description and [`ApplyClass`]. Validation, the seed document, the
12//!   typed [`Config`], the public view and the serialised form served by an
13//!   HTTP schema endpoint are all derived from it, so they cannot drift.
14//! - **Documents.** [`ConfigDocument::parse`] turns `stratum.toml` text into
15//!   a validated document or a list of every [`ValidationError`]: syntax,
16//!   unknown keys, wrong types, out-of-range values, field rules and
17//!   cross-field conflicts. A document carries `schema_version =
18//!   `[`SCHEMA_VERSION`]; any other version is rejected. Omitted fields take
19//!   their defaults.
20//! - **Preview.** [`preview`] applies a list of [`Change`]s to base text and
21//!   reports, without side effects, the edited text (comments and order
22//!   kept), a unified diff, the changed fields in schema order with
23//!   secret references redacted, each change's apply class, the strictest
24//!   class overall, and every validation error of the result.
25//! - **Apply classes.** [`ApplyClass::Live`] < [`ApplyClass::Restart`] <
26//!   [`ApplyClass::Redeploy`]; the strictest class of a set of changes is
27//!   their maximum and tells the caller what it must do for them to take
28//!   effect.
29//! - **Public hash.** [`ConfigDocument::public_config`] is the effective
30//!   configuration minus secret-reference fields, and
31//!   [`ConfigDocument::public_hash`] is SHA-256 over its documented
32//!   canonical encoding ([`PublicConfig::canonical_text`]) prefixed with
33//!   [`PUBLIC_HASH_VERSION`]. Changing a secret reference leaves the hash
34//!   unchanged; changing anything else changes it.
35//!
36//! The crate is pure: it performs no filesystem, network, clock or
37//! environment access and never resolves a secret reference. Text longer
38//! than [`MAX_DOCUMENT_BYTES`] (256 KiB) is refused before it is parsed. A
39//! syntax error reports only its line and column, never the parser's
40//! description, which can quote the input.
41//!
42//! `backup.age_recipients` accepts only post-quantum hybrid age recipients
43//! (`age1pq1…`), checked structurally (lowercase Bech32, checksum, padding,
44//! key length) without depending on `age`. The list holds at most
45//! [`MAX_BACKUP_RECIPIENTS`] (32) entries, the most a Stratum node encrypts a
46//! backup to.
47//!
48//! # Examples
49//!
50//! ```
51//! use stratum_node_config::{preview, schema, ApplyClass, Change, ConfigDocument};
52//!
53//! // Start from the generated seed, which validates and holds the defaults.
54//! let seed = schema().seed_text();
55//! let before = ConfigDocument::parse(&seed)?;
56//!
57//! // Preview an edit and inspect it before committing anything.
58//! let p = preview(&seed, &[Change::new("node.name", "alpha")])?;
59//! assert!(p.errors().is_empty());
60//! assert_eq!(p.strictest_apply_class(), Some(ApplyClass::Live));
61//!
62//! let after = p.into_document().ok_or("edit should be valid")?;
63//! assert_eq!(after.config().node_name(), "alpha");
64//! assert_ne!(after.public_hash(), before.public_hash());
65//! # Ok::<(), Box<dyn std::error::Error>>(())
66//! ```
67
68mod config;
69mod document;
70mod edit;
71mod error;
72mod public;
73mod recipient;
74mod rules;
75mod schema;
76mod seed;
77mod value;
78
79pub use config::{Config, SecretRef};
80pub use document::{ConfigDocument, MAX_DOCUMENT_BYTES};
81pub use edit::{preview, Change, FieldChange, Preview, REDACTED};
82pub use error::{PreviewError, ValidationError, ValidationErrorKind, ValidationErrors};
83pub use public::{PublicConfig, PublicHash, PublicIter, PUBLIC_HASH_VERSION};
84pub use schema::{
85    schema, ApplyClass, FieldDescriptor, Fields, Schema, ValueType, MAX_BACKUP_RECIPIENTS,
86    SCHEMA_VERSION,
87};
88pub use value::Value;
89
90/// Compiles and runs the README example as a doctest.
91#[cfg(doctest)]
92#[doc = include_str!("../README.md")]
93struct ReadmeDoctests;