Skip to content

Commit 3f46046

Browse files
committed
Use one of the default README file names if they exist
1 parent b23390b commit 3f46046

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ impl TomlManifest {
12001200
)?;
12011201

12021202
let readme = match &project.readme {
1203-
None => Some(String::from("README.md")),
1203+
None => default_readme_from_package_root(package_root),
12041204
Some(value) => match value.as_str() {
12051205
"false" => None,
12061206
_ => Some(value.clone())
@@ -1522,6 +1522,19 @@ impl TomlManifest {
15221522
}
15231523
}
15241524

1525+
const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"];
1526+
1527+
/// 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.
1529+
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+
.nth(0)
1536+
}
1537+
15251538
/// Checks a list of build targets, and ensures the target names are unique within a vector.
15261539
/// If not, the name of the offending build target is returned.
15271540
fn unique_build_targets(targets: &[Target], package_root: &Path) -> Result<(), String> {

0 commit comments

Comments
 (0)