From efec43b3b8827820cf35fe1b73d8e33f5fe84eb4 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 20 Jun 2026 01:16:50 +0900 Subject: refactor: auto-fix clippy warnings --- .../package/archiver/archivable_files_filter.rs | 4 +- .../src/package/archiver/archive_manager.rs | 26 +- .../shirabe/src/package/archiver/phar_archiver.rs | 6 + .../shirabe/src/package/archiver/zip_archiver.rs | 6 + crates/shirabe/src/package/base_package.rs | 2 +- crates/shirabe/src/package/comparer/comparer.rs | 8 +- crates/shirabe/src/package/comparer/mod.rs | 1 + crates/shirabe/src/package/dumper/array_dumper.rs | 58 +- crates/shirabe/src/package/link.rs | 5 +- crates/shirabe/src/package/loader/array_loader.rs | 392 ++++++------- .../src/package/loader/root_package_loader.rs | 38 +- .../src/package/loader/validating_array_loader.rs | 652 ++++++++++----------- crates/shirabe/src/package/locker.rs | 73 +-- crates/shirabe/src/package/mod.rs | 1 + crates/shirabe/src/package/package.rs | 14 +- .../src/package/version/stability_filter.rs | 8 +- .../shirabe/src/package/version/version_guesser.rs | 20 +- .../shirabe/src/package/version/version_parser.rs | 8 +- .../src/package/version/version_selector.rs | 42 +- 19 files changed, 682 insertions(+), 682 deletions(-) (limited to 'crates/shirabe/src/package') diff --git a/crates/shirabe/src/package/archiver/archivable_files_filter.rs b/crates/shirabe/src/package/archiver/archivable_files_filter.rs index f820e08..ee39ba3 100644 --- a/crates/shirabe/src/package/archiver/archivable_files_filter.rs +++ b/crates/shirabe/src/package/archiver/archivable_files_filter.rs @@ -1,7 +1,7 @@ //! ref: composer/src/Composer/Package/Archiver/ArchivableFilesFilter.php use shirabe_php_shim::PharData; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; pub struct ArchivableFilesFilter { inner: Box>, @@ -16,7 +16,7 @@ impl ArchivableFilesFilter { } } - fn accept(&mut self, file: &PathBuf) -> bool { + fn accept(&mut self, file: &Path) -> bool { if file.is_dir() { self.dirs.push(file.to_string_lossy().into_owned()); return false; diff --git a/crates/shirabe/src/package/archiver/archive_manager.rs b/crates/shirabe/src/package/archiver/archive_manager.rs index cb15b76..2c9eb35 100644 --- a/crates/shirabe/src/package/archiver/archive_manager.rs +++ b/crates/shirabe/src/package/archiver/archive_manager.rs @@ -198,20 +198,20 @@ impl ArchiveManager { let mut json_file = JsonFile::new(composer_json_path, None, None)?; let json_data = json_file.read()?; if let Some(archive) = json_data.get("archive") { - if let Some(name) = archive.get("name").and_then(|v| v.as_string()) { - if !name.is_empty() { - package.set_archive_name(name.to_string()); - } + if let Some(name) = archive.get("name").and_then(|v| v.as_string()) + && !name.is_empty() + { + package.set_archive_name(name.to_string()); } - if let Some(exclude) = archive.get("exclude") { - if let Some(excludes) = exclude.as_array() { - let excludes: Vec = excludes - .values() - .filter_map(|v| v.as_string().map(|s| s.to_string())) - .collect(); - if !excludes.is_empty() { - package.set_archive_excludes(excludes); - } + if let Some(exclude) = archive.get("exclude") + && let Some(excludes) = exclude.as_array() + { + let excludes: Vec = excludes + .values() + .filter_map(|v| v.as_string().map(|s| s.to_string())) + .collect(); + if !excludes.is_empty() { + package.set_archive_excludes(excludes); } } } diff --git a/crates/shirabe/src/package/archiver/phar_archiver.rs b/crates/shirabe/src/package/archiver/phar_archiver.rs index d2854eb..c25b90b 100644 --- a/crates/shirabe/src/package/archiver/phar_archiver.rs +++ b/crates/shirabe/src/package/archiver/phar_archiver.rs @@ -29,6 +29,12 @@ fn compress_formats() -> IndexMap<&'static str, i64> { #[derive(Debug)] pub struct PharArchiver; +impl Default for PharArchiver { + fn default() -> Self { + Self::new() + } +} + impl PharArchiver { pub fn new() -> Self { Self diff --git a/crates/shirabe/src/package/archiver/zip_archiver.rs b/crates/shirabe/src/package/archiver/zip_archiver.rs index 7d9f126..41201b5 100644 --- a/crates/shirabe/src/package/archiver/zip_archiver.rs +++ b/crates/shirabe/src/package/archiver/zip_archiver.rs @@ -13,6 +13,12 @@ use std::path::PathBuf; #[derive(Debug)] pub struct ZipArchiver; +impl Default for ZipArchiver { + fn default() -> Self { + Self::new() + } +} + impl ZipArchiver { pub fn new() -> Self { Self diff --git a/crates/shirabe/src/package/base_package.rs b/crates/shirabe/src/package/base_package.rs index 54abc13..d7b6978 100644 --- a/crates/shirabe/src/package/base_package.rs +++ b/crates/shirabe/src/package/base_package.rs @@ -90,7 +90,7 @@ pub trait BasePackage: PackageInterface + std::fmt::Display { fn is_platform(&self) -> bool { self.repository_opt() - .map_or(false, |r| r.is::()) + .is_some_and(|r| r.is::()) } fn get_full_pretty_version(&self, truncate: bool, display_mode: DisplayMode) -> String { diff --git a/crates/shirabe/src/package/comparer/comparer.rs b/crates/shirabe/src/package/comparer/comparer.rs index 73c94f4..d23e669 100644 --- a/crates/shirabe/src/package/comparer/comparer.rs +++ b/crates/shirabe/src/package/comparer/comparer.rs @@ -12,6 +12,12 @@ pub struct Comparer { changed: IndexMap>, } +impl Default for Comparer { + fn default() -> Self { + Self::new() + } +} + impl Comparer { pub fn new() -> Self { Self { @@ -96,7 +102,7 @@ impl Comparer { } for (dir, value) in &destination { for (file, _hash) in value { - if !source.get(dir).map_or(false, |d| d.contains_key(file)) { + if !source.get(dir).is_some_and(|d| d.contains_key(file)) { self.changed .entry("added".to_string()) .or_default() diff --git a/crates/shirabe/src/package/comparer/mod.rs b/crates/shirabe/src/package/comparer/mod.rs index d6ce6cb..878d7e0 100644 --- a/crates/shirabe/src/package/comparer/mod.rs +++ b/crates/shirabe/src/package/comparer/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception, reason = "to port PHP's structure as it is")] pub mod comparer; pub use comparer::*; diff --git a/crates/shirabe/src/package/dumper/array_dumper.rs b/crates/shirabe/src/package/dumper/array_dumper.rs index 27fc415..7bf62f2 100644 --- a/crates/shirabe/src/package/dumper/array_dumper.rs +++ b/crates/shirabe/src/package/dumper/array_dumper.rs @@ -21,6 +21,12 @@ fn mirror_to_php(mirror: Mirror) -> PhpMixed { #[derive(Debug)] pub struct ArrayDumper; +impl Default for ArrayDumper { + fn default() -> Self { + Self::new() + } +} + impl ArrayDumper { pub fn new() -> Self { Self @@ -66,19 +72,19 @@ impl ArrayDumper { Box::new(PhpMixed::String(reference.to_string())), ); } - if let Some(mirrors) = package.get_source_mirrors() { - if !mirrors.is_empty() { - source.insert( - "mirrors".to_string(), - Box::new(PhpMixed::Array( - mirrors - .into_iter() - .enumerate() - .map(|(i, m)| (i.to_string(), Box::new(mirror_to_php(m)))) - .collect(), - )), - ); - } + if let Some(mirrors) = package.get_source_mirrors() + && !mirrors.is_empty() + { + source.insert( + "mirrors".to_string(), + Box::new(PhpMixed::Array( + mirrors + .into_iter() + .enumerate() + .map(|(i, m)| (i.to_string(), Box::new(mirror_to_php(m)))) + .collect(), + )), + ); } data.insert("source".to_string(), PhpMixed::Array(source)); } @@ -105,19 +111,19 @@ impl ArrayDumper { Box::new(PhpMixed::String(shasum.to_string())), ); } - if let Some(mirrors) = package.get_dist_mirrors() { - if !mirrors.is_empty() { - dist.insert( - "mirrors".to_string(), - Box::new(PhpMixed::Array( - mirrors - .into_iter() - .enumerate() - .map(|(i, m)| (i.to_string(), Box::new(mirror_to_php(m)))) - .collect(), - )), - ); - } + if let Some(mirrors) = package.get_dist_mirrors() + && !mirrors.is_empty() + { + dist.insert( + "mirrors".to_string(), + Box::new(PhpMixed::Array( + mirrors + .into_iter() + .enumerate() + .map(|(i, m)| (i.to_string(), Box::new(mirror_to_php(m)))) + .collect(), + )), + ); } data.insert("dist".to_string(), PhpMixed::Array(dist)); } diff --git a/crates/shirabe/src/package/link.rs b/crates/shirabe/src/package/link.rs index 0157989..027fa00 100644 --- a/crates/shirabe/src/package/link.rs +++ b/crates/shirabe/src/package/link.rs @@ -92,10 +92,7 @@ impl std::fmt::Display for Link { write!( f, "{} {} {} ({})", - self.source, - self.description, - self.target, - self.constraint.to_string(), + self.source, self.description, self.target, self.constraint, ) } } diff --git a/crates/shirabe/src/package/loader/array_loader.rs b/crates/shirabe/src/package/loader/array_loader.rs index 222b5df..889624d 100644 --- a/crates/shirabe/src/package/loader/array_loader.rs +++ b/crates/shirabe/src/package/loader/array_loader.rs @@ -35,10 +35,7 @@ pub struct ArrayLoader { impl ArrayLoader { pub fn new(parser: Option, load_options: bool) -> Self { - let parser = match parser { - Some(p) => p, - None => VersionParser::new(), - }; + let parser = parser.unwrap_or_default(); Self { version_parser: parser, load_options, @@ -340,11 +337,11 @@ impl ArrayLoader { .set_target_dir(target_dir.as_string().map(|s| s.to_string())); } - if let Some(extra) = config.get("extra") { - if matches!(extra, PhpMixed::Array(_)) { - let extra_map = php_to_map(extra); - package.package_mut().set_extra(extra_map); - } + if let Some(extra) = config.get("extra") + && matches!(extra, PhpMixed::Array(_)) + { + let extra_map = php_to_map(extra); + package.package_mut().set_extra(extra_map); } if let Some(bin) = config.get("bin").cloned() { @@ -355,13 +352,13 @@ impl ArrayLoader { if let PhpMixed::List(ref mut list) = bin_list { for item in list.iter_mut() { if let Some(s) = item.as_string() { - *item = Box::new(PhpMixed::String(ltrim(s, Some("/")))); + **item = PhpMixed::String(ltrim(s, Some("/"))); } } } else if let PhpMixed::Array(ref mut map) = bin_list { for (_k, v) in map.iter_mut() { if let Some(s) = v.as_string() { - *v = Box::new(PhpMixed::String(ltrim(s, Some("/")))); + **v = PhpMixed::String(ltrim(s, Some("/"))); } } } @@ -376,10 +373,10 @@ impl ArrayLoader { .set_installation_source(installation_source.as_string().map(|s| s.to_string())); } - if let Some(default_branch) = config.get("default-branch") { - if default_branch.as_bool() == Some(true) { - package.package_mut().set_is_default_branch(true); - } + if let Some(default_branch) = config.get("default-branch") + && default_branch.as_bool() == Some(true) + { + package.package_mut().set_is_default_branch(true); } if let Some(source) = config.get("source").cloned() { @@ -473,25 +470,23 @@ impl ArrayLoader { } } - if let Some(suggest) = config.get("suggest").cloned() { - if let PhpMixed::Array(mut suggest_map) = suggest { - for (target, reason) in suggest_map.iter_mut() { - if let Some(r) = reason.as_string() { - if trim(r, None) == "self.version" { - *reason = Box::new(PhpMixed::String( - package.get_pretty_version().to_string(), - )); - let _ = target; - } - } + if let Some(suggest) = config.get("suggest").cloned() + && let PhpMixed::Array(mut suggest_map) = suggest + { + for (target, reason) in suggest_map.iter_mut() { + if let Some(r) = reason.as_string() + && trim(r, None) == "self.version" + { + **reason = PhpMixed::String(package.get_pretty_version().to_string()); + let _ = target; } - let suggests: IndexMap = suggest_map - .iter() - .map(|(k, v)| (k.clone(), strval(v))) - .collect(); - config.insert("suggest".to_string(), PhpMixed::Array(suggest_map)); - package.package_mut().set_suggests(suggests); } + let suggests: IndexMap = suggest_map + .iter() + .map(|(k, v)| (k.clone(), strval(v))) + .collect(); + config.insert("suggest".to_string(), PhpMixed::Array(suggest_map)); + package.package_mut().set_suggests(suggests); } if let Some(autoload) = config.get("autoload") { @@ -514,201 +509,198 @@ impl ArrayLoader { package.package_mut().set_php_ext(Some(php_ext_map)); } - if let Some(time_value) = config.get("time") { - if !shirabe_php_shim::empty(time_value) { - let time_str = time_value.as_string().unwrap_or(""); - let time = if Preg::is_match(r"/^\d++$/D", time_str) { - format!("@{}", time_str) - } else { - time_str.to_string() - }; + if let Some(time_value) = config.get("time") + && !shirabe_php_shim::empty(time_value) + { + let time_str = time_value.as_string().unwrap_or(""); + let time = if Preg::is_match(r"/^\d++$/D", time_str) { + format!("@{}", time_str) + } else { + time_str.to_string() + }; - if let Ok(date) = shirabe_php_shim::date_create::(&time) { - package.package_mut().set_release_date(Some(date)); - } + if let Ok(date) = shirabe_php_shim::date_create::(&time) { + package.package_mut().set_release_date(Some(date)); } } - if let Some(notification_url) = config.get("notification-url") { - if !shirabe_php_shim::empty(notification_url) { - package - .package_mut() - .set_notification_url(strval(notification_url)); - } + if let Some(notification_url) = config.get("notification-url") + && !shirabe_php_shim::empty(notification_url) + { + package + .package_mut() + .set_notification_url(strval(notification_url)); } - if let Some(archive) = config.get("archive").cloned() { - if let PhpMixed::Array(archive_map) = archive { - if let Some(name) = archive_map.get("name") { - if !shirabe_php_shim::empty(name) { - package.complete_mut().set_archive_name(strval(name)); - } - } - if let Some(exclude) = archive_map.get("exclude") { - if !shirabe_php_shim::empty(exclude) { - package - .complete_mut() - .set_archive_excludes(php_to_string_vec(exclude)); - } - } + if let Some(archive) = config.get("archive").cloned() + && let PhpMixed::Array(archive_map) = archive + { + if let Some(name) = archive_map.get("name") + && !shirabe_php_shim::empty(name) + { + package.complete_mut().set_archive_name(strval(name)); + } + if let Some(exclude) = archive_map.get("exclude") + && !shirabe_php_shim::empty(exclude) + { + package + .complete_mut() + .set_archive_excludes(php_to_string_vec(exclude)); } } - if let Some(scripts) = config.get("scripts").cloned() { - if let PhpMixed::Array(mut scripts_map) = scripts { - for (event, listeners) in scripts_map.iter_mut() { - let listeners_array = match listeners.as_ref() { - PhpMixed::Array(_) | PhpMixed::List(_) => listeners.clone(), - other => Box::new(PhpMixed::List(vec![Box::new(other.clone())])), - }; - *listeners = listeners_array; - let _ = event; - } - for reserved in ["composer", "php", "putenv"].iter() { - if scripts_map.contains_key(*reserved) { - trigger_error( - &format!( - "The `{}` script name is reserved for internal use, please avoid defining it", - reserved - ), - E_USER_DEPRECATED, - ); - } + if let Some(scripts) = config.get("scripts").cloned() + && let PhpMixed::Array(mut scripts_map) = scripts + { + for (event, listeners) in scripts_map.iter_mut() { + let listeners_array = match listeners.as_ref() { + PhpMixed::Array(_) | PhpMixed::List(_) => listeners.clone(), + other => Box::new(PhpMixed::List(vec![Box::new(other.clone())])), + }; + *listeners = listeners_array; + let _ = event; + } + for reserved in ["composer", "php", "putenv"].iter() { + if scripts_map.contains_key(*reserved) { + trigger_error( + &format!( + "The `{}` script name is reserved for internal use, please avoid defining it", + reserved + ), + E_USER_DEPRECATED, + ); } - let scripts: IndexMap> = scripts_map - .iter() - .map(|(k, v)| (k.clone(), php_to_string_vec(v))) - .collect(); - config.insert("scripts".to_string(), PhpMixed::Array(scripts_map)); - package.complete_mut().set_scripts(scripts); } + let scripts: IndexMap> = scripts_map + .iter() + .map(|(k, v)| (k.clone(), php_to_string_vec(v))) + .collect(); + config.insert("scripts".to_string(), PhpMixed::Array(scripts_map)); + package.complete_mut().set_scripts(scripts); } - if let Some(description) = config.get("description") { - if !shirabe_php_shim::empty(description) && is_string(description) { - package - .complete_mut() - .set_description(description.as_string().unwrap_or("").to_string()); - } + if let Some(description) = config.get("description") + && !shirabe_php_shim::empty(description) + && is_string(description) + { + package + .complete_mut() + .set_description(description.as_string().unwrap_or("").to_string()); } - if let Some(homepage) = config.get("homepage") { - if !shirabe_php_shim::empty(homepage) && is_string(homepage) { - package - .complete_mut() - .set_homepage(homepage.as_string().unwrap_or("").to_string()); - } + if let Some(homepage) = config.get("homepage") + && !shirabe_php_shim::empty(homepage) + && is_string(homepage) + { + package + .complete_mut() + .set_homepage(homepage.as_string().unwrap_or("").to_string()); } - if let Some(keywords) = config.get("keywords") { - if !shirabe_php_shim::empty(keywords) { - if matches!(keywords, PhpMixed::Array(_) | PhpMixed::List(_)) { - let keywords_vec: Vec = match keywords { - PhpMixed::List(list) => list.iter().map(|v| strval(v)).collect(), - PhpMixed::Array(map) => map.values().map(|v| strval(v)).collect(), - _ => vec![], - }; - package.complete_mut().set_keywords(keywords_vec); - } - } + if let Some(keywords) = config.get("keywords") + && !shirabe_php_shim::empty(keywords) + && matches!(keywords, PhpMixed::Array(_) | PhpMixed::List(_)) + { + let keywords_vec: Vec = match keywords { + PhpMixed::List(list) => list.iter().map(|v| strval(v)).collect(), + PhpMixed::Array(map) => map.values().map(|v| strval(v)).collect(), + _ => vec![], + }; + package.complete_mut().set_keywords(keywords_vec); } - if let Some(license) = config.get("license") { - if !shirabe_php_shim::empty(license) { - let license_vec: Vec = match license { - PhpMixed::Array(map) => map - .values() - .map(|v| v.as_string().unwrap_or("").to_string()) - .collect(), - PhpMixed::List(list) => list - .iter() - .map(|v| v.as_string().unwrap_or("").to_string()) - .collect(), - other => vec![other.as_string().unwrap_or("").to_string()], - }; - package.complete_mut().set_license(license_vec); - } + if let Some(license) = config.get("license") + && !shirabe_php_shim::empty(license) + { + let license_vec: Vec = match license { + PhpMixed::Array(map) => map + .values() + .map(|v| v.as_string().unwrap_or("").to_string()) + .collect(), + PhpMixed::List(list) => list + .iter() + .map(|v| v.as_string().unwrap_or("").to_string()) + .collect(), + other => vec![other.as_string().unwrap_or("").to_string()], + }; + package.complete_mut().set_license(license_vec); } - if let Some(authors) = config.get("authors") { - if !shirabe_php_shim::empty(authors) { - if let PhpMixed::List(list) = authors { - let authors_vec: Vec> = list - .iter() - .filter_map(|v| match v.as_ref() { - PhpMixed::Array(m) => Some( - m.iter() - .map(|(k, v)| { - (k.clone(), v.as_string().unwrap_or("").to_string()) - }) - .collect(), - ), - _ => None, - }) - .collect(); - package.complete_mut().set_authors(authors_vec); - } - } + if let Some(authors) = config.get("authors") + && !shirabe_php_shim::empty(authors) + && let PhpMixed::List(list) = authors + { + let authors_vec: Vec> = list + .iter() + .filter_map(|v| match v.as_ref() { + PhpMixed::Array(m) => Some( + m.iter() + .map(|(k, v)| (k.clone(), v.as_string().unwrap_or("").to_string())) + .collect(), + ), + _ => None, + }) + .collect(); + package.complete_mut().set_authors(authors_vec); } - if let Some(support) = config.get("support") { - if let PhpMixed::Array(map) = support { - let support_map: IndexMap = map - .iter() - .map(|(k, v)| (k.clone(), v.as_string().unwrap_or("").to_string())) - .collect(); - package.complete_mut().set_support(support_map); - } + if let Some(support) = config.get("support") + && let PhpMixed::Array(map) = support + { + let support_map: IndexMap = map + .iter() + .map(|(k, v)| (k.clone(), v.as_string().unwrap_or("").to_string())) + .collect(); + package.complete_mut().set_support(support_map); } - if let Some(funding) = config.get("funding") { - if !shirabe_php_shim::empty(funding) { - if let PhpMixed::List(list) = funding { - let funding_vec: Vec> = list - .iter() - .filter_map(|v| match v.as_ref() { - PhpMixed::Array(m) => { - Some(m.iter().map(|(k, v)| (k.clone(), (**v).clone())).collect()) - } - _ => None, - }) - .collect(); - package.complete_mut().set_funding(funding_vec); - } - } + if let Some(funding) = config.get("funding") + && !shirabe_php_shim::empty(funding) + && let PhpMixed::List(list) = funding + { + let funding_vec: Vec> = list + .iter() + .filter_map(|v| match v.as_ref() { + PhpMixed::Array(m) => { + Some(m.iter().map(|(k, v)| (k.clone(), (**v).clone())).collect()) + } + _ => None, + }) + .collect(); + package.complete_mut().set_funding(funding_vec); } if let Some(abandoned) = config.get("abandoned") { package.complete_mut().set_abandoned(abandoned.clone()); } - if self.load_options { - if let Some(transport_options) = config.get("transport-options") { - let options = php_to_map(transport_options); - package.package_mut().set_transport_options(options); - } + if self.load_options + && let Some(transport_options) = config.get("transport-options") + { + let options = php_to_map(transport_options); + package.package_mut().set_transport_options(options); } let alias_normalized = self.get_branch_alias(config)?; - if let Some(alias_normalized) = alias_normalized { - if !alias_normalized.is_empty() { - let pretty_alias = Preg::replace(r"{(\.9{7})+}", ".x", &alias_normalized); - - return Ok(match package { - CompleteOrRootPackage::Root(root) => RootAliasPackageHandle::new( - RootPackageHandle::from_root_package(root), - alias_normalized, - pretty_alias, - ) - .into(), - CompleteOrRootPackage::Complete(complete) => CompleteAliasPackageHandle::new( - CompletePackageHandle::from_complete_package(complete), - alias_normalized, - pretty_alias, - ) - .into(), - }); - } + if let Some(alias_normalized) = alias_normalized + && !alias_normalized.is_empty() + { + let pretty_alias = Preg::replace(r"{(\.9{7})+}", ".x", &alias_normalized); + + return Ok(match package { + CompleteOrRootPackage::Root(root) => RootAliasPackageHandle::new( + RootPackageHandle::from_root_package(root), + alias_normalized, + pretty_alias, + ) + .into(), + CompleteOrRootPackage::Complete(complete) => CompleteAliasPackageHandle::new( + CompletePackageHandle::from_complete_package(complete), + alias_normalized, + pretty_alias, + ) + .into(), + }); } Ok(package.into_handle()) @@ -773,11 +765,11 @@ impl ArrayLoader { let entry = (target.clone(), link); link_cache .entry(name.clone()) - .or_insert_with(IndexMap::new) + .or_default() .entry(r#type.to_string()) - .or_insert_with(IndexMap::new) + .or_default() .entry(target.clone()) - .or_insert_with(IndexMap::new) + .or_default() .insert(constraint_str.clone(), entry.clone()); entry }; @@ -940,10 +932,10 @@ impl ArrayLoader { let target_prefix = self .version_parser .parse_numeric_alias_prefix(&target_branch); - if let (Some(sp), Some(tp)) = (source_prefix.as_ref(), target_prefix.as_ref()) { - if stripos(tp, sp) != Some(0) { - continue; - } + if let (Some(sp), Some(tp)) = (source_prefix.as_ref(), target_prefix.as_ref()) + && stripos(tp, sp) != Some(0) + { + continue; } return Ok(Some(validated_target_branch)); diff --git a/crates/shirabe/src/package/loader/root_package_loader.rs b/crates/shirabe/src/package/loader/root_package_loader.rs index 92f2be0..2d3bd0a 100644 --- a/crates/shirabe/src/package/loader/root_package_loader.rs +++ b/crates/shirabe/src/package/loader/root_package_loader.rs @@ -221,17 +221,17 @@ impl RootPackageLoader { } for link_type in SUPPORTED_LINK_TYPES.keys() { - if let Some(section) = config.get(*link_type) { - if let Some(section_map) = section.as_array() { - for (link_name, _constraint) in section_map { - if let Some(err) = - ValidatingArrayLoader::has_package_naming_error(link_name, true) - { - return Err(anyhow::anyhow!(RuntimeException { - message: format!("{}.{}", link_type, err), - code: 0, - })); - } + if let Some(section) = config.get(*link_type) + && let Some(section_map) = section.as_array() + { + for (link_name, _constraint) in section_map { + if let Some(err) = + ValidatingArrayLoader::has_package_naming_error(link_name, true) + { + return Err(anyhow::anyhow!(RuntimeException { + message: format!("{}.{}", link_type, err), + code: 0, + })); } } } @@ -392,14 +392,14 @@ impl RootPackageLoader { for (req_name, req_version) in requires { let req_version = Preg::replace(r"^([^,\s@]+) as .+$", "$1", req_version); let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"^[^,\s@]+?#([a-f0-9]+)$", &req_version, Some(&mut m)) { - if VersionParser::parse_stability(&req_version) == "dev" { - let name = strtolower(req_name); - references.insert( - name, - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - ); - } + if Preg::is_match3(r"^[^,\s@]+?#([a-f0-9]+)$", &req_version, Some(&mut m)) + && VersionParser::parse_stability(&req_version) == "dev" + { + let name = strtolower(req_name); + references.insert( + name, + m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), + ); } } references diff --git a/crates/shirabe/src/package/loader/validating_array_loader.rs b/crates/shirabe/src/package/loader/validating_array_loader.rs index dd2642e..490c0a9 100644 --- a/crates/shirabe/src/package/loader/validating_array_loader.rs +++ b/crates/shirabe/src/package/loader/validating_array_loader.rs @@ -43,9 +43,9 @@ impl ValidatingArrayLoader { parser: Option, flags: i64, ) -> Self { - let version_parser = parser.unwrap_or_else(|| VersionParser::new()); + let version_parser = parser.unwrap_or_default(); - if strict_name != true { + if !strict_name { trigger_error( "$strictName must be set to true in ValidatingArrayLoader's constructor as of 2.2, and it will be removed in 3.0", E_USER_DEPRECATED, @@ -72,21 +72,21 @@ impl ValidatingArrayLoader { self.config = config.clone(); self.validate_string("name", true); - if let Some(name_val) = config.get("name").and_then(|v| v.as_string()) { - if let Some(err) = Self::has_package_naming_error(name_val, false) { - self.errors.push(format!("name : {}", err)); - } + if let Some(name_val) = config.get("name").and_then(|v| v.as_string()) + && let Some(err) = Self::has_package_naming_error(name_val, false) + { + self.errors.push(format!("name : {}", err)); } if self.config.contains_key("version") { let version_val = self.config["version"].clone(); - if !is_scalar(&*version_val) { + if !is_scalar(&version_val) { self.validate_string("version", false); } else { - if !is_string(&*version_val) { + if !is_string(&version_val) { self.config.insert( "version".to_string(), - Box::new(PhpMixed::String(php_to_string(&*version_val))), + Box::new(PhpMixed::String(php_to_string(&version_val))), ); } let version_str = self @@ -111,36 +111,35 @@ impl ValidatingArrayLoader { .get("config") .and_then(|v| v.as_array()) .cloned() + && let Some(platform_val) = config_section.get("platform") { - if let Some(platform_val) = config_section.get("platform") { - let platform_array: IndexMap> = match platform_val.as_ref() { - PhpMixed::Array(m) => m.clone(), - other => { - let mut m = IndexMap::new(); - m.insert("0".to_string(), Box::new(other.clone())); - m - } - }; - for (key, platform) in &platform_array { - if let PhpMixed::Bool(false) = platform.as_ref() { - continue; - } - if !is_string(platform) { - self.errors.push(format!( - "config.platform.{} : invalid value ({} {}): expected string or false", - key, - get_debug_type(platform), - var_export(platform, true) - )); - continue; - } - let platform_str = platform.as_string().unwrap_or("").to_string(); - if let Err(e) = self.version_parser.normalize(&platform_str, None) { - self.errors.push(format!( - "config.platform.{} : invalid value ({}): {}", - key, platform_str, e - )); - } + let platform_array: IndexMap> = match platform_val.as_ref() { + PhpMixed::Array(m) => m.clone(), + other => { + let mut m = IndexMap::new(); + m.insert("0".to_string(), Box::new(other.clone())); + m + } + }; + for (key, platform) in &platform_array { + if let PhpMixed::Bool(false) = platform.as_ref() { + continue; + } + if !is_string(platform) { + self.errors.push(format!( + "config.platform.{} : invalid value ({} {}): expected string or false", + key, + get_debug_type(platform), + var_export(platform, true) + )); + continue; + } + let platform_str = platform.as_string().unwrap_or("").to_string(); + if let Err(e) = self.version_parser.normalize(&platform_str, None) { + self.errors.push(format!( + "config.platform.{} : invalid value ({}): {}", + key, platform_str, e + )); } } } @@ -150,7 +149,7 @@ impl ValidatingArrayLoader { self.validate_array("extra", false); if self.config.contains_key("bin") { - if is_string(&*self.config["bin"]) { + if is_string(&self.config["bin"]) { self.validate_string("bin", false); } else { self.validate_flat_array("bin", None, false); @@ -181,7 +180,7 @@ impl ValidatingArrayLoader { if self.config.contains_key("license") { let license_val = self.config["license"].clone(); // validate main data types - if is_array(&*license_val) || is_string(&*license_val) { + if is_array(&license_val) || is_string(&license_val) { let mut licenses: IndexMap> = match license_val.as_ref() { PhpMixed::Array(m) => m.clone(), other => { @@ -194,7 +193,7 @@ impl ValidatingArrayLoader { let license_keys: Vec = licenses.keys().cloned().collect(); for index in &license_keys { let license = licenses[index].clone(); - if !is_string(&*license) { + if !is_string(&license) { self.warnings.push(format!( "License {} should be a string.", PhpMixed::String(json_encode(&*license).unwrap_or_default()), @@ -264,11 +263,11 @@ impl ValidatingArrayLoader { .unwrap_or_default(); for key in &author_keys { let author = self.config["authors"].as_array().unwrap()[key].clone(); - if !is_array(&*author) { + if !is_array(&author) { self.errors.push(format!( "authors.{} : should be an array, {} given", key, - get_debug_type(&*author) + get_debug_type(&author) )); if let Some(PhpMixed::Array(m)) = self.config.get_mut("authors").map(|v| v.as_mut()) @@ -279,21 +278,19 @@ impl ValidatingArrayLoader { } for author_data in ["homepage", "email", "name", "role"] { let val_opt = author.as_array().and_then(|m| m.get(author_data)).cloned(); - if let Some(val) = val_opt { - if !is_string(&*val) { - self.errors.push(format!( - "authors.{}.{} : invalid value, must be a string", - key, author_data - )); - if let Some(PhpMixed::Array(authors)) = - self.config.get_mut("authors").map(|v| v.as_mut()) - { - if let Some(author_entry) = authors.get_mut(key) { - if let PhpMixed::Array(am) = author_entry.as_mut() { - am.shift_remove(author_data); - } - } - } + if let Some(val) = val_opt + && !is_string(&val) + { + self.errors.push(format!( + "authors.{}.{} : invalid value, must be a string", + key, author_data + )); + if let Some(PhpMixed::Array(authors)) = + self.config.get_mut("authors").map(|v| v.as_mut()) + && let Some(author_entry) = authors.get_mut(key) + && let PhpMixed::Array(am) = author_entry.as_mut() + { + am.shift_remove(author_data); } } } @@ -302,21 +299,19 @@ impl ValidatingArrayLoader { .and_then(|m| m.get("homepage")) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(homepage_str) = homepage { - if !self.filter_url(&homepage_str, &["http", "https"]) { - self.warnings.push(format!( - "authors.{}.homepage : invalid value ({}), must be an http/https URL", - key, homepage_str - )); - if let Some(PhpMixed::Array(authors)) = - self.config.get_mut("authors").map(|v| v.as_mut()) - { - if let Some(author_entry) = authors.get_mut(key) { - if let PhpMixed::Array(am) = author_entry.as_mut() { - am.shift_remove("homepage"); - } - } - } + if let Some(homepage_str) = homepage + && !self.filter_url(&homepage_str, &["http", "https"]) + { + self.warnings.push(format!( + "authors.{}.homepage : invalid value ({}), must be an http/https URL", + key, homepage_str + )); + if let Some(PhpMixed::Array(authors)) = + self.config.get_mut("authors").map(|v| v.as_mut()) + && let Some(author_entry) = authors.get_mut(key) + && let PhpMixed::Array(am) = author_entry.as_mut() + { + am.shift_remove("homepage"); } } let email = author @@ -324,21 +319,19 @@ impl ValidatingArrayLoader { .and_then(|m| m.get("email")) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(email_str) = email { - if !filter_var(&email_str, FILTER_VALIDATE_EMAIL) { - self.warnings.push(format!( - "authors.{}.email : invalid value ({}), must be a valid email address", - key, email_str - )); - if let Some(PhpMixed::Array(authors)) = - self.config.get_mut("authors").map(|v| v.as_mut()) - { - if let Some(author_entry) = authors.get_mut(key) { - if let PhpMixed::Array(am) = author_entry.as_mut() { - am.shift_remove("email"); - } - } - } + if let Some(email_str) = email + && !filter_var(&email_str, FILTER_VALIDATE_EMAIL) + { + self.warnings.push(format!( + "authors.{}.email : invalid value ({}), must be a valid email address", + key, email_str + )); + if let Some(PhpMixed::Array(authors)) = + self.config.get_mut("authors").map(|v| v.as_mut()) + && let Some(author_entry) = authors.get_mut(key) + && let PhpMixed::Array(am) = author_entry.as_mut() + { + am.shift_remove("email"); } } let current_author_len = self @@ -349,12 +342,11 @@ impl ValidatingArrayLoader { .and_then(|v| v.as_array()) .map(|m| m.len()) .unwrap_or(0); - if current_author_len == 0 { - if let Some(PhpMixed::Array(authors)) = + if current_author_len == 0 + && let Some(PhpMixed::Array(authors)) = self.config.get_mut("authors").map(|v| v.as_mut()) - { - authors.shift_remove(key); - } + { + authors.shift_remove(key); } } let authors_len = self @@ -381,15 +373,15 @@ impl ValidatingArrayLoader { .and_then(|v| v.as_array()) .and_then(|m| m.get(key)) .cloned(); - if let Some(val) = val_opt { - if !is_string(&*val) { - self.errors - .push(format!("support.{} : invalid value, must be a string", key)); - if let Some(PhpMixed::Array(support)) = - self.config.get_mut("support").map(|v| v.as_mut()) - { - support.shift_remove(key); - } + if let Some(val) = val_opt + && !is_string(&val) + { + self.errors + .push(format!("support.{} : invalid value, must be a string", key)); + if let Some(PhpMixed::Array(support)) = + self.config.get_mut("support").map(|v| v.as_mut()) + { + support.shift_remove(key); } } } @@ -401,17 +393,17 @@ impl ValidatingArrayLoader { .and_then(|m| m.get("email")) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(email_str) = support_email { - if !filter_var(&email_str, FILTER_VALIDATE_EMAIL) { - self.warnings.push(format!( - "support.email : invalid value ({}), must be a valid email address", - email_str - )); - if let Some(PhpMixed::Array(support)) = - self.config.get_mut("support").map(|v| v.as_mut()) - { - support.shift_remove("email"); - } + if let Some(email_str) = support_email + && !filter_var(&email_str, FILTER_VALIDATE_EMAIL) + { + self.warnings.push(format!( + "support.email : invalid value ({}), must be a valid email address", + email_str + )); + if let Some(PhpMixed::Array(support)) = + self.config.get_mut("support").map(|v| v.as_mut()) + { + support.shift_remove("email"); } } @@ -422,17 +414,17 @@ impl ValidatingArrayLoader { .and_then(|m| m.get("irc")) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(irc_str) = support_irc { - if !self.filter_url(&irc_str, &["irc", "ircs"]) { - self.warnings.push(format!( + if let Some(irc_str) = support_irc + && !self.filter_url(&irc_str, &["irc", "ircs"]) + { + self.warnings.push(format!( "support.irc : invalid value ({}), must be a irc:/// or ircs:// URL", irc_str )); - if let Some(PhpMixed::Array(support)) = - self.config.get_mut("support").map(|v| v.as_mut()) - { - support.shift_remove("irc"); - } + if let Some(PhpMixed::Array(support)) = + self.config.get_mut("support").map(|v| v.as_mut()) + { + support.shift_remove("irc"); } } @@ -446,17 +438,17 @@ impl ValidatingArrayLoader { .and_then(|m| m.get(key)) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(url_str) = url_opt { - if !self.filter_url(&url_str, &["http", "https"]) { - self.warnings.push(format!( - "support.{} : invalid value ({}), must be an http/https URL", - key, url_str - )); - if let Some(PhpMixed::Array(support)) = - self.config.get_mut("support").map(|v| v.as_mut()) - { - support.shift_remove(key); - } + if let Some(url_str) = url_opt + && !self.filter_url(&url_str, &["http", "https"]) + { + self.warnings.push(format!( + "support.{} : invalid value ({}), must be an http/https URL", + key, url_str + )); + if let Some(PhpMixed::Array(support)) = + self.config.get_mut("support").map(|v| v.as_mut()) + { + support.shift_remove(key); } } } @@ -476,11 +468,11 @@ impl ValidatingArrayLoader { .unwrap_or_default(); for key in &funding_keys { let funding_option = self.config["funding"].as_array().unwrap()[key].clone(); - if !is_array(&*funding_option) { + if !is_array(&funding_option) { self.errors.push(format!( "funding.{} : should be an array, {} given", key, - get_debug_type(&*funding_option) + get_debug_type(&funding_option) )); if let Some(PhpMixed::Array(funding)) = self.config.get_mut("funding").map(|v| v.as_mut()) @@ -494,21 +486,19 @@ impl ValidatingArrayLoader { .as_array() .and_then(|m| m.get(funding_data)) .cloned(); - if let Some(val) = val_opt { - if !is_string(&*val) { - self.errors.push(format!( - "funding.{}.{} : invalid value, must be a string", - key, funding_data - )); - if let Some(PhpMixed::Array(funding)) = - self.config.get_mut("funding").map(|v| v.as_mut()) - { - if let Some(entry) = funding.get_mut(key) { - if let PhpMixed::Array(em) = entry.as_mut() { - em.shift_remove(funding_data); - } - } - } + if let Some(val) = val_opt + && !is_string(&val) + { + self.errors.push(format!( + "funding.{}.{} : invalid value, must be a string", + key, funding_data + )); + if let Some(PhpMixed::Array(funding)) = + self.config.get_mut("funding").map(|v| v.as_mut()) + && let Some(entry) = funding.get_mut(key) + && let PhpMixed::Array(em) = entry.as_mut() + { + em.shift_remove(funding_data); } } } @@ -517,21 +507,19 @@ impl ValidatingArrayLoader { .and_then(|m| m.get("url")) .and_then(|v| v.as_string()) .map(|s| s.to_string()); - if let Some(url_str) = url { - if !self.filter_url(&url_str, &["http", "https"]) { - self.warnings.push(format!( - "funding.{}.url : invalid value ({}), must be an http/https URL", - key, url_str - )); - if let Some(PhpMixed::Array(funding)) = - self.config.get_mut("funding").map(|v| v.as_mut()) - { - if let Some(entry) = funding.get_mut(key) { - if let PhpMixed::Array(em) = entry.as_mut() { - em.shift_remove("url"); - } - } - } + if let Some(url_str) = url + && !self.filter_url(&url_str, &["http", "https"]) + { + self.warnings.push(format!( + "funding.{}.url : invalid value ({}), must be an http/https URL", + key, url_str + )); + if let Some(PhpMixed::Array(funding)) = + self.config.get_mut("funding").map(|v| v.as_mut()) + && let Some(entry) = funding.get_mut(key) + && let PhpMixed::Array(em) = entry.as_mut() + { + em.shift_remove("url"); } } let entry_empty = self @@ -542,12 +530,11 @@ impl ValidatingArrayLoader { .and_then(|v| v.as_array()) .map(|m| m.is_empty()) .unwrap_or(true); - if entry_empty { - if let Some(PhpMixed::Array(funding)) = + if entry_empty + && let Some(PhpMixed::Array(funding)) = self.config.get_mut("funding").map(|v| v.as_mut()) - { - funding.shift_remove(key); - } + { + funding.shift_remove(key); } } if Self::is_empty_array(self.config.get("funding")) { @@ -576,62 +563,63 @@ impl ValidatingArrayLoader { _ => IndexMap::new(), }; - if let Some(v) = php_ext.get("extension-name").cloned() { - if !is_string(&*v) { - self.errors.push(format!( - "php-ext.extension-name : should be a string, {} given", - get_debug_type(&*v) - )); - php_ext.shift_remove("extension-name"); - } + if let Some(v) = php_ext.get("extension-name").cloned() + && !is_string(&v) + { + self.errors.push(format!( + "php-ext.extension-name : should be a string, {} given", + get_debug_type(&v) + )); + php_ext.shift_remove("extension-name"); } - if let Some(v) = php_ext.get("priority").cloned() { - if !is_int(&*v) { - self.errors.push(format!( - "php-ext.priority : should be an integer, {} given", - get_debug_type(&*v) - )); - php_ext.shift_remove("priority"); - } + if let Some(v) = php_ext.get("priority").cloned() + && !is_int(&v) + { + self.errors.push(format!( + "php-ext.priority : should be an integer, {} given", + get_debug_type(&v) + )); + php_ext.shift_remove("priority"); } - if let Some(v) = php_ext.get("support-zts").cloned() { - if !is_bool(&*v) { - self.errors.push(format!( - "php-ext.support-zts : should be a boolean, {} given", - get_debug_type(&*v) - )); - php_ext.shift_remove("support-zts"); - } + if let Some(v) = php_ext.get("support-zts").cloned() + && !is_bool(&v) + { + self.errors.push(format!( + "php-ext.support-zts : should be a boolean, {} given", + get_debug_type(&v) + )); + php_ext.shift_remove("support-zts"); } - if let Some(v) = php_ext.get("support-nts").cloned() { - if !is_bool(&*v) { - self.errors.push(format!( - "php-ext.support-nts : should be a boolean, {} given", - get_debug_type(&*v) - )); - php_ext.shift_remove("support-nts"); - } + if let Some(v) = php_ext.get("support-nts").cloned() + && !is_bool(&v) + { + self.errors.push(format!( + "php-ext.support-nts : should be a boolean, {} given", + get_debug_type(&v) + )); + php_ext.shift_remove("support-nts"); } - if let Some(v) = php_ext.get("build-path").cloned() { - if !is_string(&*v) && !matches!(v.as_ref(), PhpMixed::Null) { - self.errors.push(format!( - "php-ext.build-path : should be a string or null, {} given", - get_debug_type(&*v) - )); - php_ext.shift_remove("build-path"); - } + if let Some(v) = php_ext.get("build-path").cloned() + && !is_string(&v) + && !matches!(v.as_ref(), PhpMixed::Null) + { + self.errors.push(format!( + "php-ext.build-path : should be a string or null, {} given", + get_debug_type(&v) + )); + php_ext.shift_remove("build-path"); } if php_ext.contains_key("download-url-method") { let v = php_ext["download-url-method"].clone(); - if !is_array(&*v) && !is_string(&*v) { + if !is_array(&v) && !is_string(&v) { self.errors.push(format!( "php-ext.download-url-method : should be an array or a string, {} given", - get_debug_type(&*v) + get_debug_type(&v) )); php_ext.shift_remove("download-url-method"); } else { @@ -641,7 +629,7 @@ impl ValidatingArrayLoader { "pre-packaged-binary", ]; let defined_download_url_methods: IndexMap> = - if is_array(&*v) { + if is_array(&v) { v.as_array().unwrap().clone() } else { let mut m = IndexMap::new(); @@ -657,11 +645,11 @@ impl ValidatingArrayLoader { php_ext.shift_remove("download-url-method"); } else { for (key, download_url_method) in &defined_download_url_methods { - if !is_string(&**download_url_method) { + if !is_string(download_url_method) { self.errors.push(format!( "php-ext.download-url-method.{} : should be a string, {} given", key, - get_debug_type(&**download_url_method) + get_debug_type(download_url_method) )); php_ext.shift_remove("download-url-method"); } else if !valid_download_url_methods @@ -695,11 +683,11 @@ impl ValidatingArrayLoader { for field_name in ["os-families", "os-families-exclude"] { if let Some(field_val) = php_ext.get(field_name).cloned() { - if !is_array(&*field_val) { + if !is_array(&field_val) { self.errors.push(format!( "php-ext.{} : should be an array, {} given", field_name, - get_debug_type(&*field_val) + get_debug_type(&field_val) )); php_ext.shift_remove(field_name); } else if field_val.as_array().unwrap().is_empty() { @@ -713,12 +701,12 @@ impl ValidatingArrayLoader { field_val.as_array().unwrap().keys().cloned().collect(); for key in &field_keys { let os_family = field_val.as_array().unwrap()[key].clone(); - if !is_string(&*os_family) { + if !is_string(&os_family) { self.errors.push(format!( "php-ext.{}.{} : should be a string, {} given", field_name, key, - get_debug_type(&*os_family) + get_debug_type(&os_family) )); if let Some(PhpMixed::Array(arr)) = php_ext.get_mut(field_name).map(|v| v.as_mut()) @@ -757,10 +745,10 @@ impl ValidatingArrayLoader { if php_ext.contains_key("configure-options") { let configure_options = php_ext["configure-options"].clone(); - if !is_array(&*configure_options) { + if !is_array(&configure_options) { self.errors.push(format!( "php-ext.configure-options : should be an array, {} given", - get_debug_type(&*configure_options) + get_debug_type(&configure_options) )); php_ext.shift_remove("configure-options"); } else { @@ -772,11 +760,11 @@ impl ValidatingArrayLoader { .collect(); for key in &configure_keys { let option = configure_options.as_array().unwrap()[key].clone(); - if !is_array(&*option) { + if !is_array(&option) { self.errors.push(format!( "php-ext.configure-options.{} : should be an array, {} given", key, - get_debug_type(&*option) + get_debug_type(&option) )); if let Some(PhpMixed::Array(arr)) = php_ext.get_mut("configure-options").map(|v| v.as_mut()) @@ -801,11 +789,11 @@ impl ValidatingArrayLoader { } let name_val = option_map["name"].clone(); - if !is_string(&*name_val) { + if !is_string(&name_val) { self.errors.push(format!( "php-ext.configure-options.{}.name : should be a string, {} given", key, - get_debug_type(&*name_val) + get_debug_type(&name_val) )); if let Some(PhpMixed::Array(arr)) = php_ext.get_mut("configure-options").map(|v| v.as_mut()) @@ -815,41 +803,37 @@ impl ValidatingArrayLoader { continue; } - if let Some(needs_value) = option_map.get("needs-value").cloned() { - if !is_bool(&*needs_value) { - self.errors.push(format!( + if let Some(needs_value) = option_map.get("needs-value").cloned() + && !is_bool(&needs_value) + { + self.errors.push(format!( "php-ext.configure-options.{}.needs-value : should be a boolean, {} given", key, - get_debug_type(&*needs_value) + get_debug_type(&needs_value) )); - if let Some(PhpMixed::Array(co)) = - php_ext.get_mut("configure-options").map(|v| v.as_mut()) - { - if let Some(entry) = co.get_mut(key) { - if let PhpMixed::Array(em) = entry.as_mut() { - em.shift_remove("needs-value"); - } - } - } + if let Some(PhpMixed::Array(co)) = + php_ext.get_mut("configure-options").map(|v| v.as_mut()) + && let Some(entry) = co.get_mut(key) + && let PhpMixed::Array(em) = entry.as_mut() + { + em.shift_remove("needs-value"); } } - if let Some(description) = option_map.get("description").cloned() { - if !is_string(&*description) { - self.errors.push(format!( + if let Some(description) = option_map.get("description").cloned() + && !is_string(&description) + { + self.errors.push(format!( "php-ext.configure-options.{}.description : should be a string, {} given", key, - get_debug_type(&*description) + get_debug_type(&description) )); - if let Some(PhpMixed::Array(co)) = - php_ext.get_mut("configure-options").map(|v| v.as_mut()) - { - if let Some(entry) = co.get_mut(key) { - if let PhpMixed::Array(em) = entry.as_mut() { - em.shift_remove("description"); - } - } - } + if let Some(PhpMixed::Array(co)) = + php_ext.get_mut("configure-options").map(|v| v.as_mut()) + && let Some(entry) = co.get_mut(key) + && let PhpMixed::Array(em) = entry.as_mut() + { + em.shift_remove("description"); } } } @@ -885,19 +869,19 @@ impl ValidatingArrayLoader { .unwrap_or_default(); for (package, constraint) in &link_section { let package = package.to_string(); - if let Some(name_val) = self.config.get("name").and_then(|v| v.as_string()) { - if strcasecmp(&package, name_val) == 0 { - self.errors.push(format!( - "{}.{} : a package cannot set a {} on itself", - link_type, package, link_type - )); - if let Some(PhpMixed::Array(arr)) = - self.config.get_mut(link_type).map(|v| v.as_mut()) - { - arr.shift_remove(&package); - } - continue; + if let Some(name_val) = self.config.get("name").and_then(|v| v.as_string()) + && strcasecmp(&package, name_val) == 0 + { + self.errors.push(format!( + "{}.{} : a package cannot set a {} on itself", + link_type, package, link_type + )); + if let Some(PhpMixed::Array(arr)) = + self.config.get_mut(link_type).map(|v| v.as_mut()) + { + arr.shift_remove(&package); } + continue; } if let Some(err) = Self::has_package_naming_error(&package, true) { self.warnings.push(format!("{}.{}", link_type, err)); @@ -907,7 +891,7 @@ impl ValidatingArrayLoader { link_type, package )); } - if !is_string(&**constraint) { + if !is_string(constraint) { self.errors.push(format!( "{}.{} : invalid value, must be a string containing a version constraint", link_type, package @@ -950,7 +934,7 @@ impl ValidatingArrayLoader { && link_type == "require" && link_constraint .as_constraint() - .map_or(false, |c| ["==", "="].contains(&c.get_operator())) + .is_some_and(|c| ["==", "="].contains(&c.get_operator())) && AnyConstraint::from(SimpleConstraint::new( ">=".to_string(), "1.0.0.0-dev".to_string(), @@ -1017,7 +1001,7 @@ impl ValidatingArrayLoader { .cloned() .unwrap_or_default(); for (package, description) in &suggest_map { - if !is_string(&**description) { + if !is_string(description) { self.errors.push(format!( "suggest.{} : invalid value, must be a string describing why the package is suggested", package @@ -1076,16 +1060,16 @@ impl ValidatingArrayLoader { arr.shift_remove(r#type); } } - if r#type == "psr-4" { - if let Some(type_map) = type_config.as_array() { - for (namespace, _dirs) in type_map { - let ns_str = namespace.as_str(); - if ns_str != "" && substr(ns_str, -1, None) != "\\" { - self.errors.push(format!( + if r#type == "psr-4" + && let Some(type_map) = type_config.as_array() + { + for (namespace, _dirs) in type_map { + let ns_str = namespace.as_str(); + if !ns_str.is_empty() && substr(ns_str, -1, None) != "\\" { + self.errors.push(format!( "autoload.psr-4 : invalid value ({}), namespaces must end with a namespace separator, should be {}\\\\", ns_str, ns_str )); - } } } } @@ -1132,35 +1116,36 @@ impl ValidatingArrayLoader { self.errors .push(format!("{}.reference : must be present", src_type)); } - if let Some(type_val) = section.get("type") { - if !is_string(&**type_val) { - self.errors.push(format!( - "{}.type : should be a string, {} given", - src_type, - get_debug_type(&**type_val) - )); - } + if let Some(type_val) = section.get("type") + && !is_string(type_val) + { + self.errors.push(format!( + "{}.type : should be a string, {} given", + src_type, + get_debug_type(type_val) + )); } - if let Some(url_val) = section.get("url") { - if !is_string(&**url_val) { - self.errors.push(format!( - "{}.url : should be a string, {} given", - src_type, - get_debug_type(&**url_val) - )); - } + if let Some(url_val) = section.get("url") + && !is_string(url_val) + { + self.errors.push(format!( + "{}.url : should be a string, {} given", + src_type, + get_debug_type(url_val) + )); } - if let Some(ref_val) = section.get("reference") { - if !is_string(&**ref_val) && !is_int(&**ref_val) { - self.errors.push(format!( - "{}.reference : should be a string or int, {} given", - src_type, - get_debug_type(&**ref_val) - )); - } + if let Some(ref_val) = section.get("reference") + && !is_string(ref_val) + && !is_int(ref_val) + { + self.errors.push(format!( + "{}.reference : should be a string or int, {} given", + src_type, + get_debug_type(ref_val) + )); } if let Some(ref_val) = section.get("reference") { - let ref_str = php_to_string(&**ref_val); + let ref_str = php_to_string(ref_val); if Preg::is_match("{^\\s*-}", &ref_str) { self.errors.push(format!( "{}.reference : must not start with a \"-\", \"{}\" given", @@ -1169,7 +1154,7 @@ impl ValidatingArrayLoader { } } if let Some(url_val) = section.get("url") { - let url_str = php_to_string(&**url_val); + let url_str = php_to_string(url_val); if Preg::is_match("{^\\s*-}", &url_str) { self.errors.push(format!( "{}.url : must not start with a \"-\", \"{}\" given", @@ -1195,28 +1180,26 @@ impl ValidatingArrayLoader { .unwrap_or(false); if has_branch_alias { let branch_alias_val = self.config["extra"].as_array().unwrap()["branch-alias"].clone(); - if !is_array(&*branch_alias_val) { + if !is_array(&branch_alias_val) { self.errors.push( "extra.branch-alias : must be an array of versions => aliases".to_string(), ); } else { let branch_alias_map = branch_alias_val.as_array().cloned().unwrap_or_default(); for (source_branch, target_branch) in &branch_alias_map { - if !is_string(&**target_branch) { + if !is_string(target_branch) { self.warnings.push(format!( "extra.branch-alias.{} : the target branch ({}) must be a string, \"{}\" received.", source_branch, json_encode(&**target_branch).unwrap_or_default(), - get_debug_type(&**target_branch) + get_debug_type(target_branch) )); if let Some(PhpMixed::Array(extra)) = self.config.get_mut("extra").map(|v| v.as_mut()) + && let Some(ba) = extra.get_mut("branch-alias") + && let PhpMixed::Array(bam) = ba.as_mut() { - if let Some(ba) = extra.get_mut("branch-alias") { - if let PhpMixed::Array(bam) = ba.as_mut() { - bam.shift_remove(source_branch); - } - } + bam.shift_remove(source_branch); } continue; } @@ -1231,12 +1214,10 @@ impl ValidatingArrayLoader { )); if let Some(PhpMixed::Array(extra)) = self.config.get_mut("extra").map(|v| v.as_mut()) + && let Some(ba) = extra.get_mut("branch-alias") + && let PhpMixed::Array(bam) = ba.as_mut() { - if let Some(ba) = extra.get_mut("branch-alias") { - if let PhpMixed::Array(bam) = ba.as_mut() { - bam.shift_remove(source_branch); - } - } + bam.shift_remove(source_branch); } continue; } @@ -1255,12 +1236,10 @@ impl ValidatingArrayLoader { )); if let Some(PhpMixed::Array(extra)) = self.config.get_mut("extra").map(|v| v.as_mut()) + && let Some(ba) = extra.get_mut("branch-alias") + && let PhpMixed::Array(bam) = ba.as_mut() { - if let Some(ba) = extra.get_mut("branch-alias") { - if let PhpMixed::Array(bam) = ba.as_mut() { - bam.shift_remove(source_branch); - } - } + bam.shift_remove(source_branch); } continue; } @@ -1272,21 +1251,19 @@ impl ValidatingArrayLoader { let target_prefix = self .version_parser .parse_numeric_alias_prefix(&target_branch_str); - if let (Some(sp), Some(tp)) = (source_prefix, target_prefix) { - if !tp.to_lowercase().starts_with(&sp.to_lowercase()) { - self.warnings.push(format!( + if let (Some(sp), Some(tp)) = (source_prefix, target_prefix) + && !tp.to_lowercase().starts_with(&sp.to_lowercase()) + { + self.warnings.push(format!( "extra.branch-alias.{} : the target branch ({}) is not a valid numeric alias for this version", source_branch, target_branch_str )); - if let Some(PhpMixed::Array(extra)) = - self.config.get_mut("extra").map(|v| v.as_mut()) - { - if let Some(ba) = extra.get_mut("branch-alias") { - if let PhpMixed::Array(bam) = ba.as_mut() { - bam.shift_remove(source_branch); - } - } - } + if let Some(PhpMixed::Array(extra)) = + self.config.get_mut("extra").map(|v| v.as_mut()) + && let Some(ba) = extra.get_mut("branch-alias") + && let PhpMixed::Array(bam) = ba.as_mut() + { + bam.shift_remove(source_branch); } } } @@ -1410,11 +1387,11 @@ impl ValidatingArrayLoader { } fn validate_string(&mut self, property: &str, mandatory: bool) -> bool { - if self.config.contains_key(property) && !is_string(&*self.config[property]) { + if self.config.contains_key(property) && !is_string(&self.config[property]) { self.errors.push(format!( "{} : should be a string, {} given", property, - get_debug_type(&*self.config[property]) + get_debug_type(&self.config[property]) )); self.config.shift_remove(property); @@ -1425,7 +1402,8 @@ impl ValidatingArrayLoader { || trim( self.config[property].as_string().unwrap_or(""), Some(" \t\n\r\0\u{0B}"), - ) == ""; + ) + .is_empty(); if is_empty { if mandatory { self.errors.push(format!("{} : must be present", property)); @@ -1439,11 +1417,11 @@ impl ValidatingArrayLoader { } fn validate_array(&mut self, property: &str, mandatory: bool) -> bool { - if self.config.contains_key(property) && !is_array(&*self.config[property]) { + if self.config.contains_key(property) && !is_array(&self.config[property]) { self.errors.push(format!( "{} : should be an array, {} given", property, - get_debug_type(&*self.config[property]) + get_debug_type(&self.config[property]) )); self.config.shift_remove(property); @@ -1486,12 +1464,12 @@ impl ValidatingArrayLoader { .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect()) .unwrap_or_default(); for (key, value) in entries { - if !is_string(&*value) && !is_numeric(&*value) { + if !is_string(&value) && !is_numeric(&value) { self.errors.push(format!( "{}.{} : must be a string or int, {} given", property, key, - get_debug_type(&*value) + get_debug_type(&value) )); if let Some(PhpMixed::Array(arr)) = self.config.get_mut(property).map(|v| v.as_mut()) @@ -1504,7 +1482,7 @@ impl ValidatingArrayLoader { } if let Some(regex_str) = regex { - let value_str = php_to_string(&*value); + let value_str = php_to_string(&value); if !Preg::is_match(&format!("{{^{}$}}u", regex_str), &value_str) { self.warnings.push(format!( "{}.{} : invalid value ({}), must match {}", @@ -1543,7 +1521,7 @@ impl ValidatingArrayLoader { } fn filter_url(&self, value: &str, schemes: &[&str]) -> bool { - if value == "" { + if value.is_empty() { return true; } diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs index 1a16c27..369433e 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -240,33 +240,33 @@ impl Locker { } } - if let Some(aliases) = lock_data.get("aliases") { - if let PhpMixed::List(alias_list) = aliases { - for alias in alias_list { - if let PhpMixed::Array(m) = alias.as_ref() { - let alias_pkg_name = m - .get("package") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(); - if let Some(base_pkg) = package_by_name.get(&alias_pkg_name) { - let alias_of = base_pkg.as_complete_package().expect( + if let Some(aliases) = lock_data.get("aliases") + && let PhpMixed::List(alias_list) = aliases + { + for alias in alias_list { + if let PhpMixed::Array(m) = alias.as_ref() { + let alias_pkg_name = m + .get("package") + .and_then(|v| v.as_string()) + .unwrap_or("") + .to_string(); + if let Some(base_pkg) = package_by_name.get(&alias_pkg_name) { + let alias_of = base_pkg.as_complete_package().expect( "CompleteAliasPackage requires aliasOf to be a real CompletePackage", ); - let alias_pkg = CompleteAliasPackageHandle::new( - alias_of, - m.get("alias_normalized") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(), - m.get("alias") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(), - ); - alias_pkg.set_root_package_alias(true); - packages.add_package(alias_pkg.into())?; - } + let alias_pkg = CompleteAliasPackageHandle::new( + alias_of, + m.get("alias_normalized") + .and_then(|v| v.as_string()) + .unwrap_or("") + .to_string(), + m.get("alias") + .and_then(|v| v.as_string()) + .unwrap_or("") + .to_string(), + ); + alias_pkg.set_root_package_alias(true); + packages.add_package(alias_pkg.into())?; } } } @@ -465,6 +465,7 @@ impl Locker { } /// Locks provided data into lockfile. + #[allow(clippy::too_many_arguments, reason = "to keep PHP signature")] pub fn set_lock_data( &mut self, packages: Vec, @@ -585,7 +586,7 @@ impl Locker { .collect(), ), ); - if platform_overrides.len() > 0 { + if !platform_overrides.is_empty() { lock.insert( "platform-overrides".to_string(), PhpMixed::Array( @@ -663,7 +664,7 @@ impl Locker { where F: FnOnce(IndexMap) -> IndexMap, { - let contents = file_get_contents(&composer_json.get_path()); + let contents = file_get_contents(composer_json.get_path()); let contents = match contents { Some(s) => s, None => { @@ -678,7 +679,7 @@ impl Locker { } }; - let lock_mtime = filemtime(&self.lock_file.get_path()); + let lock_mtime = filemtime(self.lock_file.get_path()); let lock_data_php = self.lock_file.read()?; let mut lock_data: IndexMap = match lock_data_php { PhpMixed::Array(m) => m.into_iter().map(|(k, v)| (k, *v)).collect(), @@ -700,10 +701,10 @@ impl Locker { ))?; *self.lock_data_cache.borrow_mut() = None; self.virtual_file_written = false; - if let Some(mtime) = lock_mtime { - if is_int(&PhpMixed::Int(mtime)) { - let _ = touch2(&self.lock_file.get_path(), mtime); - } + if let Some(mtime) = lock_mtime + && is_int(&PhpMixed::Int(mtime)) + { + let _ = touch2(self.lock_file.get_path(), mtime); } Ok(()) } @@ -930,7 +931,7 @@ impl Locker { method: "getRequires".to_string(), description: "Required".to_string(), }]; - if include_dev == true { + if include_dev { sets.push(SetEntry { repo: self.get_locked_repository(true)?, method: "getDevRequires".to_string(), @@ -949,7 +950,7 @@ impl Locker { _ => unreachable!(), }; for link in links.values() { - if PlatformRepository::is_platform_package(&link.get_target()) { + if PlatformRepository::is_platform_package(link.get_target()) { continue; } if link.get_pretty_constraint() == "self.version" { @@ -957,7 +958,7 @@ impl Locker { } if installed_repo .find_packages_with_replacers_and_providers( - &link.get_target(), + link.get_target(), Some(FindPackageConstraint::Constraint( link.get_constraint().clone(), )), @@ -965,7 +966,7 @@ impl Locker { .is_empty() { let results = installed_repo - .find_packages_with_replacers_and_providers(&link.get_target(), None)?; + .find_packages_with_replacers_and_providers(link.get_target(), None)?; if !results.is_empty() { // PHP `reset($results)` returns the first shared package; clone the handle. diff --git a/crates/shirabe/src/package/mod.rs b/crates/shirabe/src/package/mod.rs index 903056f..912ac17 100644 --- a/crates/shirabe/src/package/mod.rs +++ b/crates/shirabe/src/package/mod.rs @@ -10,6 +10,7 @@ pub mod handle; pub mod link; pub mod loader; pub mod locker; +#[allow(clippy::module_inception, reason = "to port PHP's structure as it is")] pub mod package; pub mod package_interface; pub mod root_alias_package; diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs index 8ef93be..a579baa 100644 --- a/crates/shirabe/src/package/package.rs +++ b/crates/shirabe/src/package/package.rs @@ -719,14 +719,14 @@ impl PackageInterface for Package { self.php_ext.clone() } fn set_repository(&mut self, repository: RepositoryInterfaceHandle) -> anyhow::Result<()> { - if let Some(existing) = self.repository.as_ref().and_then(|w| w.upgrade()) { - if !Rc::ptr_eq(&existing, repository.as_rc()) { - return Err(LogicException { - message: "A package can only be added to one repository".to_string(), - code: 0, - } - .into()); + if let Some(existing) = self.repository.as_ref().and_then(|w| w.upgrade()) + && !Rc::ptr_eq(&existing, repository.as_rc()) + { + return Err(LogicException { + message: "A package can only be added to one repository".to_string(), + code: 0, } + .into()); } self.repository = Some(repository.downgrade()); Ok(()) diff --git a/crates/shirabe/src/package/version/stability_filter.rs b/crates/shirabe/src/package/version/stability_filter.rs index da2f1a6..0bcfe4a 100644 --- a/crates/shirabe/src/package/version/stability_filter.rs +++ b/crates/shirabe/src/package/version/stability_filter.rs @@ -15,10 +15,10 @@ impl StabilityFilter { for name in names { // allow if package matches the package-specific stability flag if let Some(&flag) = stability_flags.get(name) { - if let Some(&stability_value) = STABILITIES.get(stability) { - if stability_value <= flag { - return true; - } + if let Some(&stability_value) = STABILITIES.get(stability) + && stability_value <= flag + { + return true; } } else if acceptable_stabilities.contains_key(stability) { // allow if package matches the global stability requirement and has no exception diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs index 4eb3864..5d41434 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -90,10 +90,10 @@ impl VersionGuesser { } let version_data = self.guess_hg_version(package_config, path)?; - if let Some(vd) = version_data { - if vd.version.is_some() { - return Ok(Some(self.postprocess(vd))); - } + if let Some(vd) = version_data + && vd.version.is_some() + { + return Ok(Some(self.postprocess(vd))); } let version_data = self.guess_fossil_version(path)?; @@ -102,10 +102,10 @@ impl VersionGuesser { } let version_data = self.guess_svn_version(package_config, path)?; - if let Some(vd) = version_data { - if vd.version.is_some() { - return Ok(Some(self.postprocess(vd))); - } + if let Some(vd) = version_data + && vd.version.is_some() + { + return Ok(Some(self.postprocess(vd))); } Ok(None) @@ -173,7 +173,7 @@ impl VersionGuesser { package_config: &IndexMap, path: &str, ) -> Result { - GitUtil::clean_env(&mut self.process); + GitUtil::clean_env(&self.process); let mut commit: Option = None; let mut version: Option = None; let mut pretty_version: Option = None; @@ -263,7 +263,7 @@ impl VersionGuesser { } } GitUtil::check_for_repo_ownership_error( - &self.process.borrow().get_error_output(), + self.process.borrow().get_error_output(), path, self.io.clone(), ); diff --git a/crates/shirabe/src/package/version/version_parser.rs b/crates/shirabe/src/package/version/version_parser.rs index e600c37..87fa6c8 100644 --- a/crates/shirabe/src/package/version/version_parser.rs +++ b/crates/shirabe/src/package/version/version_parser.rs @@ -18,6 +18,12 @@ pub struct VersionParser { inner: SemverVersionParser, } +impl Default for VersionParser { + fn default() -> Self { + Self::new() + } +} + impl VersionParser { pub const DEFAULT_BRANCH_ALIAS: &'static str = "9999999-dev"; @@ -45,7 +51,7 @@ impl VersionParser { let count = pairs.len(); let mut i = 0_usize; while i < count { - let mut pair = Preg::replace(r"{^([^=: ]+)[=: ](.*)$}", "$1 $2", &pairs[i].trim()); + let mut pair = Preg::replace(r"{^([^=: ]+)[=: ](.*)$}", "$1 $2", pairs[i].trim()); if !pair.contains(' ') && i + 1 < count && !pairs[i + 1].contains('/') diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs index 872ca93..0d69132 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -59,6 +59,7 @@ impl VersionSelector { }) } + #[allow(clippy::too_many_arguments, reason = "to keep PHP signature")] pub fn find_best_candidate( &mut self, package_name: &str, @@ -92,7 +93,7 @@ impl VersionSelector { }; let mut candidates = self.repository_set.borrow().find_packages( &strtolower(package_name), - constraint.as_ref().map(|c| c.clone()), + constraint.clone(), repo_set_flags, )?; @@ -158,16 +159,16 @@ impl VersionSelector { .as_any() .downcast_ref::( ); - if let Some(list_filter) = list_filter_opt { - if list_filter.is_upper_bound_ignored(name) { - let filtered_constraint = list_filter.filter_constraint( - name, - link.get_constraint().clone(), - false, - )?; - if filtered_constraint.matches(provided_constraint) { - continue 'reqs; - } + if let Some(list_filter) = list_filter_opt + && list_filter.is_upper_bound_ignored(name) + { + let filtered_constraint = list_filter.filter_constraint( + name, + link.get_constraint().clone(), + false, + )?; + if filtered_constraint.matches(provided_constraint) { + continue 'reqs; } } } @@ -220,13 +221,13 @@ impl VersionSelector { continue; } - found_package = Some(pkg.clone().into()); + found_package = Some(pkg.clone()); break; } package = found_package; } else { package = if !candidates.is_empty() { - Some(candidates.remove(0).into()) + Some(candidates.remove(0)) } else { None }; @@ -279,14 +280,13 @@ impl VersionSelector { let loader = ArrayLoader::new(Some(self.get_parser().clone()), false); let dumper = ArrayDumper::new(); let extra = loader.get_branch_alias(&dumper.dump(package.clone()))?; - if let Some(extra) = extra { - if extra != VersionParser::DEFAULT_BRANCH_ALIAS { - let new_extra = - Preg::replace(r"{^(\d+\.\d+\.\d+)(\.9999999)-dev$}", "$1.0", &extra); - if new_extra != extra { - let new_extra = new_extra.replace(".9999999", ".0"); - return self.transform_version(&new_extra, &new_extra, "dev"); - } + if let Some(extra) = extra + && extra != VersionParser::DEFAULT_BRANCH_ALIAS + { + let new_extra = Preg::replace(r"{^(\d+\.\d+\.\d+)(\.9999999)-dev$}", "$1.0", &extra); + if new_extra != extra { + let new_extra = new_extra.replace(".9999999", ".0"); + return self.transform_version(&new_extra, &new_extra, "dev"); } } -- cgit v1.3.1