From 382522a158220f2320c0d17c156016888e74e5f1 Mon Sep 17 00:00:00 2001 From: TheAlan404 Date: Sun, 6 Aug 2023 18:18:56 +0300 Subject: [PATCH] clippy && fmt --- README.md | 3 +- src/core/runner.rs | 4 +- src/core/serverjar.rs | 12 ++++-- src/model/downloadable/markdown.rs | 19 ++++++++-- src/model/downloadable/mod.rs | 58 ++++++++++++++++++----------- src/model/servertype/interactive.rs | 8 +++- src/model/servertype/mod.rs | 11 +----- src/sources/forge.rs | 4 +- src/sources/github.rs | 20 ++++++---- src/sources/maven.rs | 38 ++++++++++--------- src/sources/mod.rs | 6 +-- src/sources/neoforge.rs | 2 +- src/util/maven_import.rs | 56 +++++++++++----------------- src/util/mod.rs | 4 +- 14 files changed, 134 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index 8e65ab1..72d18e2 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,12 @@ whats a semver? /s ### `0.3.1` (unreleased) -- Added [neoforge](https://neoforged.net/) server type +- Added [NeoForge](https://neoforged.net/) server type - Added [Forge](https://forums.minecraftforge.net/) server type - Added Downloadable type **Maven** - Improved building process - Fixed a bug on `mcman run` which messed up the output when server crashes +- Some downloadables now support variables ### `0.3.0` diff --git a/src/core/runner.rs b/src/core/runner.rs index 46c93d7..79d492a 100644 --- a/src/core/runner.rs +++ b/src/core/runner.rs @@ -110,7 +110,9 @@ impl BuildContext { } let exit_status = child.wait()?; - stdout_process.await.context("Awaiting stdout proxy printing thread")?; + stdout_process + .await + .context("Awaiting stdout proxy printing thread")?; if !exit_status.success() { println!(); diff --git a/src/core/serverjar.rs b/src/core/serverjar.rs index 9a6ddea..1f4cf1c 100644 --- a/src/core/serverjar.rs +++ b/src/core/serverjar.rs @@ -58,7 +58,8 @@ impl BuildContext { jar_name.clone() } else { "".to_owned() - }).dim() + }) + .dim() ); } else { println!( @@ -67,7 +68,8 @@ impl BuildContext { jar_name.clone() } else { "".to_owned() - }).dim() + }) + .dim() ); let mut cmd_args = vec!["-jar", &installer_jar]; @@ -115,7 +117,11 @@ impl BuildContext { } }?; - self.startup_method = self.server.jar.get_startup_method(&self.http_client, &serverjar_name, &self.server.mc_version).await?; + self.startup_method = self + .server + .jar + .get_startup_method(&self.http_client, &serverjar_name, &self.server.mc_version) + .await?; Ok(()) } diff --git a/src/model/downloadable/markdown.rs b/src/model/downloadable/markdown.rs index 2089917..c368aa5 100644 --- a/src/model/downloadable/markdown.rs +++ b/src/model/downloadable/markdown.rs @@ -109,7 +109,7 @@ impl Downloadable { Self::Maven { version, .. } => { map.insert("Name".to_owned(), self.get_md_link()); - map.insert("Version".to_owned(), version.to_owned()); + map.insert("Version".to_owned(), version.clone()); } Self::Url { @@ -190,8 +190,17 @@ impl Downloadable { map.insert("Asset/File".to_owned(), artifact.clone()); } - Self::Maven { url, group, artifact, version, filename } => { - map.insert("Project/URL".to_owned(), format!("{group}.{artifact} - ({url})")); + Self::Maven { + url, + group, + artifact, + version, + filename, + } => { + map.insert( + "Project/URL".to_owned(), + format!("{group}.{artifact} - ({url})"), + ); map.insert("Version/Release".to_owned(), version.clone()); map.insert("Asset/File".to_owned(), filename.clone()); } @@ -214,7 +223,9 @@ impl Downloadable { "URL".to_string() } } - Self::Maven { group, artifact, .. } => { + Self::Maven { + group, artifact, .. + } => { format!("Maven/{group}.{artifact}") } } diff --git a/src/model/downloadable/mod.rs b/src/model/downloadable/mod.rs index ba43af3..049a5f3 100644 --- a/src/model/downloadable/mod.rs +++ b/src/model/downloadable/mod.rs @@ -2,14 +2,14 @@ use anyhow::Result; use async_trait::async_trait; use serde::{Deserialize, Serialize}; -use crate::sources::maven::{get_maven_url, self}; +use crate::sources::maven::{self, get_maven_url}; use crate::{model::Server, Source}; use crate::sources::{ - curserinth::{ fetch_curserinth_filename, get_curserinth_url}, + curserinth::{fetch_curserinth_filename, get_curserinth_url}, github::{download_github_release, fetch_github_release_filename, get_github_release_url}, - jenkins::{ get_jenkins_download_url, get_jenkins_filename}, - modrinth::{ fetch_modrinth_filename, get_modrinth_url}, + jenkins::{get_jenkins_download_url, get_jenkins_filename}, + modrinth::{fetch_modrinth_filename, get_modrinth_url}, spigot::{fetch_spigot_resource_latest_ver, get_spigot_url}, }; mod import_url; @@ -112,9 +112,13 @@ impl Downloadable { artifact, } => Ok(get_jenkins_download_url(client, url, job, build, artifact).await?), - Self::Maven { url, group, artifact, version, filename } => { - Ok(get_maven_url(client, url, group, artifact, version, filename, mcver).await?) - } + Self::Maven { + url, + group, + artifact, + version, + filename, + } => Ok(get_maven_url(client, url, group, artifact, version, filename, mcver).await?), } } } @@ -128,16 +132,21 @@ impl Source for Downloadable { filename_hint: Option<&str>, ) -> Result { match self { - Self::GithubRelease { repo, tag, asset } => { - Ok(download_github_release(repo, tag, asset, &server.mc_version, client, filename_hint).await?) - } - - dl => { - Ok(client.get(dl.get_url(client, server, filename_hint).await?) - .send() - .await? - .error_for_status()?) - } + Self::GithubRelease { repo, tag, asset } => Ok(download_github_release( + repo, + tag, + asset, + &server.mc_version, + client, + filename_hint, + ) + .await?), + + dl => Ok(client + .get(dl.get_url(client, server, filename_hint).await?) + .send() + .await? + .error_for_status()?), } } @@ -182,10 +191,17 @@ impl Source for Downloadable { } => Ok(get_jenkins_filename(client, url, job, build, artifact) .await? .1), - - Self::Maven { url, group, artifact, version, filename } => { - Ok(maven::get_maven_filename(client, url, group, artifact, version, filename, mcver).await?) - } + + Self::Maven { + url, + group, + artifact, + version, + filename, + } => Ok(maven::get_maven_filename( + client, url, group, artifact, version, filename, mcver, + ) + .await?), } } } diff --git a/src/model/servertype/interactive.rs b/src/model/servertype/interactive.rs index 5f08af4..d5d771f 100644 --- a/src/model/servertype/interactive.rs +++ b/src/model/servertype/interactive.rs @@ -81,8 +81,12 @@ impl ServerType { loader: "latest".to_owned(), installer: "latest".to_owned(), }, - 2 => Self::NeoForge { loader: "latest".to_owned() }, - 3 => Self::Forge { loader: "latest".to_owned() }, + 2 => Self::NeoForge { + loader: "latest".to_owned(), + }, + 3 => Self::Forge { + loader: "latest".to_owned(), + }, _ => unreachable!(), }) } diff --git a/src/model/servertype/mod.rs b/src/model/servertype/mod.rs index ec38135..91f6eb4 100644 --- a/src/model/servertype/mod.rs +++ b/src/model/servertype/mod.rs @@ -5,16 +5,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::Source; use crate::model::Downloadable; -use crate::sources::{ - fabric, - jenkins, - papermc, - purpur, - quilt, - vanilla, - neoforge, - forge, -}; +use crate::sources::{fabric, forge, jenkins, neoforge, papermc, purpur, quilt, vanilla}; use super::{Server, StartupMethod}; diff --git a/src/sources/forge.rs b/src/sources/forge.rs index 5000501..3ac553e 100644 --- a/src/sources/forge.rs +++ b/src/sources/forge.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result, Context}; +use anyhow::{anyhow, Context, Result}; use crate::util; @@ -30,7 +30,7 @@ pub async fn get_versions_for(mcver: &str, client: &reqwest::Client) -> Result Result { let loader_versions = get_versions_for(mcver, client).await?; - util::get_latest_semver(loader_versions).ok_or(anyhow!("No loader versions for {mcver}")) + util::get_latest_semver(&loader_versions).ok_or(anyhow!("No loader versions for {mcver}")) } pub async fn map_forge_version( diff --git a/src/sources/github.rs b/src/sources/github.rs index 41eb51b..dab44cc 100644 --- a/src/sources/github.rs +++ b/src/sources/github.rs @@ -69,7 +69,9 @@ pub async fn fetch_github_release_asset( "latest" => releases.first(), id => releases.iter().find(|r| r.tag_name == id), } - .ok_or(anyhow!("Github release with tag '{tag}' not found on repository '{repo}'"))?; + .ok_or(anyhow!( + "Github release with tag '{tag}' not found on repository '{repo}'" + ))?; let assets = &release.assets; @@ -86,16 +88,18 @@ pub async fn fetch_github_release_asset( id.to_owned() }; - assets.iter().find(|a| { - id == a.name - }).or(assets.iter().find(|a| { - a.name.contains(&id) - })) + assets + .iter() + .find(|a| id == a.name) + .or(assets.iter().find(|a| a.name.contains(&id))) } } - .ok_or(anyhow!("Github release asset with name '{asset}' on release '{}' not found", release.tag_name))?; + .ok_or(anyhow!( + "Github release asset with name '{asset}' on release '{}' not found", + release.tag_name + ))?; - Ok(resolved_asset.to_owned()) + Ok(resolved_asset.clone()) } pub async fn fetch_github_release_filename( diff --git a/src/sources/maven.rs b/src/sources/maven.rs index 3b36b8c..fb438e6 100644 --- a/src/sources/maven.rs +++ b/src/sources/maven.rs @@ -1,4 +1,4 @@ -use anyhow::{Result, anyhow}; +use anyhow::{anyhow, Result}; pub fn get_metadata_url(url: &str, group_id: &str, artifact_id: &str) -> String { format!( @@ -76,7 +76,8 @@ pub async fn get_maven_filename( ) -> Result { let version = match_maven_version(client, url, group_id, artifact_id, version, mcver).await?; - let file = file.replace("${artifact}", artifact_id) + let file = file + .replace("${artifact}", artifact_id) .replace("${version}", &version) .replace("${mcversion}", mcver) .replace("${mcver}", mcver); @@ -100,22 +101,23 @@ pub async fn match_maven_version( let version = match version { "latest" => fetch_versions().await?.0, - id => if id.contains("$") { - let versions = fetch_versions().await?.1; - let id = id.replace("${artifact}", artifact_id) - .replace("${mcversion}", mcver) - .replace("${mcver}", mcver); - versions.iter().find(|v| { - v.to_owned() == &id - }).or_else(|| { - versions.iter().find(|v| { - v.contains(&id) - }) - }).ok_or(anyhow!("Couldn't resolve maven artifact version"))? - .to_owned() - } else { - id.to_owned() - }, + id => { + if id.contains('$') { + let versions = fetch_versions().await?.1; + let id = id + .replace("${artifact}", artifact_id) + .replace("${mcversion}", mcver) + .replace("${mcver}", mcver); + versions + .iter() + .find(|v| *v == &id) + .or_else(|| versions.iter().find(|v| v.contains(&id))) + .ok_or(anyhow!("Couldn't resolve maven artifact version"))? + .clone() + } else { + id.to_owned() + } + } }; Ok(version) diff --git a/src/sources/mod.rs b/src/sources/mod.rs index f41f819..621960a 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -1,13 +1,13 @@ pub mod curserinth; pub mod fabric; +pub mod forge; pub mod github; pub mod jenkins; +pub mod maven; pub mod modrinth; +pub mod neoforge; pub mod papermc; pub mod purpur; pub mod quilt; pub mod spigot; pub mod vanilla; -pub mod maven; -pub mod neoforge; -pub mod forge; diff --git a/src/sources/neoforge.rs b/src/sources/neoforge.rs index 280ef39..134592f 100644 --- a/src/sources/neoforge.rs +++ b/src/sources/neoforge.rs @@ -33,7 +33,7 @@ pub async fn get_latest_version_for(mcver: &str, client: &reqwest::Client) -> Re .await .context("Fetching NeoForge versions")?; - util::get_latest_semver(loader_versions).ok_or(anyhow!("No loader versions for {mcver}")) + util::get_latest_semver(&loader_versions).ok_or(anyhow!("No loader versions for {mcver}")) } pub async fn map_neoforge_version( diff --git a/src/util/maven_import.rs b/src/util/maven_import.rs index 9b5554d..2eb7192 100644 --- a/src/util/maven_import.rs +++ b/src/util/maven_import.rs @@ -1,9 +1,7 @@ -use anyhow::{Result, anyhow, bail}; +use anyhow::{anyhow, bail, Result}; use crate::model::Downloadable; - - /// Example: /// ```xml /// @@ -13,29 +11,29 @@ use crate::model::Downloadable; /// /// ``` #[allow(unused)] -pub fn import_from_maven_dependency_xml( - url: &str, - xml: &str -) -> Result { +pub fn import_from_maven_dependency_xml(url: &str, xml: &str) -> Result { let xml = roxmltree::Document::parse(xml)?; - let group = xml.descendants().find(|t| { - t.tag_name().name() == "groupId" - }).ok_or(anyhow!("dependency.groupId must be present"))? + let group = xml + .descendants() + .find(|t| t.tag_name().name() == "groupId") + .ok_or(anyhow!("dependency.groupId must be present"))? .text() .ok_or(anyhow!("dependency.groupId must be text"))? .to_owned(); - let artifact = xml.descendants().find(|t| { - t.tag_name().name() == "artifactId" - }).ok_or(anyhow!("dependency.artifactId must be present"))? + let artifact = xml + .descendants() + .find(|t| t.tag_name().name() == "artifactId") + .ok_or(anyhow!("dependency.artifactId must be present"))? .text() .ok_or(anyhow!("dependency.artifactId must be text"))? .to_owned(); - let version = xml.descendants().find(|t| { - t.tag_name().name() == "version" - }).ok_or(anyhow!("dependency.version must be present"))? + let version = xml + .descendants() + .find(|t| t.tag_name().name() == "version") + .ok_or(anyhow!("dependency.version must be present"))? .text() .ok_or(anyhow!("dependency.version must be text"))? .to_owned(); @@ -53,23 +51,17 @@ pub fn import_from_maven_dependency_xml( /// ``` /// implementation("net.neoforged:forge:1.20.1-47.1.62") /// ``` -/// +/// /// Gradle Groovy: -/// +/// /// ``` /// implementation "net.neoforged:forge:1.20.1-47.1.62" /// ``` #[allow(unused)] -pub fn import_from_gradle_dependency( - url: &str, - imp: &str -) -> Result { +pub fn import_from_gradle_dependency(url: &str, imp: &str) -> Result { let imp = imp.replace("implementation", ""); - let imp = imp.replace(&[' ', '(', ')', '"'], ""); - let li = imp - .trim() - .split(':') - .collect::>(); + let imp = imp.replace([' ', '(', ')', '"'], ""); + let li = imp.trim().split(':').collect::>(); if li.len() != 3 { bail!("Gradle dependency should have 3 sections delimeted by ':' inside the quoted string"); @@ -89,16 +81,10 @@ pub fn import_from_gradle_dependency( /// "net.neoforged" %% "forge" %% "1.20.1-47.1.62" /// ``` #[allow(unused)] -pub fn import_from_sbt( - url: &str, - sbt: &str, -) -> Result { +pub fn import_from_sbt(url: &str, sbt: &str) -> Result { let sbt = sbt.replace(char::is_whitespace, ""); let sbt = sbt.replace('"', ""); - let li = sbt - .split("%") - .filter(|x| !x.is_empty()) - .collect::>(); + let li = sbt.split('%').filter(|x| !x.is_empty()).collect::>(); if li.len() != 3 { bail!("SBT should have 3 sections delimeted by '%' or '%%'"); diff --git a/src/util/mod.rs b/src/util/mod.rs index 0b4f561..14410a8 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -75,7 +75,7 @@ pub fn match_artifact_name(input: &str, artifact_name: &str) -> bool { artifact_name.contains(input) } -pub fn get_latest_semver(list: Vec) -> Option { +pub fn get_latest_semver(list: &[String]) -> Option { let mut list = list .iter() .filter_map(|s| semver::Version::parse(s).ok()) @@ -83,5 +83,5 @@ pub fn get_latest_semver(list: Vec) -> Option { list.sort_by(semver::Version::cmp); - list.last().map(|v| v.to_string()) + list.last().map(ToString::to_string) }