directree_macros

Crates.iodirectree_macros
lib.rsdirectree_macros
version0.1.0
sourcesrc
created_at2019-04-18 20:31:42.328769
updated_at2019-04-18 20:31:42.328769
descriptiondirectree procedural macros
homepagehttps://git.chronicembetterment.org/chronic_embetterment/directree
repositoryhttps://git.chronicembetterment.org/chronic_embetterment/directree
max_upload_size
id128721
size6,587
Charles Hall (CobaltCause)

documentation

https://docs.rs/directree_macros

README

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.

Commit count: 0

cargo fmt