Expand description
§fixture-tree
Generate Rust source code that mirrors a filesystem directory as a module tree, providing zero-cost accessors for file paths and contents at compile time.
fixture-tree is intended for use from build scripts (build.rs). It walks a
directory of fixture files (test data, configs, model weights, etc.) and emits a
.rs file where every directory becomes a mod and every file becomes a function
returning its path and, optionally, its contents via include_str! or
include_bytes!.
This can be particularly useful if you find yourself using a lot of static files for testing specific use-cases. This allows you to use code linting to easily traverse your fixture file structure and will result in a compilation error if you mix around file paths.
§Quick start
// build.rs
fn main() {
fixture_tree::Config::new()
.from_path("fixtures") // relative to CARGO_MANIFEST_DIR
.with_ext("json") // only include .json files
.with_ext_as_string("json") // embed contents via include_str!
.build()
.unwrap()
.generate_fixtures()
.unwrap();
}Then in your library or test code:
include!(concat!(env!("OUT_DIR"), "/fixture_tree_autogen.rs"));
#[test]
fn read_fixture() {
let (path, contents) = configs::pass::basic();
assert!(path.exists());
assert!(contents.contains("ok"));
}§Path handling
When the source directory is under CARGO_MANIFEST_DIR (the typical case),
generated paths use concat!(env!("CARGO_MANIFEST_DIR"), "/...") so they
resolve correctly on any machine. If the source directory is outside the
manifest (e.g. a system temp directory), absolute paths are emitted instead.
§Filtering
Files can be filtered in two ways:
- By extension —
Config::with_ext/Config::with_extsrestrict which file extensions are walked. An empty list means “all extensions”. - By regex (requires the
regexfeature) - [Config::with_allow_pattern] / [Config::with_allow_patterns] match against the file’s path relative to the source root. When at least one regex is configured a file must match any of them to be included.
Entire subtrees can be excluded with Config::without_path /
Config::without_paths, which compare against the directory’s relative path.
§Generated code shape
For each directory a path() function is emitted. For each matched file a
function named after the file stem (lowercased, dashes replaced with
underscores) is emitted. Files registered as string extensions return
(&'static Path, &'static str), binary extensions return
(&'static Path, &'static [u8]) and files not registered as binary or strings
just return a &'static Path.
Empty directories are pruned from the
output.
Structs§
- Config
- Builder for configuring a
FixtureTree. - Directory
- In-memory representation of a scanned directory.
- Fixture
- A discovered fixture file.
- Fixture
Tree - The top-level handle returned by
Config::build.
Enums§
- Error
- Errors that can occur when building or generating a
FixtureTree.