preserves/doc/preserves-schema-rs.md

75 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2021-09-02 11:46:49 +00:00
---
title: preserves-schema-rs
---
The `preserves-schema-rs` program reads
[Preserves Schema](../preserves-schema.html) AST input files (such as
are produced by [`preserves-schemac`]({% link doc/preserves-schemac.md
%})). It produces a collection of Rust source files providing parsers,
unparsers, and Rust data structures reflecting the definitions in the
inputs.
## Using the compiler from `build.rs` instead
You will usually not need to use the `preserves-schema-rs`
command-line program. Instead, access the preserves-schema compiler
API from your `build.rs`. The following example is taken from
2023-10-27 12:11:38 +00:00
[`build.rs` for the `preserves-path` crate](https://gitlab.com/preserves/preserves/-/blob/af5de5b836ffc51999db93797d1995ff677cf6f8/implementations/rust/preserves-path/build.rs):
2021-09-02 11:46:49 +00:00
use preserves_schema::compiler::*;
use std::io::Error;
use std::path::PathBuf;
fn main() -> Result<(), Error> {
let buildroot = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let mut gen_dir = buildroot.clone();
gen_dir.push("src/schemas");
let mut c = CompilerConfig::new(gen_dir, "crate::schemas".to_owned());
let inputs = expand_inputs(&vec!["path.bin".to_owned()])?;
2023-10-27 12:11:38 +00:00
c.load_schemas_and_bundles(&inputs, &vec![])?;
2021-09-02 11:46:49 +00:00
compile(&c)
}
This approach also requires an `include!` from your main, hand-written
source tree. The following is a snippet from
2023-10-27 12:11:38 +00:00
[`preserves-path/src/lib.rs`](https://gitlab.com/preserves/preserves/-/blob/af5de5b836ffc51999db93797d1995ff677cf6f8/implementations/rust/preserves-path/src/lib.rs):
2021-09-02 11:46:49 +00:00
pub mod schemas {
include!(concat!(env!("OUT_DIR"), "/src/schemas/mod.rs"));
}
## Installation
The tool is
[written in Rust](https://crates.io/crates/preserves-schema).
[Install `cargo`.](https://doc.rust-lang.org/cargo/getting-started/installation.html)
Then, `cargo install preserves-schema`.
## Usage
2023-10-27 12:11:38 +00:00
preserves-schema 3.990.2
2021-09-02 11:46:49 +00:00
USAGE:
2023-10-27 12:12:57 +00:00
preserves-schema-rs [FLAGS] [OPTIONS] --output-dir <output-dir> --prefix <prefix>
[--] [input-glob]...
2021-09-02 11:46:49 +00:00
FLAGS:
2023-10-27 12:11:38 +00:00
-h, --help Prints help information
--rustfmt-skip
-V, --version Prints version information
2021-09-02 11:46:49 +00:00
OPTIONS:
--module <module>...
-o, --output-dir <output-dir>
-p, --prefix <prefix>
--support-crate <support-crate>
2023-10-27 12:11:38 +00:00
--xref <xref>...
2021-09-02 11:46:49 +00:00
ARGS:
<input-glob>...