stratum-node-config 0.1.0-alpha.4

Versioned stratum.toml schema, comment-preserving editor, semantic validator, change preview and public-config hash for Stratum nodes
Documentation
//! Schema, validation, comment-preserving editing and public hashing for a
//! Stratum node's `stratum.toml`.
//!
//! **Pre-release.** This is `0.1.0-alpha`: the API and the file format may
//! change before 0.1.0.
//!
//! # The model
//!
//! - **One schema table.** [`schema`] returns the only definition of the
//!   configuration: every field's dotted path, [`ValueType`], default,
//!   description and [`ApplyClass`]. Validation, the seed document, the
//!   typed [`Config`], the public view and the serialised form served by an
//!   HTTP schema endpoint are all derived from it, so they cannot drift.
//! - **Documents.** [`ConfigDocument::parse`] turns `stratum.toml` text into
//!   a validated document or a list of every [`ValidationError`]: syntax,
//!   unknown keys, wrong types, out-of-range values, field rules and
//!   cross-field conflicts. A document carries `schema_version =
//!   `[`SCHEMA_VERSION`]; any other version is rejected. Omitted fields take
//!   their defaults.
//! - **Preview.** [`preview`] applies a list of [`Change`]s to base text and
//!   reports, without side effects, the edited text (comments and order
//!   kept), a unified diff, the changed fields in schema order with
//!   secret references redacted, each change's apply class, the strictest
//!   class overall, and every validation error of the result.
//! - **Apply classes.** [`ApplyClass::Live`] < [`ApplyClass::Restart`] <
//!   [`ApplyClass::Redeploy`]; the strictest class of a set of changes is
//!   their maximum and tells the caller what it must do for them to take
//!   effect.
//! - **Public hash.** [`ConfigDocument::public_config`] is the effective
//!   configuration minus secret-reference fields, and
//!   [`ConfigDocument::public_hash`] is SHA-256 over its documented
//!   canonical encoding ([`PublicConfig::canonical_text`]) prefixed with
//!   [`PUBLIC_HASH_VERSION`]. Changing a secret reference leaves the hash
//!   unchanged; changing anything else changes it.
//!
//! The crate is pure: it performs no filesystem, network, clock or
//! environment access and never resolves a secret reference. Text longer
//! than [`MAX_DOCUMENT_BYTES`] (256 KiB) is refused before it is parsed. A
//! syntax error reports only its line and column, never the parser's
//! description, which can quote the input.
//!
//! `backup.age_recipients` accepts only post-quantum hybrid age recipients
//! (`age1pq1…`), checked structurally (lowercase Bech32, checksum, padding,
//! key length) without depending on `age`. The list holds at most
//! [`MAX_BACKUP_RECIPIENTS`] (32) entries, the most a Stratum node encrypts a
//! backup to.
//!
//! # Examples
//!
//! ```
//! use stratum_node_config::{preview, schema, ApplyClass, Change, ConfigDocument};
//!
//! // Start from the generated seed, which validates and holds the defaults.
//! let seed = schema().seed_text();
//! let before = ConfigDocument::parse(&seed)?;
//!
//! // Preview an edit and inspect it before committing anything.
//! let p = preview(&seed, &[Change::new("node.name", "alpha")])?;
//! assert!(p.errors().is_empty());
//! assert_eq!(p.strictest_apply_class(), Some(ApplyClass::Live));
//!
//! let after = p.into_document().ok_or("edit should be valid")?;
//! assert_eq!(after.config().node_name(), "alpha");
//! assert_ne!(after.public_hash(), before.public_hash());
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

mod config;
mod document;
mod edit;
mod error;
mod public;
mod recipient;
mod rules;
mod schema;
mod seed;
mod value;

pub use config::{Config, SecretRef};
pub use document::{ConfigDocument, MAX_DOCUMENT_BYTES};
pub use edit::{preview, Change, FieldChange, Preview, REDACTED};
pub use error::{PreviewError, ValidationError, ValidationErrorKind, ValidationErrors};
pub use public::{PublicConfig, PublicHash, PublicIter, PUBLIC_HASH_VERSION};
pub use schema::{
    schema, ApplyClass, FieldDescriptor, Fields, Schema, ValueType, MAX_BACKUP_RECIPIENTS,
    SCHEMA_VERSION,
};
pub use value::Value;

/// Compiles and runs the README example as a doctest.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;