Crates.io | directree_macros |
lib.rs | directree_macros |
version | 0.1.0 |
source | src |
created_at | 2019-04-18 20:31:42.328769 |
updated_at | 2019-04-18 20:31:42.328769 |
description | directree procedural macros |
homepage | https://git.chronicembetterment.org/chronic_embetterment/directree |
repository | https://git.chronicembetterment.org/chronic_embetterment/directree |
max_upload_size | |
id | 128721 |
size | 6,587 |
directree
A macro for expressing a directory tree at compile time.
The directree
macro can be used to express a file hierarchy like so:
use directree::directree;
mod paths {
directree! {
foo: "dir" {
bar: "file.toml",
// Note that an empty {} is allowed, but does nothing other than
// visually distinguish whether it's supposed to be a file or a
// directory.
baz: "subdir" {},
}
}
}
directree
turns this DSL into a series of modules and functions with the same
hierarchy:
mod paths {
fn foo() -> &'static std::path::Path {
std::path::Path::new("dir")
}
mod foo {
fn bar() -> &'static std::path::Path {
std::path::Path::new("dir/file.toml")
}
fn baz() -> &'static std::path::Path {
std::path::Path::new("dir/subdir")
}
}
}
This can then be used to obtain specified files and directories in normal code:
let foo_dir = std::fs::OpenOptions::new()
.read(true)
.open(crate::paths::foo())?;
let bar_file = std::fs::OpenOptions::new()
.read(true)
.open(crate::paths::foo::bar())?;
It's useful to be able to specify all files and directories your software needs access to in one place. Doing so provides your software with a single source of truth on what things are named on disk and where they are stored. Using this project will also allow the compiler to catch typos in module and function names instead of having to manually verify directories written by hand.