Crate premortem

Crate premortem 

Source
Expand description

Premortem: A configuration library that performs a premortem on your app’s config.

Premortem validates your configuration before your application runs, finding all the ways it could die from bad config upfront. It uses stillwater’s functional patterns for error accumulation and composable validation.

§Core Concepts

  • Error Accumulation: Find ALL configuration errors, not just the first one
  • Source Layering: Merge config from files and environment variables
  • Testable I/O: Dependency injection via ConfigEnv trait
  • Type Safety: Deserialize into your types with full validation

§Quick Start

use premortem::prelude::*;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct AppConfig {
    host: String,
    port: u16,
}

impl Validate for AppConfig {
    fn validate(&self) -> ConfigValidation<()> {
        if self.port > 0 {
            Validation::Success(())
        } else {
            Validation::fail_with(ConfigError::ValidationError {
                path: "port".to_string(),
                source_location: None,
                value: Some(self.port.to_string()),
                message: "port must be positive".to_string(),
            })
        }
    }
}

fn main() -> Result<(), ConfigErrors> {
    let config = Config::<AppConfig>::builder()
        .source(Toml::file("config.toml"))
        .source(Env::new().prefix("APP"))
        .build()?;

    println!("Running on {}:{}", config.host, config.port);
    Ok(())
}

§Import Patterns

For most users, import the prelude:

use premortem::prelude::*;

§Selective Imports

Import only what you need:

use premortem::{Config, Validate};
use premortem::error::ConfigErrors;

§Advanced: Direct Stillwater Access

For custom sources or advanced patterns:

use premortem::prelude::*;
use stillwater::Effect;  // Direct stillwater access

§Architecture

Premortem follows the “pure core, imperative shell” pattern:

  • Pure Core: Value merging, deserialization, and validation are pure functions
  • Imperative Shell: I/O operations use the ConfigEnv trait for dependency injection

This architecture enables:

  • Easy unit testing with MockEnv
  • Composable validation with error accumulation
  • Clear separation of concerns

§Module Structure

  • prelude: Convenient re-exports for common usage
  • config: Config and ConfigBuilder for loading configuration
  • error: Error types (ConfigError, ConfigErrors, ConfigValidation)
  • value: Value enum for intermediate representation
  • source: Source trait and ConfigValues container
  • env: ConfigEnv trait and MockEnv for testing
  • validate: Validate trait for custom validation

§Stillwater Integration

Premortem uses these stillwater types:

TypeUsage
Validation<T, E>Error accumulation for config errors
NonEmptyVec<T>Guaranteed non-empty error lists
SemigroupCombining errors from multiple sources

These are re-exported from the prelude for convenience.

Re-exports§

pub use config::Config;
pub use config::ConfigBuilder;
pub use env::ConfigEnv;
pub use env::MockEnv;
pub use env::RealEnv;
pub use error::group_by_source;
pub use error::ConfigError;
pub use error::ConfigErrors;
pub use error::ConfigValidation;
pub use error::ConfigValidationExt;
pub use error::SourceErrorKind;
pub use error::SourceLocation;
pub use pretty::ColorOption;
pub use pretty::PrettyPrintOptions;
pub use pretty::ValidationExt;
pub use source::merge_config_values;
pub use source::ConfigValues;
pub use source::Source;
pub use trace::TraceBuilder;
pub use trace::TracedConfig;
pub use trace::TracedValue;
pub use trace::ValueTrace;
pub use validate::validators;
pub use validate::current_source_location;
pub use validate::custom;
pub use validate::validate_field;
pub use validate::validate_nested;
pub use validate::validate_optional_nested;
pub use validate::with_validation_context;
pub use validate::SourceLocationMap;
pub use validate::Validate;
pub use validate::ValidationContext;
pub use validate::Validator;
pub use validate::When;
pub use value::ConfigValue;
pub use value::Value;
pub use sources::Env;
pub use sources::Toml;
pub use sources::Defaults;
pub use sources::PartialDefaults;

Modules§

config
Core Config type and ConfigBuilder.
env
ConfigEnv trait for testable I/O.
error
Error types for the premortem configuration library.
prelude
Convenient re-exports for common premortem usage.
pretty
Pretty printing for configuration errors.
source
Configuration source trait and types.
sources
Configuration source implementations.
trace
Value tracing and origin tracking for debugging configuration issues.
validate
Validation trait and validators for configuration types.
value
Value types for configuration representation.

Structs§

NonEmptyVec
A non-empty vector guaranteed to contain at least one element.

Enums§

Validation
A validation that either succeeds with a value or fails with accumulated errors

Traits§

Semigroup
A type that supports an associative binary operation

Derive Macros§

DeriveValidate
Derive the Validate trait for a struct.