36 releases

0.9.6 Nov 30, 2024
0.9.4 Aug 31, 2024
0.8.4 Jul 22, 2024
0.6.0 Feb 6, 2024
0.4.0 Jul 3, 2023

#1028 in Encoding

Download history 931/week @ 2024-08-26 703/week @ 2024-09-02 485/week @ 2024-09-09 366/week @ 2024-09-16 594/week @ 2024-09-23 632/week @ 2024-09-30 720/week @ 2024-10-07 488/week @ 2024-10-14 713/week @ 2024-10-21 866/week @ 2024-10-28 281/week @ 2024-11-04 533/week @ 2024-11-11 672/week @ 2024-11-18 1255/week @ 2024-11-25 762/week @ 2024-12-02 574/week @ 2024-12-09

3,293 downloads per month
Used in 21 crates (via schematic)

MIT license

51KB
1.5K SLoC

Schematic

Schematic is a library that provides:

  • A layered serde-driven configuration system with support for merge strategies, validation rules, environment variables, and more!
  • A schema modeling system that can be used to generate TypeScript types, JSON schemas, and more!

Both of these features can be used independently or together.

cargo add schematic

Get started: https://moonrepo.github.io/schematic

Configuration

  • Supports JSON, TOML, and YAML based configs via serde.
  • Load sources from the file system or secure URLs.
  • Source layering that merge into a final configuration.
  • Extend additional files through an annotated setting.
  • Field-level merge strategies with built-in merge functions.
  • Aggregated validation with built-in validate functions (provided by garde).
  • Environment variable parsing and overrides.
  • Beautiful parsing and validation errors (powered by miette).
  • Generates schemas that can be rendered to TypeScript types, JSON schemas, and more!

Define a struct or enum and derive the Config trait.

use schematic::Config;

#[derive(Config)]
struct AppConfig {
	#[setting(default = 3000, env = "PORT")]
	port: usize,

	#[setting(default = true)]
	secure: bool,

	#[setting(default = vec!["localhost".into()])]
	allowed_hosts: Vec<String>,
}

Then load, parse, merge, and validate the configuration from one or many sources. A source is either a file path, secure URL, or code block.

use schematic::{ConfigLoader, Format};

let result = ConfigLoader::<AppConfig>::new()
	.code("secure: false", Format::Yaml)?
	.file("path/to/config.yml")?
	.url("https://ordomain.com/to/config.yaml")?
	.load()?;

result.config;
result.layers;

Schemas

Define a struct or enum and derive or implement the Schematic trait.

use schematic::Schematic;

#[derive(Schematic)]
struct Task {
	command: String,
	args: Vec<String>,
	env: HashMap<String, String>,
}

Then generate output in multiple formats, like JSON schemas or TypeScript types, using the schema type information.

use schematic::schema::{SchemaGenerator, TypeScriptRenderer};

let mut generator = SchemaGenerator::default();
generator.add::<Task>();
generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?;

Dependencies

~0.7–2.8MB
~48K SLoC