Skip to content

Commit 6776198

Browse files
authored
Merge pull request #96 from fornwall/main-and-spaces
Detect main function even with spaces after
2 parents 4e473e6 + 09ae600 commit 6776198

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/manifest.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ pub fn split_input(
2626
script_name: &str,
2727
toolchain: Option<String>,
2828
) -> MainResult<(String, String)> {
29-
fn contains_main_method(line: &str) -> bool {
30-
let line = line.trim_start();
31-
line.starts_with("fn main(")
32-
|| line.starts_with("pub fn main(")
33-
|| line.starts_with("async fn main(")
34-
|| line.starts_with("pub async fn main(")
29+
fn contains_main_method(source: &str) -> bool {
30+
let re_shebang: Regex = Regex::new(r"(?m)^ *(pub )?(async )?fn main *\(").unwrap();
31+
re_shebang.is_match(source)
3532
}
3633

3734
let (part_mani, source, template, sub_prelude) = match input {
@@ -41,7 +38,7 @@ pub fn split_input(
4138
let (manifest, source) =
4239
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));
4340

44-
let source = if source.lines().any(contains_main_method) {
41+
let source = if contains_main_method(source) {
4542
source.to_string()
4643
} else {
4744
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)

tests/data/script-main-with-spaces.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main () {
2+
println!("--output--");
3+
println!("Hello, World!");
4+
}

tests/tests/script.rs

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ fn test_script_full_line_without_main() {
3434
.unwrap()
3535
}
3636

37+
#[test]
38+
fn test_script_main_with_space() {
39+
let out = rust_script!("tests/data/script-main-with-spaces.rs").unwrap();
40+
scan!(out.stdout_output();
41+
("Hello, World!") => ()
42+
)
43+
.unwrap()
44+
}
45+
3746
#[test]
3847
fn test_script_invalid_doc_comment() {
3948
let out = rust_script!("tests/data/script-invalid-doc-comment.rs").unwrap();

0 commit comments

Comments
 (0)