Skip to content

Commit

Permalink
refactor: move filter_modrinth_versions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Dec 23, 2023
1 parent 1602286 commit 7df3212
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
33 changes: 1 addition & 32 deletions src/model/servertoml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use std::{
use anyhow::{anyhow, bail, Result};
use serde::{Deserialize, Serialize};

use crate::sources::modrinth;

use super::{ClientSideMod, Downloadable, ServerLauncher, ServerType, SoftwareType, World};
use super::{ClientSideMod, Downloadable, ServerLauncher, ServerType, World};

#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
#[serde(default)]
Expand Down Expand Up @@ -97,7 +95,6 @@ impl Server {
Ok(())
}

#[allow(dead_code)]
pub fn format(&self, str: &str) -> String {
mcapi::dollar_repl(str, |key| match key {
"mcver" | "mcversion" | "SERVER_VERSION" => Some(self.mc_version.clone()),
Expand Down Expand Up @@ -151,34 +148,6 @@ impl Server {

map
}

// TODO: move to ModrinthAPI
pub fn filter_modrinth_versions(
&self,
list: &[modrinth::ModrinthVersion],
) -> Vec<modrinth::ModrinthVersion> {
let is_proxy = self.jar.get_software_type() == SoftwareType::Proxy;
let is_vanilla = matches!(self.jar, ServerType::Vanilla {});

let mcver = &self.mc_version;
let loader = self.jar.get_modrinth_name();

list.iter()
.filter(|v| is_proxy || v.game_versions.contains(mcver))
.filter(|v| {
if let Some(n) = &loader {
v.loaders
.iter()
.any(|l| l == "datapack" || l == n || (l == "fabric" && n == "quilt"))
} else if is_vanilla {
v.loaders.contains(&"datapack".to_owned())
} else {
true
}
})
.cloned()
.collect()
}
}

impl Default for Server {
Expand Down
33 changes: 30 additions & 3 deletions src/sources/modrinth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::time::sleep;

use crate::{
app::{App, CacheStrategy, ResolvedFile},
model::SoftwareType,
model::{SoftwareType, ServerType},
};

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -162,12 +162,12 @@ impl<'a> ModrinthAPI<'a> {
pub async fn fetch_versions(&self, id: &str) -> Result<Vec<ModrinthVersion>> {
let versions = self.fetch_all_versions(id).await?;

Ok(self.0.server.filter_modrinth_versions(&versions))
Ok(self.filter_versions(&versions))
}

pub async fn fetch_version(&self, id: &str, version: &str) -> Result<ModrinthVersion> {
let all_versions = self.fetch_all_versions(id).await?;
let versions = self.0.server.filter_modrinth_versions(&all_versions);
let versions = self.filter_versions(&all_versions);

let ver = version.replace("${mcver}", &self.0.mc_version());
let ver = ver.replace("${mcversion}", &self.0.mc_version());
Expand Down Expand Up @@ -243,6 +243,33 @@ impl<'a> ModrinthAPI<'a> {
serde_json::to_string(&arr).unwrap()
}

pub fn filter_versions(
&self,
list: &[ModrinthVersion],
) -> Vec<ModrinthVersion> {
let is_proxy = self.0.server.jar.get_software_type() == SoftwareType::Proxy;
let is_vanilla = matches!(self.0.server.jar, ServerType::Vanilla {});

let mcver = &self.0.mc_version();
let loader = self.0.server.jar.get_modrinth_name();

list.iter()
.filter(|v| is_proxy || v.game_versions.contains(mcver))
.filter(|v| {
if let Some(n) = &loader {
v.loaders
.iter()
.any(|l| l == "datapack" || l == n || (l == "fabric" && n == "quilt"))
} else if is_vanilla {
v.loaders.contains(&"datapack".to_owned())
} else {
true
}
})
.cloned()
.collect()
}

pub async fn search(&self, query: &str) -> Result<Vec<ModrinthProject>> {
Ok(self
.0
Expand Down

0 comments on commit 7df3212

Please sign in to comment.