macro_files

![CI badge] ![Crate badge] ![Rustc badge]

Macro consuming JSON like data structures to create directories and files at runtime.

Installation

toml [dependencies] macro_files = "0.1"

Version requirement: rustc 1.56+

Examples

Create directories and files

```rust fn projectreadme(projectname: &str) -> String { format!("# {}", projectname) } let projectname = "Project name".tostring(); let adrdirectory = "adr";

let tempdir = macrofiles::tempfile::tempdir().unwrap(); macrofiles::create!({ tempdir.path(): { "README.md": projectreadme(&projectname), ".adr-dir": adrdirectory, adrdirectory: { "templates": { "template.md": "# ADR Template", } }, "LICENSE": "MIT" }, }).unwrap();

let filecontents = std::fs::read(tempdir.path().join("LICENSE")).unwrap(); asserteq!( String::fromutf8lossy(&filecontents), "MIT" );

// Macro expands as: // { // #[allow(unusedvariables)] // let path = ::std::path::PathBuf::default(); // { // let path = &path.join(tempdir.path()); // $crate::createdir(&path).andthen(|| { // $crate::writefile(path.join("README.md"), (projectreadme(&projectname))) // .andthen(|| { // $crate::writefile(path.join(".adr-dir"), adrdirectory).andthen(|| { // { // let path = &path.join(adrdirectory); // $crate::createdir(&path).andthen(|| { // let path = &path.join("templates"); // $crate::createdir(&path).andthen(|| { // $crate::writefile( // path.join("template.md"), // "# ADR Template", // ) // .andthen(|| Ok::<(), ::std::io::Error>(())) // }) // }) // } // .andthen(|| $crate::writefile(path.join("LICENSE"), "MIT")) // }) // }) // }) // } // .andthen(|_| Ok::<(), ::std::io::Error>(())) // } ```

Create directories and files within a temporary directory.

This requires the default feature tempfile that uses the [tempfile] crate.

The macro will return a [tempfile::TempDir] struct, the temporary directory will lives as long as the returned [tempfile::TempDir] struct is not dropped (see documentation).

```rust let tempdir = macrofiles::create_temp!({ "README.md": "# Project name", "LICENSE": "MIT", }).unwrap();

let filecontents = std::fs::read(tempdir.path().join("README.md")).unwrap(); asserteq!( String::fromutf8lossy(&filecontents), "# Project name" ); ```


License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.