Skip to content

Commit ba30e76

Browse files
committed
Allow ? to be used in script files without main()
This is done by specifying the Result<(), Box<dyn std::error::Error+Sync+Send>> return type. This aligns with what https://github.com/stevedonovan/runner is doing.
1 parent 98f081b commit ba30e76

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn split_input(
5353
let source = if source.lines().any(|line| line.starts_with("fn main()")) {
5454
source.to_string()
5555
} else {
56-
format!("fn main() {{\n{}\n}}", source)
56+
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
5757
};
5858
(manifest, source, templates::get_template("file")?, false)
5959
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use std::fs::File;
2+
3+
File::open("__rust-script-this-file-does-not-exist.txt")?;

tests/tests/script.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,11 @@ fn script_with_same_name_as_dependency() {
144144
)
145145
.unwrap()
146146
}
147+
148+
149+
#[test]
150+
fn script_without_main_question_mark() {
151+
let out = rust_script!("tests/data/script-without-main-question-mark.rs").unwrap();
152+
assert_eq!(out.stderr,
153+
"Error: Os { code: 2, kind: NotFound, message: \"No such file or directory\" }\n");
154+
}

0 commit comments

Comments
 (0)