Skip to content

Commit 1dc66fb

Browse files
authored
Merge pull request #15 from fornwall/pub-fn-main
Pub fn main
2 parents bda2931 + 5d6d9eb commit 1dc66fb

File tree

7 files changed

+88
-57
lines changed

7 files changed

+88
-57
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest, macos-latest, windows-latest]
11-
rust: ["1.47.0", "1.48.0"]
11+
rust: ["1.47.0", "1.49.0"]
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- name: Checkout source

Cargo.lock

Lines changed: 57 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-script"
3-
version = "0.11.0"
3+
version = "0.12.0"
44
edition = "2018"
55
authors = ["Fredrik Fornwall <[email protected]>"]
66
description = "Command-line tool to run Rust \"scripts\" which can make use of crates."

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ fn try_main() -> MainResult<i32> {
413413
"dependency somehow has three parts?!"
414414
);
415415

416-
if name == "" {
416+
if name.is_empty() {
417417
return Err(("cannot have empty dependency package name").into());
418-
} else if version == "" {
418+
} else if version.is_empty() {
419419
return Err(("cannot have empty dependency version").into());
420420
}
421421

src/manifest.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ pub fn split_input(
4444
prelude_items: &[String],
4545
input_id: &OsString,
4646
) -> MainResult<(String, String)> {
47+
fn contains_main_method(line: &str) -> bool {
48+
line.starts_with("fn main()")
49+
|| line.starts_with("pub fn main()")
50+
|| line.starts_with("async fn main()")
51+
|| line.starts_with("pub async fn main()")
52+
}
53+
4754
let template_buf;
4855
let (part_mani, source, template, sub_prelude) = match *input {
4956
Input::File(_, _, content, _) => {
@@ -52,10 +59,7 @@ pub fn split_input(
5259
let (manifest, source) =
5360
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));
5461

55-
let source = if source
56-
.lines()
57-
.any(|line| line.starts_with("fn main()") || line.starts_with("async fn main()"))
58-
{
62+
let source = if source.lines().any(contains_main_method) {
5963
source.to_string()
6064
} else {
6165
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)

tests/data/pub-fn-main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! ```cargo
2+
//! [dependencies]
3+
//! boolinator = "=0.1.0"
4+
//! ```
5+
use boolinator::Boolinator;
6+
7+
pub fn main() {
8+
println!("--output--");
9+
println!("{:?}", true.as_some(1));
10+
}

tests/tests/script.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,12 @@ fn test_script_async_main() {
161161
)
162162
.unwrap()
163163
}
164+
165+
#[test]
166+
fn test_pub_fn_main() {
167+
let out = rust_script!("tests/data/pub-fn-main.rs").unwrap();
168+
scan!(out.stdout_output();
169+
("Some(1)") => ()
170+
)
171+
.unwrap()
172+
}

0 commit comments

Comments
 (0)