Skip to content

Commit

Permalink
🎨 Run cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Oct 18, 2024
1 parent 3a09dd0 commit 7b12d87
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn cmd() -> Command {
pub fn run(matches: &ArgMatches) -> ExitCode {
let debug_mode: Option<bool> = matches
.get_flag("build-debug")
.then(|| true)
.or(matches.get_flag("build-release").then(|| false));
.then_some(true)
.or(matches.get_flag("build-release").then_some(false));
let now = Instant::now();
let mut project = match Project::current() {
Ok(p) => p,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/completions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Arg, ArgAction, ArgMatches, Command};
use clap::{Arg, ArgMatches, Command};
use clap_complete::Shell;
use std::{io, process::ExitCode};

Expand Down
2 changes: 1 addition & 1 deletion src/cli/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn run(matches: &ArgMatches) -> ExitCode {
let id: &String = matches.get_one("id").unwrap();
let code: u8 = id
.parse()
.expect(format!("Code is invalid (must be 0-255)",).as_str());
.unwrap_or_else(|_| { panic!("{}", "Code is invalid (must be 0-255)".to_string()) });
let notif = match diagnostic::Notification::from_code(code) {
Some(n) => n,
None => {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::fs;
use std::str::FromStr;
use std::{env, path::PathBuf, process::ExitCode};

const DEV_BP: &'static str = "development_behavior_packs";
const DEV_RP: &'static str = "development_resource_packs";
const DEV_SP: &'static str = "development_skin_packs";
const DEV_BP: &str = "development_behavior_packs";
const DEV_RP: &str = "development_resource_packs";
const DEV_SP: &str = "development_skin_packs";

mod location {
use crate::diagnostic;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/uuid/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ fn update(uuids: &mut Uuids, packs: Vec<Pack>, kinds: Vec<String>, uuid: Option<
.into_iter()
.filter(|i| packs.contains(&i.1))
{
if kinds.contains(&&"header".to_string()) {
if kinds.contains(&"header".to_string()) {
data.0.update_header(uuid.copied());
log::info!("Refreshed header UUID for {}", data.1);
}
if kinds.contains(&&"module".to_string()) {
if kinds.contains(&"module".to_string()) {
data.0.update_module(uuid.copied());
log::info!("Refreshed module UUID for {}", data.1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
// ignored by gitignore. So we handle this case by including such files into the watched paths list.
let any_external_paths = paths
.iter()
.filter(|p| !p.starts_with(&paths::root()))
.filter(|p| !p.starts_with(paths::root()))
.cloned();
let mut paths = remove_ignored_files(&paths::root(), &paths[..]);
paths.extend(any_external_paths);
Expand Down
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn evaluate(source: &str) -> Result<bool, Box<rhai::EvalAltResult>> {
.register_fn("dll_prefix", || DLL_PREFIX)
.register_fn("dll_suffix", || DLL_SUFFIX)
.register_fn("env", |key: rhai::ImmutableString| {
env::var(key.as_str()).unwrap_or(String::new())
env::var(key.as_str()).unwrap_or_default()
})
.register_fn("env_present", |key: rhai::ImmutableString| {
env::var(key.as_str()).is_err_and(|e| e == env::VarError::NotPresent)
Expand Down
4 changes: 2 additions & 2 deletions src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Health {
let uuids = self.root.join(paths::uuids());
let valid_file_format: bool = (|| {
toml::from_str::<toml::Value>(match fs::read_to_string(&uuids) {
Ok(ref data) => &data,
Ok(ref data) => data,
Err(e) => {
log::error!("Failed to read UUID file: {}", e);
return false;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Health {
match data {
Err(e) => {
log::error!("Invalid TOML: {}", e);
return false;
false
}
Ok(mut data) => {
let mut modified = false;
Expand Down
35 changes: 12 additions & 23 deletions src/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::Write;
use std::path::PathBuf;

/// File extension for Minecraft language files.
pub const LANGUAGE_FILE_EXTENSION: &'static str = "lang";
pub const LANGUAGE_FILE_EXTENSION: &str = "lang";

/// A value mapped to languages.
pub type Localized<T> = HashMap<Language, T>;
Expand Down Expand Up @@ -71,12 +71,7 @@ impl Default for LanguageGroups {
impl LanguageGroups {
/// Returns the language group which contains `language`.
pub fn group_of(&self, language: &Language) -> Option<&LanguageGroup> {
for group in &self.0 {
if group.contains(language) {
return Some(group);
}
}
None
self.0.iter().find(|&group| group.contains(language))
}

/// Adds a language to it's own group if it is not present in any group yet.
Expand Down Expand Up @@ -158,7 +153,7 @@ impl LanguageGroups {

// Step 2
for lang in given {
match self.group_of(&lang) {
match self.group_of(lang) {
Some(group) if self.group_of(target).is_some_and(|g| g == group) => {
return Some(lang);
}
Expand All @@ -173,7 +168,7 @@ impl LanguageGroups {

// Step 4
for lang in given {
match self.group_of(&lang) {
match self.group_of(lang) {
Some(group) if self.group_of(fallback).is_some_and(|g| g == group) => {
return Some(lang);
}
Expand All @@ -200,7 +195,7 @@ pub fn generate_language_json(dir: &PathBuf) -> Result<(), Box<dyn std::error::E
.extension()
.is_some_and(|ext| ext == LANGUAGE_FILE_EXTENSION)
})
.map(|f| match f.path().file_stem() {
.filter_map(|f| match f.path().file_stem() {
Some(stem) => match stem.to_str() {
Some(stem) => {
if Language::from_file_id(stem).is_some() {
Expand All @@ -222,7 +217,6 @@ pub fn generate_language_json(dir: &PathBuf) -> Result<(), Box<dyn std::error::E
None
}
})
.filter_map(|x| x)
.collect();
let json: serde_json::Value = serde_json::from_value(langs.into())?;
let path = dir.join("languages.json");
Expand All @@ -248,23 +242,18 @@ pub fn update_language_files(
for (key, target) in &data {
let mut covered: Vec<&Language> = Vec::new();
for (lang, translation) in target {
append_language_file(dir, &lang, &key, &translation)?;
covered.push(&lang);
append_language_file(dir, lang, key, translation)?;
covered.push(lang);
}

// cover all remaining languages with their fallback if present
let remaining_languages = groups.0.iter().flatten().filter(|l| !covered.contains(l));
for remaining in remaining_languages {
if let Some(lang_with_translation) = groups.best_language(remaining, &covered, fallback)
{
let translation = target.get(lang_with_translation).expect(
format!(
"{} should provide a translation for {}",
lang_with_translation, key
)
.as_str(),
);
append_language_file(dir, remaining, &key, &translation)?;
let translation = target.get(lang_with_translation).unwrap_or_else(|| panic!("{} should provide a translation for {}",
lang_with_translation, key));
append_language_file(dir, remaining, key, translation)?;
} else {
// TODO: this is probably unreachable as `name` and `description` are always at least set
// to some language
Expand Down Expand Up @@ -568,8 +557,8 @@ impl Language {
L::Other(id, _name) => {
let pair = id
.split_once('-')
.expect(format!("language has invalid id: {}", id).as_str());
vec![pair.0, "_", pair.1.to_uppercase().as_str()].join("")
.unwrap_or_else(|| panic!("language has invalid id: {}", id));
[pair.0, "_", pair.1.to_uppercase().as_str()].join("")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env::current_dir;
use std::path::PathBuf;

/// The name of the file which contains the ID of the Allay project.
pub const FINGERPRINT: &'static str = ".allay-fingerprint";
pub const FINGERPRINT: &str = ".allay-fingerprint";

/// Returns the path of the project root or [`None`] if an `allay.toml` file cannot be found.
pub fn try_root() -> Option<PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Project {
)?;

log::debug!("Adding fingerprint");
if let Err(e) = fs::write(&dest.join(paths::FINGERPRINT), self.id) {
if let Err(e) = fs::write(dest.join(paths::FINGERPRINT), self.id) {
log::error!("Failed to add fingerprint: {}", e);
};

Expand Down
6 changes: 3 additions & 3 deletions src/scaffolding/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Project skeleton for an Allay project.
/// The gitignore template.
pub const GITIGNORE: &'static [u8] = include_bytes!("gitignore");
pub const GITIGNORE: &[u8] = include_bytes!("gitignore");

/// The configuration file (allay.toml) template.
// TODO: use template engine to fill in info like git username in authors field
pub const CONFIG: &'static [u8] = include_bytes!("allay.toml");
pub const CONFIG: &[u8] = include_bytes!("allay.toml");

/// The default pack icon.
pub const PACK_ICON: &'static [u8] = include_bytes!("pack_icon.png");
pub const PACK_ICON: &[u8] = include_bytes!("pack_icon.png");
12 changes: 6 additions & 6 deletions src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ pub struct Uuids {
pub wt: Data,
}

impl Into<Table> for Uuids {
fn into(self) -> Table {
impl From<Uuids> for Table {
fn from(val: Uuids) -> Self {
let mut table = Table::new();
table.add_row(row![b => "", "Header", "Module", "Dependencies"]);
for (name, pack) in [
("BP", self.bp),
("RP", self.rp),
("SP", self.sp),
("WT", self.wt),
("BP", val.bp),
("RP", val.rp),
("SP", val.sp),
("WT", val.wt),
] {
table.add_row(row![
name,
Expand Down

0 comments on commit 7b12d87

Please sign in to comment.