Crates.io | schematic |
lib.rs | schematic |
version | |
source | src |
created_at | 2023-05-12 22:15:24.839743 |
updated_at | 2024-11-30 07:16:25.51581 |
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 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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())?;