Skip to content

Commit 2a31511

Browse files
authored
docs: add template for cargo script usage (#23)
* docs: add template for cargo script usage * test: fix tests
1 parent 3b4b2f6 commit 2a31511

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

README.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,90 @@
11
# rust-script-ext
22
Opinionated set of extensions for use with
3-
[`rust-script`](https://github.com/fornwall/rust-script).
3+
[`rust-script`](https://github.com/fornwall/rust-script) or
4+
[`cargo script`](https://github.com/rust-lang/rfcs/pull/3503).
45

56
Using `rust-script` to run Rust like a shell script is great!
67
This crate provides an opinionated set of extensions tailored towards common patterns in scripts.
78
These patterns include file reading, argument parsing, error handling.
89
The goal is for script writers to focus on the _business logic_, not implementing parsers, handling
910
errors, parsing arguments, etc.
1011

12+
## Using `rust-script`
1113
````sh
1214
$ cargo install rust-script
1315
..
14-
$ cat ./template.rs
16+
$ cat ./template-rust-script.rs
1517
#!/usr/bin/env -S rust-script -c
1618
//! You might need to chmod +x your script!
1719
//! ```cargo
1820
//! [dependencies.rust-script-ext]
1921
//! git = "https://github.com/kurtlawrence/rust-script-ext"
2022
//! rev = "e914bc0a04e9216ffdfd15c47f4c39b74d98bbeb"
2123
//! ```
22-
24+
// See <https://kurtlawrence.github.io/rust-script-ext/rust_script_ext/> for documentation
2325
use rust_script_ext::prelude::*;
2426

25-
fn main() {
27+
fn main() -> Result<()> {
2628
// fastrand comes from rust_script_ext::prelude::*
2729
let n = std::iter::repeat_with(|| fastrand::u32(1..=100))
2830
.take(5)
2931
.collect::<Vec<_>>();
3032

3133
println!("Here's 5 random numbers: {n:?}");
34+
Ok(())
3235
}
33-
$ ./template.rs
36+
$ ./template-rust-script.rs
3437
Here's 5 random numbers: [28, 97, 9, 23, 58]
3538
````
3639
40+
## Using `cargo script`
41+
````sh
42+
$ cat ./template-cargo-script.rs
43+
#!/usr/bin/env -S cargo +nightly -Zscript
44+
---
45+
[dependencies.rust-script-ext]
46+
git = "https://github.com/kurtlawrence/rust-script-ext"
47+
rev = "e914bc0a04e9216ffdfd15c47f4c39b74d98bbeb"
48+
---
49+
// You might need to chmod +x your script!
50+
// See <https://kurtlawrence.github.io/rust-script-ext/rust_script_ext/> for documentation
51+
use rust_script_ext::prelude::*;
52+
53+
fn main() -> Result<()> {
54+
// fastrand comes from rust_script_ext::prelude::*
55+
let n = std::iter::repeat_with(|| fastrand::u32(1..=100))
56+
.take(5)
57+
.collect::<Vec<_>>();
58+
59+
println!("Here's 5 random numbers: {n:?}");
60+
Ok(())
61+
}
62+
$ ./template-cargo-script.rs
63+
Here's 5 random numbers: [91, 65, 32, 75, 39]
64+
````
65+
3766
## Template Quickstart
3867
39-
`template.rs` contains a simple scaffold for a `rust-script[-ext]`:
68+
`template-rust-script.rs` contains a simple scaffold for use with `rust-script`:
69+
70+
```sh
71+
curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template-rust-script.rs -o my-script.rs
72+
chmod +x my-script.rs
73+
./my-script.rs
74+
```
75+
76+
`template-cargo-script.rs` contains a simple scaffold for use with `cargo-script`:
4077
4178
```sh
42-
curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template.rs -o my-script.rs
79+
curl -L https://github.com/kurtlawrence/rust-script-ext/raw/main/template-cargo-script.rs -o my-script.rs
4380
chmod +x my-script.rs
4481
./my-script.rs
4582
```
4683
84+
> `cargo script` does not (currently) set up an isolated environment when running the script, which
85+
> can cause errors if the script lives _within a Rust crate_.
86+
> I recommend using `rust-script` instead.
87+
4788
## What's included?
4889
4990
What `rust-script-ext` provides is continually evolving.
@@ -55,6 +96,8 @@ If you find something lacking, I encourage you to open a PR!
5596
5697
## Language Server Support
5798
99+
> Note: only tested with `rust-script`.
100+
58101
[`rscls`](https://github.com/MiSawa/rscls/) works as a middle-man LSP between rust-analyzer and
59102
rust-script.
60103
Below are instructions for getting LSP support using Neovim.

macros/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn cargs_expanding() {
3030
);
3131

3232
let a = cargs!(hello / path, "a literal",);
33-
assert_eq!(a, ["hello/path".to_string(), "a literal".to_string()]);
33+
assert_eq!(a, ["hello/path".to_string(), "\"a literal\"".to_string()]);
3434
}
3535

3636
#[test]

src/cmd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ mod tests {
378378
"macros",
379379
"src",
380380
"target",
381-
"template.rs",
381+
"template-cargo-script.rs",
382+
"template-rust-script.rs",
382383
]
383384
);
384385

template-cargo-script.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env -S cargo +nightly -Zscript
2+
---
3+
[dependencies.rust-script-ext]
4+
git = "https://github.com/kurtlawrence/rust-script-ext"
5+
rev = "e914bc0a04e9216ffdfd15c47f4c39b74d98bbeb"
6+
---
7+
// You might need to chmod +x your script!
8+
// See <https://kurtlawrence.github.io/rust-script-ext/rust_script_ext/> for documentation
9+
use rust_script_ext::prelude::*;
10+
11+
fn main() -> Result<()> {
12+
// fastrand comes from rust_script_ext::prelude::*
13+
let n = std::iter::repeat_with(|| fastrand::u32(1..=100))
14+
.take(5)
15+
.collect::<Vec<_>>();
16+
17+
println!("Here's 5 random numbers: {n:?}");
18+
Ok(())
19+
}
File renamed without changes.

0 commit comments

Comments
 (0)