Skip to main content

Module codegen

Module codegen 

Source
Expand description

The generator of validation modules, for a build script (feature codegen).

A usual business application works with a fixed set of schemas that is known when the project is created: the invoices, orders, tax messages or bank files it accepts are defined by schemas that are part of the project, and they change with a release of the application, not while it runs. So the project can compile them when it is built. Its build.rs names them:

xsd_schema::codegen::Configuration::new()
    .schema("schema/maindoc/UBL-Invoice-2.1.xsd")
    .module("invoice")
    .process()?;

and its source declares the module with xsd_schema::include_validator!(pub invoice); (feature plan of the dependency). The generator compiles the schema, builds its validation tables and writes them to $OUT_DIR/<module>.rs, with every schema document it read embedded. The schema is checked when the project is built, the tables ship inside the binary, and there is no schema file to deploy and no schema to compile at start-up; what the module’s functions answer is in the plan module’s documentation. A program that receives its schemas while it runs does not use this: it compiles them with SchemaSetBuilder.

The build dependency and the dependency must be the same version of this crate: a module written by another version does not compile, and says so. The generator reads a schema as the xsd11 build of this crate does (the feature implies xsd11), so a schema error fails the build script, a pattern that is not an XSD regular expression included. The dependency needs plan, and xsd11 only for an XSD 1.1 module: the tables of an XSD 1.0 schema are read by a dependency with or without xsd11.

A build script does not fetch schemas: documents are read from the file system only, and a remote location (an http: import, say) fails the build with its name unless Configuration::map_location maps it to a file of the crate. Cargo is told to run the build script again when build.rs or any document it read changes. Before it writes the module, the generator compiles the embedded documents once more, from memory and by their keys, as the module’s fallback will: a module whose documents do not compile that way is not written.

The guide Generated Validation (doc/GENERATED_VALIDATION.md in the repository) has the costs, measured on three real schemas, and examples.

Structs§

Configuration
The settings of one generated module.