Function flatdata::generate

source ·
pub fn generate(
    schemas_path: impl AsRef<Path>,
    out_dir: impl AsRef<Path>
) -> Result<(), GeneratorError>
Expand description

A helper function wrapping the flatdata generator.

Can be used to write build.rs build scripts, generating outputs either from a single schema, or recursively from a folder of schemas.

schemas_path can either be a single file, or a filder containing schemas. In both cases the function will only handle files with the .flatdata extension.

Generated files are in the same relative location, e.g.

schemas_path/
├────────────example_a/
│            ├─────────my_schema.flatdata
│            └─────────my_other_schema.flatdata
└────────────example_b.flatdata

results in

out_dir/
├───────example_a/
│       ├─────────my_schema.rs
│       └─────────my_other_schema.rs
└───────example_b.rs

Examples

build.rs

use std::env;

fn main() {
    flatdata::generate("schemas_path/", &env::var("OUT_DIR").unwrap()).unwrap();
}

my_schema.rs

#![allow(dead_code)]

include!(concat!(env!("OUT_DIR"), "/example_a/my_schema.rs"));

// re-export if desired
pub use my_schema::*;

Development

If you are working on the generator, you can make sure your build.rs script picks up the source by setting FLATDATA_GENERATOR_PATH to point to the flatdata-generator folder.

Build

This method will try to install flatdata-generator in a python venv automatically You can provide your own generator by setting FLATDATA_GENERATOR_BIN to point to the flatdata-generator binary.