Crates.io | schematic |
lib.rs | schematic |
version | 0.17.6 |
source | src |
created_at | 2023-05-12 22:15:24.839743 |
updated_at | 2024-11-03 02:07:15.002577 |
description | A layered serde configuration and schema library. |
homepage | https://moonrepo.github.io/schematic |
repository | https://github.com/moonrepo/schematic |
max_upload_size | |
id | 863337 |
size | 628,455 |
Schematic is a library that provides:
Both of these features can be used independently or together.
cargo add schematic
Get started: https://moonrepo.github.io/schematic
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;
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())?;