Skip to content

Commit

Permalink
Do not require 'fn main()' in script files
Browse files Browse the repository at this point in the history
Surround the script file with 'fn main() { .. }' if no line starts
with 'fn main()'.
  • Loading branch information
fornwall committed Oct 31, 2020
1 parent 6c608ae commit 58793ac
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,24 @@ pub fn split_input(
let (manifest, source) =
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));

let source = if source.lines().any(|line| line.starts_with("fn main()")) {
source.to_string()
} else {
format!("fn main() {{\n{}\n}}", source)
};
(manifest, source, templates::get_template("file")?, false)
}
Input::Expr(content, template) => {
template_buf = templates::get_template(template.unwrap_or("expr"))?;
let (manifest, template_src) = find_embedded_manifest(&template_buf)
.unwrap_or((Manifest::Toml(""), &template_buf));
(manifest, content, template_src.into(), true)
(manifest, content.to_string(), template_src.into(), true)
}
Input::Loop(content, count) => {
let templ = if count { "loop-count" } else { "loop" };
(
Manifest::Toml(""),
content,
content.to_string(),
templates::get_template(templ)?,
true,
)
Expand All @@ -71,6 +76,7 @@ pub fn split_input(

let mut prelude_str;
let mut subs = HashMap::with_capacity(2);

subs.insert(consts::SCRIPT_BODY_SUB, &source[..]);

if sub_prelude {
Expand Down
1 change: 0 additions & 1 deletion tests/data/script-full-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! [dependencies]
//! boolinator = "=0.1.0"
//! ```
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("--output--");
Expand Down
13 changes: 13 additions & 0 deletions tests/data/script-full-line-without-main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env rust-script
/*!
This is merged into a default manifest in order to form the full package manifest:
```cargo
[dependencies]
boolinator = "=0.1.0"
```
*/
use boolinator::Boolinator;

println!("--output--");
println!("{:?}", true.as_some(1));
1 change: 0 additions & 1 deletion tests/data/script-full-line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ This is merged into a default manifest in order to form the full package manifes
boolinator = "=0.1.0"
```
*/
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("--output--");
Expand Down
9 changes: 9 additions & 0 deletions tests/data/script-short-without-main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// cargo-deps: boolinator="=0.1.0"
// You can also leave off the version number, in which case, it's assumed
// to be "*". Also, the `cargo-deps` comment *must* be a single-line
// comment, and it *must* be the first thing in the file, after the
// hashbang.
use boolinator::Boolinator;

println!("--output--");
println!("{:?}", true.as_some(1));
1 change: 0 additions & 1 deletion tests/data/script-short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// to be "*". Also, the `cargo-deps` comment *must* be a single-line
// comment, and it *must* be the first thing in the file, after the
// hashbang.
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("--output--");
Expand Down
18 changes: 18 additions & 0 deletions tests/tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ fn test_script_full_line() {
.unwrap()
}

#[test]
fn test_script_full_line_without_main() {
let out = cargo_script!("tests/data/script-full-line-without-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}

#[test]
fn test_script_invalid_doc_comment() {
let out = cargo_script!("tests/data/script-invalid-doc-comment.rs").unwrap();
Expand Down Expand Up @@ -64,6 +73,15 @@ fn test_script_short() {
.unwrap()
}

#[test]
fn test_script_short_without_main() {
let out = cargo_script!("tests/data/script-short-without-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}

#[test]
fn test_script_test() {
let out = cargo_script!("--test", "tests/data/script-test.rs").unwrap();
Expand Down

0 comments on commit 58793ac

Please sign in to comment.