Skip to content

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

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

MathieuTricoire/macro_files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

macro_files

CI badge Crate badge Rustc badge

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

Installation

[dependencies]
macro_files = "0.1"

Version requirement: rustc 1.56+

Examples

Create directories and files

fn project_readme(project_name: &str) -> String {
    format!("# {}", project_name)
}
let project_name = "Project name".to_string();
let adr_directory = "adr";

let temp_dir = macro_files::tempfile::tempdir().unwrap();
macro_files::create!({
   temp_dir.path(): {
        "README.md": project_readme(&project_name),
        ".adr-dir": adr_directory,
        adr_directory: {
            "templates": {
                "template.md": "# ADR Template",
            }
        },
        "LICENSE": "MIT"
    },
}).unwrap();

let file_contents = std::fs::read(temp_dir.path().join("LICENSE")).unwrap();
assert_eq!(
    String::from_utf8_lossy(&file_contents),
    "MIT"
);

// Macro expands as:
// {
//     #[allow(unused_variables)]
//     let path = ::std::path::PathBuf::default();
//     {
//         let path = &path.join(temp_dir.path());
//         $crate::create_dir(&path).and_then(|_| {
//             $crate::write_file(path.join("README.md"), (project_readme(&project_name)))
//                 .and_then(|_| {
//                     $crate::write_file(path.join(".adr-dir"), adr_directory).and_then(|_| {
//                         {
//                             let path = &path.join(adr_directory);
//                             $crate::create_dir(&path).and_then(|_| {
//                                 let path = &path.join("templates");
//                                 $crate::create_dir(&path).and_then(|_| {
//                                     $crate::write_file(
//                                         path.join("template.md"),
//                                         "# ADR Template",
//                                     )
//                                     .and_then(|_| Ok::<(), ::std::io::Error>(()))
//                                 })
//                             })
//                         }
//                         .and_then(|_| $crate::write_file(path.join("LICENSE"), "MIT"))
//                     })
//                 })
//         })
//     }
//     .and_then(|_| 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).

let temp_dir = macro_files::create_temp!({
   "README.md": "# Project name",
   "LICENSE": "MIT",
}).unwrap();

let file_contents = std::fs::read(temp_dir.path().join("README.md")).unwrap();
assert_eq!(
    String::from_utf8_lossy(&file_contents),
    "# 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.

About

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

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Languages