Skip to content

Commit 5c09ad1

Browse files
committed
Fix: unavailable author for cargo new
- if author name and email not found from config or env variables, defaults to an empty author list authors = [] - simplified selection of name + email from available choices in (fn mk)
1 parent 15f2cdb commit 5c09ad1

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -645,27 +645,37 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
645645
init_vcs(path, vcs, config)?;
646646
write_ignore_file(path, &ignore, vcs)?;
647647

648-
let (author_name, email) = discover_author(path)?;
649-
let author = match (cfg.name, cfg.email, author_name, email) {
650-
(Some(name), Some(email), _, _)
651-
| (Some(name), None, _, Some(email))
652-
| (None, Some(email), Some(name), _)
653-
| (None, None, Some(name), Some(email)) => {
648+
let (discovered_name, discovered_email) = discover_author(path)?;
649+
650+
// "Name <email>" or "Name" or "<email>" or None if neither name nor email is obtained
651+
// cfg takes priority over the discovered ones
652+
let author_name = match (cfg.name, discovered_name) {
653+
(Some(name), _) | (_, Some(name)) => Some(name),
654+
(None, None) => None,
655+
};
656+
657+
let author_email = match (cfg.email, discovered_email) {
658+
(Some(email), _) | (_, Some(email)) => Some(email),
659+
(None, None) => None,
660+
};
661+
662+
let author = match (author_name, author_email) {
663+
(Some(name), Some(email)) => {
654664
if email.is_empty() {
655-
name
665+
Some(name)
656666
} else {
657-
format!("{} <{}>", name, email)
667+
Some(format!("{} <{}>", name, email))
658668
}
659669
}
660-
(Some(name), None, _, None) | (None, None, Some(name), None) => name,
661-
(None, Some(email), None, _) | (None, None, None, Some(email)) => {
670+
(Some(name), None) => Some(name),
671+
(None, Some(email)) => {
662672
if email.is_empty() {
663-
"".to_string()
673+
None
664674
} else {
665-
format!("<{}>", email)
675+
Some(format!("<{}>", email))
666676
}
667677
}
668-
(None, None, None, None) => "".to_string(),
678+
(None, None) => None,
669679
};
670680

671681
let mut cargotoml_path_specifier = String::new();
@@ -714,10 +724,9 @@ edition = {}
714724
[dependencies]
715725
{}"#,
716726
name,
717-
if author.is_empty() {
718-
format!("")
719-
} else {
720-
format!("{}", toml::Value::String(author))
727+
match author {
728+
Some(value) => format!("{}", toml::Value::String(value)),
729+
None => format!(""),
721730
},
722731
match opts.edition {
723732
Some(edition) => toml::Value::String(edition.to_string()),

0 commit comments

Comments
 (0)