pub fn to_file(
    value: impl Serialize,
    target: impl AsRef<Path>
) -> Result<(), UnevalError>
Expand description

Writes generated Rust code to file.

This is probably the most common way to use uneval. When Cargo runs your crate’s build task, it sets the OUT_DIR environment variable to the path to build target directory (see Cargo reference for more). So, you can use it in two steps:

  1. Generate the Rust code and write it to temporary file:
let path: std::path::PathBuf = [
    std::env::var("OUT_DIR").expect("No build target path set"),
    "file_name.rs".into()
].iter().collect();
uneval::to_file(value, path).expect("Write failed");
  1. Include the generated Rust code wherever it is needed:
let value = include!(concat!(env!(OUT_DIR), "/file_name.rs"));