@@ -1199,19 +1199,11 @@ impl TomlManifest {
1199
1199
project. namespaced_features . unwrap_or ( false ) ,
1200
1200
) ?;
1201
1201
1202
- let readme = match & project. readme {
1203
- None => default_readme_from_package_root ( package_root) ,
1204
- Some ( value) => match value. as_str ( ) {
1205
- "false" => None ,
1206
- _ => Some ( value. clone ( ) )
1207
- }
1208
- } ;
1209
-
1210
1202
let metadata = ManifestMetadata {
1211
1203
description : project. description . clone ( ) ,
1212
1204
homepage : project. homepage . clone ( ) ,
1213
1205
documentation : project. documentation . clone ( ) ,
1214
- readme,
1206
+ readme : readme_for_project ( package_root , project ) ,
1215
1207
authors : project. authors . clone ( ) . unwrap_or_default ( ) ,
1216
1208
license : project. license . clone ( ) ,
1217
1209
license_file : project. license_file . clone ( ) ,
@@ -1522,17 +1514,37 @@ impl TomlManifest {
1522
1514
}
1523
1515
}
1524
1516
1517
+ /// Returns the name of the README file for a `TomlProject`.
1518
+ fn readme_for_project ( package_root : & Path , project : & Box < TomlProject > ) -> Option < String > {
1519
+ match & project. readme {
1520
+ None => default_readme_from_package_root ( package_root) ,
1521
+ Some ( value) => match value. as_str ( ) {
1522
+ "false" => None ,
1523
+ _ => Some ( value. clone ( ) )
1524
+ }
1525
+ }
1526
+ }
1527
+
1525
1528
const DEFAULT_README_FILES : [ & str ; 3 ] = [ "README.md" , "README.txt" , "README" ] ;
1526
1529
1527
1530
/// Checks if a file with any of the default README file names exists in the package root.
1528
- /// If so, returns a String representing that name.
1531
+ /// If so, returns a ` String` representing that name.
1529
1532
fn default_readme_from_package_root ( package_root : & Path ) -> Option < String > {
1530
- DEFAULT_README_FILES
1531
- . iter ( )
1532
- . map ( |& fname| package_root. join ( Path :: new ( fname) ) )
1533
- . filter ( |path| path. is_file ( ) )
1534
- . flat_map ( |path| path. file_name ( ) . map ( |fname| fname. to_string_lossy ( ) . into_owned ( ) ) )
1535
- . next ( )
1533
+ _default_readme_from_package_root ( package_root) . ok ( )
1534
+ }
1535
+
1536
+ fn _default_readme_from_package_root ( package_root : & Path ) -> CargoResult < String > {
1537
+ for entry in package_root. read_dir ( ) ? {
1538
+ let entry = entry?;
1539
+
1540
+ let fname = entry. file_name ( ) ;
1541
+
1542
+ if entry. metadata ( ) ?. is_file ( ) && DEFAULT_README_FILES . contains ( & fname. to_str ( ) . unwrap ( ) ) {
1543
+ return Ok ( fname. into_string ( ) . map_err ( |_| anyhow ! ( "Could not convert the README's file name into a String" ) ) ?) ;
1544
+ }
1545
+ }
1546
+
1547
+ Err ( anyhow ! ( "No files with the default README file names found in the package root." ) )
1536
1548
}
1537
1549
1538
1550
/// Checks a list of build targets, and ensures the target names are unique within a vector.
0 commit comments