diff --git a/Cargo.lock b/Cargo.lock index cf5ae60..cb462a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -433,7 +433,7 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "rust-script" -version = "0.24.0" +version = "0.25.0" dependencies = [ "clap", "dirs-next", diff --git a/Cargo.toml b/Cargo.toml index 6512eee..c69b7dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust-script" -version = "0.24.0" +version = "0.25.0" edition = "2021" rust-version = "1.64" authors = ["Fredrik Fornwall "] diff --git a/src/manifest.rs b/src/manifest.rs index 2c8a1aa..0bc81c0 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -34,14 +34,18 @@ pub fn split_input( let (part_mani, source, template, sub_prelude) = match input { Input::File(_, _, content) => { assert_eq!(prelude_items.len(), 0); - let content = strip_shebang(content); + let (content, shebang_used) = strip_shebang(content); let (manifest, source) = find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content)); let source = if contains_main_method(source) { - source.to_string() + if shebang_used { + format!("//\n{}", source) + } else { + source.to_string() + } } else { - format!("fn main() -> Result<(), Box> {{\n {{\n {} }}\n Ok(())\n}}", source) + format!("fn main() -> Result<(), Box> {{ {{\n{} }}\n Ok(())\n}}", source) }; (manifest, source, consts::FILE_TEMPLATE, false) } @@ -169,7 +173,8 @@ name = "n" version = "0.1.0""#, STRIP_SECTION ), - r#"fn main() {}"# + r#"// +fn main() {}"# ) ); @@ -395,11 +400,11 @@ fn main() {} /** Returns a slice of the input string with the leading shebang, if there is one, omitted. */ -fn strip_shebang(s: &str) -> &str { +fn strip_shebang(s: &str) -> (&str, bool) { let re_shebang: Regex = Regex::new(r"^#![^\[].*?(\r\n|\n)").unwrap(); match re_shebang.find(s) { - Some(m) => &s[m.end()..], - None => s, + Some(m) => (&s[m.end()..], true), + None => (s, false), } }