fornwall / rust-script

Run Rust files and expressions as scripts without any setup or compilation step.
https://rust-script.org
Apache License 2.0
1.21k stars 41 forks source link

Option to show macro expansion of input #47

Closed lilyball closed 1 year ago

lilyball commented 2 years ago

I'd love a flag that makes this print the macro expansion of the input instead of compiling and running it. This could be done as a flag like --macro-expand that selects the nightly toolchain (if not otherwise set) and runs cargo rustc --profile=check -- -Zunpretty=expanded.

Alternatives considered

rust-script could have a flag for simply changing the cargo command being run, such that if I have cargo-expand installed I could write something like rust-script --cargo-cmd expand script.rs. This is more flexible (e.g. I could also use it to run cargo clippy or cargo check if I'm just trying to see any warnings or errors), though it does require the user to install cargo-expand first.

With this approach the --test and --bench flags could potentially be obsoleted in favor of just specifying the test and bench cargo commands, though in this case the syntax for specifying the command should be simpler. I notice that right now rust-script lists its syntax as rust-script [OPTIONS] [--] [script]... but if I actually run rust-script foo bar it treats foo as a script and ignores bar completely (also it indicates script as being optional and yet it is not). This could be updated to the syntax rust-script [OPTIONS] [--] [cmd] <script> instead such that I could write rust-script test foo.rs to run cargo test or rust-script expand foo.rs to run cargo expand.

fornwall commented 1 year ago

Version 0.23.0 has just been released.

There the -p/--project parameter can be used to generate the package and print the package path, without doing any build step.

This allows things such as cargo tree --manifest-path $(rust-script -p test.rs)/Cargo.toml or cd "$(rust-script --package script.ers), so should provide flexibility.

Let me know how it works!