Skip to content

Commit 1dacd11

Browse files
committed
refactor(gctx): Prefer let-else
1 parent c169b66 commit 1dacd11

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/cargo/util/context/mod.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -657,38 +657,42 @@ impl GlobalContext {
657657
///
658658
/// Callers should prefer [`Workspace::build_dir`] instead.
659659
pub fn build_dir(&self, workspace_manifest_path: &PathBuf) -> CargoResult<Option<Filesystem>> {
660-
if let Some(val) = &self.build_config()?.build_dir {
661-
let replacements = vec![
662-
(
663-
"{workspace-root}",
664-
workspace_manifest_path
665-
.parent()
666-
.unwrap()
667-
.to_str()
668-
.context("workspace root was not valid utf-8")?
669-
.to_string(),
670-
),
671-
(
672-
"{cargo-cache-home}",
673-
self.home()
674-
.as_path_unlocked()
675-
.to_str()
676-
.context("cargo home was not valid utf-8")?
677-
.to_string(),
678-
),
679-
("{workspace-path-hash}", {
680-
let real_path = std::fs::canonicalize(workspace_manifest_path)?;
681-
let hash = crate::util::hex::short_hash(&real_path);
682-
format!("{}{}{}", &hash[0..2], std::path::MAIN_SEPARATOR, &hash[2..])
683-
}),
684-
];
685-
686-
let template_variables = replacements
687-
.iter()
688-
.map(|(key, _)| key[1..key.len() - 1].to_string())
689-
.collect_vec();
660+
let Some(val) = &self.build_config()?.build_dir else {
661+
// For now, fallback to the previous implementation.
662+
// This will change in the future.
663+
return self.target_dir();
664+
};
665+
let replacements = vec![
666+
(
667+
"{workspace-root}",
668+
workspace_manifest_path
669+
.parent()
670+
.unwrap()
671+
.to_str()
672+
.context("workspace root was not valid utf-8")?
673+
.to_string(),
674+
),
675+
(
676+
"{cargo-cache-home}",
677+
self.home()
678+
.as_path_unlocked()
679+
.to_str()
680+
.context("cargo home was not valid utf-8")?
681+
.to_string(),
682+
),
683+
("{workspace-path-hash}", {
684+
let real_path = std::fs::canonicalize(workspace_manifest_path)?;
685+
let hash = crate::util::hex::short_hash(&real_path);
686+
format!("{}{}{}", &hash[0..2], std::path::MAIN_SEPARATOR, &hash[2..])
687+
}),
688+
];
690689

691-
let path = val
690+
let template_variables = replacements
691+
.iter()
692+
.map(|(key, _)| key[1..key.len() - 1].to_string())
693+
.collect_vec();
694+
695+
let path = val
692696
.resolve_templated_path(self, replacements)
693697
.map_err(|e| match e {
694698
path::ResolveTemplateError::UnexpectedVariable {
@@ -716,20 +720,15 @@ impl GlobalContext {
716720
}
717721
})?;
718722

719-
// Check if the target directory is set to an empty string in the config.toml file.
720-
if val.raw_value().is_empty() {
721-
bail!(
722-
"the build directory is set to an empty string in {}",
723-
val.value().definition
724-
)
725-
}
726-
727-
Ok(Some(Filesystem::new(path)))
728-
} else {
729-
// For now, fallback to the previous implementation.
730-
// This will change in the future.
731-
return self.target_dir();
723+
// Check if the target directory is set to an empty string in the config.toml file.
724+
if val.raw_value().is_empty() {
725+
bail!(
726+
"the build directory is set to an empty string in {}",
727+
val.value().definition
728+
)
732729
}
730+
731+
Ok(Some(Filesystem::new(path)))
733732
}
734733

735734
/// Get a configuration value by key.

0 commit comments

Comments
 (0)