refactor: Enable clippy nursery lint

This commit is contained in:
Gerald Pinder 2024-02-10 15:32:20 -05:00
parent 421ca616e7
commit 6b4c86f01f
4 changed files with 39 additions and 31 deletions

View file

@ -126,14 +126,18 @@ impl BugReportCommand {
}
fn get_recipe(&self) -> Option<Recipe> {
let recipe_path = if let Some(recipe_path) = self.recipe_path.clone() {
recipe_path
} else if let Ok(recipe) = get_config_file("recipe", "Enter path to recipe file") {
recipe
} else {
trace!("Failed to get recipe");
String::new()
};
let recipe_path = self.recipe_path.clone().map_or_else(
|| {
get_config_file("recipe", "Enter path to recipe file").map_or_else(
|_| {
trace!("Failed to get recipe");
String::new()
},
|recipe| recipe,
)
},
|recipe_path| recipe_path,
);
Recipe::parse(&recipe_path).ok()
}
@ -263,7 +267,7 @@ fn get_shell_info() -> ShellInfo {
ShellInfo {
version,
name: current_shell.to_string(),
name: current_shell,
}
}

View file

@ -206,7 +206,7 @@ impl BuildCommand {
} else {
tags.first()
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_string())
.unwrap_or_else(|| image_name.to_string())
};
debug!("Full tag is {first_image_name}");
@ -243,7 +243,7 @@ impl BuildCommand {
debug!("Pushing is enabled");
let credentials =
credentials.ok_or(anyhow!("Should have checked for creds earlier"))?;
credentials.ok_or_else(|| anyhow!("Should have checked for creds earlier"))?;
push_images_podman_api(&tags, &image_name, &first_image_name, &client, &credentials)
.await?;
@ -301,7 +301,7 @@ impl BuildCommand {
let credentials = self
.get_login_creds()
.ok_or(anyhow!("Unable to get credentials"))?;
.ok_or_else(|| anyhow!("Unable to get credentials"))?;
let (registry, username, password) = (
credentials.registry,
@ -437,7 +437,7 @@ impl BuildCommand {
} else {
tags.first()
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_string())
.unwrap_or_else(|| image_name.to_string())
};
info!("Building image {full_image}");
@ -540,7 +540,7 @@ fn sign_images(image_name: &str, tag: Option<&str>) -> Result<()> {
let image_digest = get_image_digest(image_name, tag)?;
let image_name_tag = tag
.map(|t| format!("{image_name}:{t}"))
.unwrap_or(image_name.to_owned());
.unwrap_or_else(|| image_name.to_owned());
match (
env::var("CI_DEFAULT_BRANCH"),
@ -648,11 +648,10 @@ fn sign_images(image_name: &str, tag: Option<&str>) -> Result<()> {
fn get_image_digest(image_name: &str, tag: Option<&str>) -> Result<String> {
trace!("get_image_digest({image_name}, {tag:?})");
let image_url = if let Some(tag) = tag {
format!("docker://{image_name}:{tag}")
} else {
format!("docker://{image_name}")
};
let image_url = tag.map_or_else(
|| format!("docker://{image_name}"),
|tag| format!("docker://{image_name}:{tag}"),
);
trace!("skopeo inspect --format='{{.Digest}}' {image_url}");
let image_digest = String::from_utf8(

View file

@ -1,5 +1,11 @@
//! The root library for blue-build.
#![warn(clippy::correctness, clippy::suspicious, clippy::perf, clippy::style)]
#![warn(
clippy::correctness,
clippy::suspicious,
clippy::perf,
clippy::style,
clippy::nursery
)]
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![allow(clippy::module_name_repetitions)]

View file

@ -214,7 +214,7 @@ impl ModuleExt {
/// # Errors
/// Can return an `anyhow` Error if the file cannot be read or deserialized
/// into a [`ModuleExt`]
pub fn parse_module_from_file(file_name: &str) -> Result<ModuleExt> {
pub fn parse_module_from_file(file_name: &str) -> Result<Self> {
let file_path = PathBuf::from("config").join(file_name);
let file_path = if file_path.is_absolute() {
file_path
@ -222,13 +222,13 @@ impl ModuleExt {
std::env::current_dir()?.join(file_path)
};
let file = fs::read_to_string(file_path.clone())?;
let file = fs::read_to_string(file_path)?;
serde_yaml::from_str::<ModuleExt>(&file).map_or_else(
|_| -> Result<ModuleExt> {
serde_yaml::from_str::<Self>(&file).map_or_else(
|_| -> Result<Self> {
let module =
serde_yaml::from_str::<Module>(&file).map_err(ops::serde_yaml_err(&file))?;
Ok(ModuleExt::builder().modules(vec![module]).build())
Ok(Self::builder().modules(vec![module]).build())
},
Ok,
)
@ -256,17 +256,16 @@ impl Module {
modules
.iter()
.flat_map(|module| {
if let Some(file_name) = &module.from_file {
match ModuleExt::parse_module_from_file(file_name) {
module.from_file.as_ref().map_or_else(
|| vec![module.clone()],
|file_name| match ModuleExt::parse_module_from_file(file_name) {
Err(e) => {
error!("Failed to get module from {file_name}: {e}");
vec![]
}
Ok(module_ext) => Self::get_modules(&module_ext.modules),
}
} else {
vec![module.clone()]
}
},
)
})
.collect()
}