1 unstable release

0.1.0 Apr 18, 2019

#5 in #organization


Used in directree

Unlicense

6KB
126 lines

directree

A macro for expressing a directory tree at compile time.


An example

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())?;

But why?

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.

Dependencies

~1.5MB
~41K SLoC