Switch all HashSets to BTreeSets

This commit is contained in:
Warren Hood 2024-09-01 12:57:58 +02:00
parent 6aa9cff368
commit 6239bb9fd8
5 changed files with 30 additions and 17 deletions

View file

@ -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<ModLoader>,
pub download_url: Option<String>,
pub server_side: Option<bool>,
pub client_side: Option<bool>
pub client_side: Option<bool>,
}
impl PartialEq for ModMeta {
@ -46,6 +46,18 @@ impl PartialEq for ModMeta {
}
}
impl PartialOrd for ModMeta {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
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 {

View file

@ -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<String, ModMeta>,
pub default_providers: Vec<ModProvider>,
pub forbidden_mods: HashSet<String>,
pub forbidden_mods: BTreeSet<String>,
}
impl ModpackMeta {

View file

@ -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<HashSet<ModMeta>>,
// pub deps: Option<BTreeSet<ModMeta>>,
pub deps: Option<BTreeSet<ModMeta>>,
/// Server side
pub server_side: bool,
/// Required on client side

View file

@ -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(

View file

@ -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<OsString>,
cache: &mut BTreeSet<OsString>,
) -> 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<ModProvider> = HashSet::new();
let mut checked_providers: BTreeSet<ModProvider> = 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<String> {
let mut dependent_mods = HashSet::new();
fn get_dependent_mods(&self, mod_name: &str) -> BTreeSet<String> {
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<String> = self
let mods_to_remove: BTreeSet<String> = self
.mods
.keys()
.filter(|mod_name| {