@@ -52,6 +52,7 @@ impl Layout {
52
52
53
53
try_add_file ( & mut bins, root_path. join ( "src" ) . join ( "main.rs" ) ) ;
54
54
try_add_files ( & mut bins, root_path. join ( "src" ) . join ( "bin" ) ) ;
55
+ try_add_mains_from_dirs ( & mut bins, root_path. join ( "src" ) . join ( "bin" ) ) ;
55
56
56
57
try_add_files ( & mut examples, root_path. join ( "examples" ) ) ;
57
58
@@ -74,6 +75,25 @@ fn try_add_file(files: &mut Vec<PathBuf>, file: PathBuf) {
74
75
files. push ( file) ;
75
76
}
76
77
}
78
+
79
+ // Add directories form src/bin which contain main.rs file
80
+ fn try_add_mains_from_dirs ( files : & mut Vec < PathBuf > , root : PathBuf ) {
81
+ if let Ok ( new) = fs:: read_dir ( & root) {
82
+ let new: Vec < PathBuf > = new. filter_map ( |i| i. ok ( ) )
83
+ // Filter only directories
84
+ . filter ( |i| {
85
+ i. file_type ( ) . map ( |f| f. is_dir ( ) ) . unwrap_or ( false )
86
+ // Convert DirEntry into PathBuf and append "main.rs"
87
+ } ) . map ( |i| {
88
+ i. path ( ) . join ( "main.rs" )
89
+ // Filter only directories where main.rs is present
90
+ } ) . filter ( |f| {
91
+ f. as_path ( ) . exists ( )
92
+ } ) . collect ( ) ;
93
+ files. extend ( new) ;
94
+ }
95
+ }
96
+
77
97
fn try_add_files ( files : & mut Vec < PathBuf > , root : PathBuf ) {
78
98
if let Ok ( new) = fs:: read_dir ( & root) {
79
99
files. extend ( new. filter_map ( |dir| {
@@ -505,7 +525,18 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Vec<TomlTarget> {
505
525
* bin == layout. root . join ( "src" ) . join ( "main.rs" ) {
506
526
Some ( name. to_string ( ) )
507
527
} else {
508
- bin. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |f| f. to_string ( ) )
528
+ // bin is either a source file or a directory with main.rs inside.
529
+ if bin. ends_with ( "main.rs" ) && !bin. ends_with ( "src/bin/main.rs" ) {
530
+ if let Some ( parent) = bin. parent ( ) {
531
+ // Use a name of this directory as a name for binary
532
+ parent. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |f| f. to_string ( ) )
533
+ } else {
534
+ None
535
+ }
536
+ } else {
537
+ // regular case, just a file in the bin directory
538
+ bin. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |f| f. to_string ( ) )
539
+ }
509
540
} ;
510
541
511
542
name. map ( |name| {
@@ -1444,9 +1475,9 @@ fn inferred_bin_path(bin: &TomlBinTarget,
1444
1475
package_root : & Path ,
1445
1476
bin_len : usize ) -> PathBuf {
1446
1477
// here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
1447
- // srb /bin/foo.rs or src/bin/main.rs
1478
+ // src /bin/foo.rs, src/bin/foo/main .rs or src/bin/main.rs
1448
1479
if bin_len == 1 {
1449
- let path = Path :: new ( "src" ) . join ( & format ! ( "main.rs" ) ) ;
1480
+ let path = Path :: new ( "src" ) . join ( "main.rs" ) ;
1450
1481
if package_root. join ( & path) . exists ( ) {
1451
1482
return path. to_path_buf ( )
1452
1483
}
@@ -1463,7 +1494,13 @@ fn inferred_bin_path(bin: &TomlBinTarget,
1463
1494
return path. to_path_buf ( )
1464
1495
}
1465
1496
1466
- return Path :: new ( "src" ) . join ( "bin" ) . join ( & format ! ( "main.rs" ) ) . to_path_buf ( )
1497
+ // check for the case where src/bin/foo/main.rs is present
1498
+ let path = Path :: new ( "src" ) . join ( "bin" ) . join ( bin. name ( ) ) . join ( "main.rs" ) ;
1499
+ if package_root. join ( & path) . exists ( ) {
1500
+ return path. to_path_buf ( )
1501
+ }
1502
+
1503
+ return Path :: new ( "src" ) . join ( "bin" ) . join ( "main.rs" ) . to_path_buf ( )
1467
1504
}
1468
1505
1469
1506
// bin_len > 1
@@ -1472,19 +1509,25 @@ fn inferred_bin_path(bin: &TomlBinTarget,
1472
1509
return path. to_path_buf ( )
1473
1510
}
1474
1511
1512
+ // we can also have src/bin/foo/main.rs, but the former one is preferred
1513
+ let path = Path :: new ( "src" ) . join ( "bin" ) . join ( bin. name ( ) ) . join ( "main.rs" ) ;
1514
+ if package_root. join ( & path) . exists ( ) {
1515
+ return path. to_path_buf ( )
1516
+ }
1517
+
1475
1518
if !has_lib {
1476
1519
let path = Path :: new ( "src" ) . join ( & format ! ( "{}.rs" , bin. name( ) ) ) ;
1477
1520
if package_root. join ( & path) . exists ( ) {
1478
1521
return path. to_path_buf ( )
1479
1522
}
1480
1523
}
1481
1524
1482
- let path = Path :: new ( "src" ) . join ( "bin" ) . join ( & format ! ( "main.rs" ) ) ;
1525
+ let path = Path :: new ( "src" ) . join ( "bin" ) . join ( "main.rs" ) ;
1483
1526
if package_root. join ( & path) . exists ( ) {
1484
1527
return path. to_path_buf ( )
1485
1528
}
1486
1529
1487
- return Path :: new ( "src" ) . join ( & format ! ( "main.rs" ) ) . to_path_buf ( )
1530
+ return Path :: new ( "src" ) . join ( "main.rs" ) . to_path_buf ( )
1488
1531
}
1489
1532
1490
1533
fn build_profiles ( profiles : & Option < TomlProfiles > ) -> Profiles {
0 commit comments