From 6239bb9fd8e04fe539b7e6c8c4bd2b772560cb29 Mon Sep 17 00:00:00 2001 From: Warren Hood Date: Sun, 1 Sep 2024 12:57:58 +0200 Subject: [PATCH] Switch all HashSets to BTreeSets --- mcmpmgr/src/mod_meta.rs | 16 ++++++++++++++-- mcmpmgr/src/modpack.rs | 4 ++-- mcmpmgr/src/providers/mod.rs | 5 +++-- mcmpmgr/src/providers/modrinth.rs | 4 ++-- mcmpmgr/src/resolver.rs | 18 +++++++++--------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/mcmpmgr/src/mod_meta.rs b/mcmpmgr/src/mod_meta.rs index cf7d3e3..0cd0940 100644 --- a/mcmpmgr/src/mod_meta.rs +++ b/mcmpmgr/src/mod_meta.rs @@ -4,7 +4,7 @@ use std::{borrow::BorrowMut, error::Error}; use crate::modpack::ModLoader; -#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)] pub enum ModProvider { /// Get mods from CurseForge CurseForge, @@ -36,7 +36,7 @@ pub struct ModMeta { pub loader: Option, pub download_url: Option, pub server_side: Option, - pub client_side: Option + pub client_side: Option, } impl PartialEq for ModMeta { @@ -46,6 +46,18 @@ impl PartialEq for ModMeta { } } +impl PartialOrd for ModMeta { + fn partial_cmp(&self, other: &Self) -> Option { + self.name.partial_cmp(&other.name) + } +} + +impl Ord for ModMeta { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.name.cmp(&other.name) + } +} + impl Eq for ModMeta {} impl ModMeta { diff --git a/mcmpmgr/src/modpack.rs b/mcmpmgr/src/modpack.rs index 1e3f327..a654f2d 100644 --- a/mcmpmgr/src/modpack.rs +++ b/mcmpmgr/src/modpack.rs @@ -2,7 +2,7 @@ use crate::mod_meta::{ModMeta, ModProvider}; use anyhow::Result; use serde::{Deserialize, Serialize}; use std::{ - collections::{BTreeMap, HashSet}, + collections::{BTreeMap, BTreeSet}, path::{Path, PathBuf}, }; @@ -43,7 +43,7 @@ pub struct ModpackMeta { pub modloader: ModLoader, pub mods: BTreeMap, pub default_providers: Vec, - pub forbidden_mods: HashSet, + pub forbidden_mods: BTreeSet, } impl ModpackMeta { diff --git a/mcmpmgr/src/providers/mod.rs b/mcmpmgr/src/providers/mod.rs index e00b578..ec898cf 100644 --- a/mcmpmgr/src/providers/mod.rs +++ b/mcmpmgr/src/providers/mod.rs @@ -1,6 +1,6 @@ use crate::mod_meta::ModMeta; use serde::{Deserialize, Serialize}; -use std::{collections::HashSet, fmt::Display, path::PathBuf, str::FromStr}; +use std::{collections::BTreeSet, fmt::Display, path::PathBuf, str::FromStr}; pub mod modrinth; pub mod raw; @@ -58,7 +58,8 @@ pub struct PinnedMod { /// Version of mod pub version: String, /// Pinned dependencies of a pinned mod - pub deps: Option>, + // pub deps: Option>, + pub deps: Option>, /// Server side pub server_side: bool, /// Required on client side diff --git a/mcmpmgr/src/providers/modrinth.rs b/mcmpmgr/src/providers/modrinth.rs index dd3a377..38cea9b 100644 --- a/mcmpmgr/src/providers/modrinth.rs +++ b/mcmpmgr/src/providers/modrinth.rs @@ -1,6 +1,6 @@ use anyhow::{Error, Result}; use serde::{Deserialize, Serialize}; -use std::collections::HashSet; +use std::collections::BTreeSet; use super::PinnedMod; use crate::{ @@ -158,7 +158,7 @@ impl Modrinth { } }; - let mut deps_meta = HashSet::new(); + let mut deps_meta = BTreeSet::new(); if let Some(deps) = &package.dependencies { for dep in deps.iter().filter(|dep| dep.dependency_type == "required") { deps_meta.insert( diff --git a/mcmpmgr/src/resolver.rs b/mcmpmgr/src/resolver.rs index ca62450..1fe3aed 100644 --- a/mcmpmgr/src/resolver.rs +++ b/mcmpmgr/src/resolver.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use sha1::Sha1; use sha2::{Digest, Sha512}; use std::{ - collections::{BTreeMap, HashSet}, + collections::{BTreeMap, BTreeSet}, ffi::{OsStr, OsString}, path::{Path, PathBuf}, }; @@ -39,7 +39,7 @@ impl PinnedPackMeta { download_side: DownloadSide, ) -> Result<()> { let files = std::fs::read_dir(mods_dir)?; - let mut pinned_files_cache = HashSet::new(); + let mut pinned_files_cache = BTreeSet::new(); for file in files.into_iter() { let file = file?; if file.file_type()?.is_file() { @@ -109,7 +109,7 @@ impl PinnedPackMeta { &self, file_name: &OsStr, mod_side: DownloadSide, - cache: &mut HashSet, + cache: &mut BTreeSet, ) -> bool { if cache.contains(file_name) { return true; @@ -166,7 +166,7 @@ impl PinnedPackMeta { } } let mut deps = - HashSet::from_iter(self.pin_mod(mod_metadata, pack_metadata).await?.into_iter()); + BTreeSet::from_iter(self.pin_mod(mod_metadata, pack_metadata).await?.into_iter()); if ignore_transitive_versions { // Ignore transitive dep versions @@ -181,7 +181,7 @@ impl PinnedPackMeta { .clone(); while !deps.is_empty() { - let mut next_deps = HashSet::new(); + let mut next_deps = BTreeSet::new(); for dep in deps.iter() { println!( "Adding mod {}@{} (dependency of {}@{})", @@ -213,7 +213,7 @@ impl PinnedPackMeta { } else { &vec![] }; - let mut checked_providers: HashSet = HashSet::new(); + let mut checked_providers: BTreeSet = BTreeSet::new(); for mod_provider in mod_providers .iter() .chain(pack_metadata.default_providers.iter()) @@ -305,8 +305,8 @@ impl PinnedPackMeta { ) } - fn get_dependent_mods(&self, mod_name: &str) -> HashSet { - let mut dependent_mods = HashSet::new(); + fn get_dependent_mods(&self, mod_name: &str) -> BTreeSet { + let mut dependent_mods = BTreeSet::new(); for (pinned_mod_name, pinned_mod) in self.mods.iter() { if let Some(deps) = &pinned_mod.deps { @@ -356,7 +356,7 @@ impl PinnedPackMeta { /// Remove all mods from lockfile that aren't in the pack metadata or depended on by another mod fn prune_mods(&mut self, pack_metadata: &ModpackMeta) -> Result<()> { - let mods_to_remove: HashSet = self + let mods_to_remove: BTreeSet = self .mods .keys() .filter(|mod_name| {