Skip to content

Commit

Permalink
Merge pull request #97 from fornwall/avoid-shebang-line-number-mismatch
Browse files Browse the repository at this point in the history
Avoid shebang causing line number mismatch
  • Loading branch information
fornwall authored Apr 10, 2023
2 parents 6776198 + a22bf35 commit 79e8b18
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down
19 changes: 12 additions & 7 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n{} }}\n Ok(())\n}}", source)
};
(manifest, source, consts::FILE_TEMPLATE, false)
}
Expand Down Expand Up @@ -169,7 +173,8 @@ name = "n"
version = "0.1.0""#,
STRIP_SECTION
),
r#"fn main() {}"#
r#"//
fn main() {}"#
)
);

Expand Down Expand Up @@ -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),
}
}

Expand Down

0 comments on commit 79e8b18

Please sign in to comment.