diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d4d27f9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,208 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "clap" +version = "4.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06badb543e734a2d6568e19a40af66ed5364360b9226184926f89d229b4b4267" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f169caba89a7d512b5418b09864543eeb4d497416c917d7137863bd2076ad" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.135" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" + +[[package]] +name = "macrokata" +version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "once_cell" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ff145de --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "macrokata" +version = "0.1.0" +edition = "2021" +default-run = "macrokata" + +[[bin]] +name = "macrokata" +path = "src/main.rs" + +[[bin]] +name = "01" +path = "exercises/01_my_first_macro/main.rs" + +[[bin]] +name = "01_soln" +path = "exercises/01_my_first_macro/solutions/main.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.0.17", features = ["derive"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..202e9c7 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# MacroKata + +MacroKata is a set of exercises which you can use to learn how +to write macros in rust. Unlike most exercises, not only is your goal +to produce the same output with your code as with the files +in solution.rs; but it should *also* be to have your expanded code be the same +as `result.rs`. + +You should complete the kata in order, as they increase in +difficulty, and depend on previous kata. + +## Getting Started + +Clone this repository: + +``` sh +$ git clone https://www.github.com/tfpk/macrokata/ +``` + +You will also need to install the rust "nightly" toolchain, so that we can show +expanded macros. + +``` sh +$ rustup toolchain install nightly +``` + +Build the main binary provided with this repo: + +``` sh +$ cargo build +``` + +You can find the first kata (`my_first_macro`) inside `tasks/01_my_first_macro`. +Read the `README.md` file, and get started by editing the `main.rs` file. + +To test your code, run: + +``` sh +$ cargo run 01_my_first_macro +``` diff --git a/exercises/01_my_first_macro/README.md b/exercises/01_my_first_macro/README.md new file mode 100644 index 0000000..30d26b5 --- /dev/null +++ b/exercises/01_my_first_macro/README.md @@ -0,0 +1,59 @@ +# Exercise 1: My First Macro + +Welcome to this introduction to Rust's Macro system. +To complete each exercise (including this one), you should: + +* [ ] Read the `README.md` file, to understand what your goal is. +* [ ] Try and complete the `main.rs` file. +* [ ] Run the binary in this file, `cargo run 01_my_first_macro`. +* [ ] Get your code to compile, run, and have the main file look the same as `result.rs` + + +## What are Macros? + +Rust's macros are a way of automatically generating code before the compiler compiles it. +Because the generation happens before the compiler does anything; it doesn't +care about types; and it can output any Rust code you'd like. + +This allows you to break some of the syntax rules rust imposes on you. For example, +Rust does not allow "variadic" functions -- functions with variable numbers of +arguments. This makes a `println` function impossible -- it can take any number +of arguments (`println!("hello")` and `println("{}", 123)`, for example). + +Rust gets around this rule by using a `println!` macro. Before `println!` is compiled, +Rust rewrites it so it takes a single array of arguments. That way, even though +it looks to you like there are multiple arguments, once it's compiled there's always +just one array. + +Macros can range from very simple -- reducing duplicated code; to very complex, implementing the +whole of HTML inside of Rust. This guide aims to build you up from the simple to the complex. + +As you may have guessed, you've already used them -- `println!` for example, is a macro. +`vec![]` is as well. In general, macros always have a name. To run a macro, call it's name +with a bang (`!`) afterwards, and then brackets (any of `()`, `[]` or `{}`) containing +arguments. + +In other words, to run the macro `my_macro`, you'd say `my_macro!()` or `my_macro![]` or `my_macro!{}`. + +## How do I create one? + +The simplest form of macro looks like this: + +``` rust +macro_rules! my_macro { + () => { + my_code(); + } +} +``` + +The `macro_rules!` instructs the compiler that there is a new macro you are defining. +It is followed by the name of the macro, `my_macro`. +The next line lets you specify variables for your macro, but for now we'll ignore it. +Any code inside the curly brackets is what Rust will replace `my_macro!()` with. + +So, `my_macro!()` will be replaced by `my_code();` + +## See it for yourself + +You now know everything you need to in order to complete `01_my_first_macro`. diff --git a/exercises/01_my_first_macro/main.rs b/exercises/01_my_first_macro/main.rs new file mode 100644 index 0000000..a2b2d12 --- /dev/null +++ b/exercises/01_my_first_macro/main.rs @@ -0,0 +1,14 @@ +////////// DO NOT CHANGE BELOW HERE ///////// +// This function should be called by the `show_output!()` macro +fn show_output() { + println!("I should appear as the output.") +} +////////// DO NOT CHANGE ABOVE HERE ///////// + +// TODO: create `show_output!()` macro. + +////////// DO NOT CHANGE BELOW HERE ///////// + +fn main() { + show_output!() +} diff --git a/exercises/01_my_first_macro/result.rs b/exercises/01_my_first_macro/result.rs new file mode 100644 index 0000000..29695aa --- /dev/null +++ b/exercises/01_my_first_macro/result.rs @@ -0,0 +1,3 @@ +fn main() { + show_output() +} diff --git a/exercises/01_my_first_macro/solutions/main.rs b/exercises/01_my_first_macro/solutions/main.rs new file mode 100644 index 0000000..da15718 --- /dev/null +++ b/exercises/01_my_first_macro/solutions/main.rs @@ -0,0 +1,12 @@ +// This function should be called by the `show_output!()` macro +fn show_output() { + println!("I should appear as the output.") +} + +macro_rules! show_output { + () => {show_output()} +} + +fn main() { + show_output!() +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f9504a4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,66 @@ +use clap::{Parser, Subcommand}; +use std::process::{Command, Stdio}; +use std::error::Error; + +#[derive(Subcommand, Debug)] +enum SubCommands { + Run { + /// The name of the exercise to run. + exercise: String + } +} + +/// MacroKata is a set of exercises to learn how to use +/// macros well. +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Choose what MacroKata does. + #[clap(subcommand)] + command: SubCommands, +} + +fn run(_exercise: String) -> Result<(), Box> { + println!("This is the expansion you produced, along with any errors"); + println!(""); + + Command::new("cargo") + .arg("expand") + .arg("--bin") + .arg("01") + .arg("main") + .spawn() + .unwrap() + .wait() + .unwrap(); + + println!(""); + println!("The expansion we expected is:"); + println!(""); + + + Command::new("cargo") + .arg("expand") + .arg("--bin") + .arg("01_soln") + .arg("main") + .stderr(Stdio::null()) + .spawn() + .unwrap() + .wait() + .unwrap(); + + // expand + // + Ok(()) +} + +fn main() -> Result<(), Box> { + let args = Args::parse(); + + match args.command { + SubCommands::Run { exercise } => { + run(exercise) + } + } +}