[−][src]Function odata_client_codegen::write_module_build_artifact
pub fn write_module_build_artifact(
metadata_xml_path: &str,
target_module_path: &str,
service_url: &str,
apply_rustfmt: bool,
construct_config: ConstructConfig,
entity_model_filter: Option<&mut dyn EntityModelFilter>
) -> Result<(), Error>
Convenience function to be used in a build script (build.rs
). Generates an OData client module
from the provided metadata XML, and writes to a location in the build output directory. Source
XML filepath and target code filepath are specified relative to the crate/workspace root. To use
the generated module, the user should manually declare a bodiless module within their code, and
add the #[path = "..."]
attribute, specifying the path to the generated module, relative to
the consuming source file. E.g.:
// Cargo.toml
[dependencies]
odata_client = "*"
[build-dependencies]
odata_client_codegen = "*"
// build.rs use odata_client_codegen::{write_module_build_artifact, ConstructConfig, ItemNameWhitelist}; fn main() { // Generate code items for entity set "Foo" and singleton "Bar" only let mut item_filter = ItemNameWhitelist::new() .with_entity_sets(vec!["Foo"]) .with_singletons(vec!["Bar"]); // Generate module & write to `my_service.rs` write_module_build_artifact( "./my_service_metadata.xml", "./target/odata_clients/my_service.rs", "https://example.ru/odata_api/", true, ConstructConfig::default(), Some(&mut item_filter), ).unwrap(); }
// src/main.rs #[path = "../target/odata_clients/my_service.rs"] mod my_service;
This function emits rerun-if-changed
directives, which disables the default behaviour of
re-running the build script on any included file change.
The generated file can be formatted using rustfmt by setting the apply_rustfmt
argument to
true
. If the rustfmt
executable is not available in the executing shell's path, set this
argument to false
.
N.B. Writing to a directory other than that specified by the OUT_DIR
env var is
unsupported usage of build scripts. But it works, and specifying a static target path allows for
using the #[path = "..."]
attribute (or even writing the module straight into the src
directory). This provides better goto & autocomplete support from rust-analyzer.