Skip to content

Commit

Permalink
refactor: several refactors to make clippy not as annoying
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Sep 23, 2024
1 parent c17c43a commit 2f8dbf0
Show file tree
Hide file tree
Showing 33 changed files with 171 additions and 196 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mcman"
version = "0.5.0"
edition = "2021"
rust-version = "1.75"
rust-version = "1.80"
repository = "https://github.com/ParadigmMC/mcman"
homepage = "https://paradigmmc.github.io/mcman/"
authors = ["ParadigmMC"]
Expand Down
3 changes: 1 addition & 2 deletions src/_archive/model/downloadable/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Downloadable {
}
}

pub fn get_type_name(&self) -> String {
pub const fn get_type_name(&self) -> &'static str {
match self {
Self::Url { .. } => "URL",
Self::GithubRelease { .. } => "GithubRel",
Expand All @@ -50,7 +50,6 @@ impl Downloadable {
Self::Spigot { .. } => "Spigot",
Self::Maven { .. } => "Maven",
}
.to_owned()
}

pub fn fields_to_map(&self) -> IndexMap<Cow<'static, str>, String> {
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/actions/markdown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl App {

map.insert(
String::from("addons"),
markdown_options.render_addons(metadata.addons),
markdown_options.render_addons(&metadata.addons),
);

Ok(map)
Expand All @@ -59,7 +59,7 @@ impl App {
pub async fn render_addon_metadata(&self, metadata: Vec<AddonMetadata>) -> Result<String> {
let markdown_options = self.get_markdown_options().await.unwrap_or_default();

let table = markdown_options.table_addons(metadata, markdown_options.output_type);
let table = markdown_options.table_addons(&metadata, markdown_options.output_type);

Ok(table.render(markdown_options.output_type))
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/actions/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl App {
server: &Server,
base: &Path,
) -> Result<()> {
let script = shell.generate_script(self.get_script_lines_for(&shell, server).await?);
let script = shell.generate_script(&self.get_script_lines_for(&shell, server).await?);

tokio::fs::write(base.join(format!("start.{}", shell.file_ext())), script).await?;

Expand Down
9 changes: 2 additions & 7 deletions src/api/app/step/execute_java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ impl App {

let mut proc = JavaProcess::new(&dir.canonicalize()?, &java.path, args)?;

fn on_line(line: &str) {
println!("| {line}");
}

proc.lines(on_line);
proc.lines(|line| println!("| {line}"));

let res = proc.wait().await?;

if !res.success() {
bail!(
"Java process exited with code {}",
res.code()
.map_or("unknown".to_owned(), |x| x.to_string())
res.code().map_or("unknown".to_owned(), |x| x.to_string())
);
}

Expand Down
29 changes: 16 additions & 13 deletions src/api/models/addon/addon_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

use super::Addon;

Check warning on line 5 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `super::Addon`

warning: unused import: `super::Addon` --> src/api/models/addon/addon_type.rs:5:5 | 5 | use super::Addon; | ^^^^^^^^^^^^

#[derive(Debug, Deserialize, Serialize, Clone, Hash, PartialEq, Eq, JsonSchema)]
#[serde(tag = "type", rename_all = "lowercase")]
Expand Down Expand Up @@ -58,32 +61,32 @@ pub enum AddonType {
},
}

impl ToString for AddonType {
fn to_string(&self) -> String {
impl fmt::Display for AddonType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AddonType::Url { url } => format!("Url [{url}]"),
AddonType::Modrinth { id, version } => format!("Modrinth/{id} [{version}]"),
AddonType::Curseforge { id, version } => format!("Curseforge/{id} [{version}]"),
AddonType::Spigot { id, version } => format!("Spigot/{id} [{version}]"),
AddonType::Hangar { id, version } => format!("Hangar/{id} [{version}]"),
AddonType::GithubRelease {
Self::Url { url } => write!(f, "Url [{url}]"),
Self::Modrinth { id, version } => write!(f, "Modrinth/{id} [{version}]"),
Self::Curseforge { id, version } => write!(f, "Curseforge/{id} [{version}]"),
Self::Spigot { id, version } => write!(f, "Spigot/{id} [{version}]"),
Self::Hangar { id, version } => write!(f, "Hangar/{id} [{version}]"),
Self::GithubRelease {
repo,
version,
filename,
} => format!("Github/{repo} [{version}; {filename}]"),
AddonType::Jenkins {
} => write!(f, "Github/{repo} [{version}; {filename}]"),
Self::Jenkins {
url,

Check warning on line 78 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `url`

warning: unused variable: `url` --> src/api/models/addon/addon_type.rs:78:17 | 78 | url, | ^^^ help: try ignoring the field: `url: _`
job,
build,
artifact,
} => format!("Jenkins/{job} [{build}; {artifact}]"),
AddonType::MavenArtifact {
} => write!(f, "Jenkins/{job} [{build}; {artifact}]"),
Self::MavenArtifact {
url,

Check warning on line 84 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `url`

warning: unused variable: `url` --> src/api/models/addon/addon_type.rs:84:17 | 84 | url, | ^^^ help: try ignoring the field: `url: _`
group,
artifact,
version,
filename,
} => format!("Maven/{group}.{artifact} [{version}; {filename}]"),
} => write!(f, "Maven/{group}.{artifact} [{version}; {filename}]"),
}
}
}
19 changes: 10 additions & 9 deletions src/api/models/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::fmt;

use super::mrpack::{Env, EnvSupport};

Expand All @@ -13,23 +14,23 @@ pub enum Environment {
Client,
}

impl ToString for Environment {
fn to_string(&self) -> String {
impl fmt::Display for Environment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Environment::Both => String::from("both"),
Environment::Server => String::from("server"),
Environment::Client => String::from("client"),
Self::Both => write!(f, "both"),
Self::Server => write!(f, "server"),
Self::Client => write!(f, "client"),
}
}
}

impl Environment {
pub fn server(&self) -> bool {
matches!(&self, Environment::Server | Environment::Both)
pub const fn server(self) -> bool {
matches!(self, Self::Server | Self::Both)
}

pub fn client(&self) -> bool {
matches!(&self, Environment::Client | Environment::Both)
pub const fn client(self) -> bool {
matches!(self, Self::Client | Self::Both)
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/api/models/lockfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ use super::Addon;

pub const LOCKFILE: &str = ".mcman.lock";

#[derive(Debug, Serialize, Deserialize)]
#[derive(Default)]
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Lockfile {
pub vars: HashMap<String, String>,
pub addons: Vec<Addon>,
pub bootstrapped_files: HashMap<PathBuf, BootstrappedFile>,
}


pub enum LockfileMessage {

Check warning on line 18 in src/api/models/lockfile/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

enum `LockfileMessage` is never used

warning: enum `LockfileMessage` is never used --> src/api/models/lockfile/mod.rs:18:10 | 18 | pub enum LockfileMessage { | ^^^^^^^^^^^^^^^
BootstrapFile(PathBuf, BootstrappedFile),
}
16 changes: 8 additions & 8 deletions src/api/models/markdown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -61,14 +61,14 @@ pub enum MdColumn {
Link,
}

impl ToString for MdColumn {
fn to_string(&self) -> String {
impl fmt::Display for MdColumn {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MdColumn::Icon => String::from("."),
MdColumn::Name => String::from("Name"),
MdColumn::Description => String::from("Description"),
MdColumn::Version => String::from("Version"),
MdColumn::Link => String::from("Link"),
Self::Icon => write!(f, "."),
Self::Name => write!(f, "Name"),
Self::Description => write!(f, "Description"),
Self::Version => write!(f, "Version"),
Self::Link => write!(f, "Link"),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/models/markdown/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::api::{
use super::{MarkdownOptions, MarkdownOutput, MdColumn};

impl MarkdownOptions {
pub fn render_addons(&self, list: Vec<AddonMetadata>) -> String {
self.table_addons(list, self.output_type)
pub fn render_addons(&self, list: &[AddonMetadata]) -> String {
self.table_addons(&list, self.output_type)

Check failure on line 10 in src/api/models/markdown/render.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/api/models/markdown/render.rs:10:27 | 10 | self.table_addons(&list, self.output_type) | ^^^^^ help: change this to: `list` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_borrow)]`
.render(self.output_type)
}

pub fn table_addons(&self, list: Vec<AddonMetadata>, output: MarkdownOutput) -> MarkdownTable {
pub fn table_addons(&self, list: &[AddonMetadata], output: MarkdownOutput) -> MarkdownTable {
let mut table = MarkdownTable::new();

for column in &self.columns {
Expand All @@ -24,7 +24,7 @@ impl MarkdownOptions {
));
}

for meta in &list {
for meta in list {
let mut row = vec![];

for column in &self.columns {
Expand Down
29 changes: 13 additions & 16 deletions src/api/models/metadata/addon_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ impl AddonMetadataSource {
.join("\n")
}

pub fn into_str(&self) -> &'static str {
pub fn into_str(self) -> &'static str {
match self {
AddonMetadataSource::Modrinth => "modrinth",
AddonMetadataSource::Hangar => "hangar",
AddonMetadataSource::Spigot => "spigot",
AddonMetadataSource::Other => "other",
AddonMetadataSource::Github => "github",
AddonMetadataSource::Jenkins => "jenkins",
AddonMetadataSource::Maven => "maven",
AddonMetadataSource::Curseforge => "curseforge",
Self::Modrinth => "modrinth",
Self::Hangar => "hangar",
Self::Spigot => "spigot",
Self::Other => "other",
Self::Github => "github",
Self::Jenkins => "jenkins",
Self::Maven => "maven",
Self::Curseforge => "curseforge",
}
}

pub fn markdown_tag(&self) -> String {
pub fn markdown_tag(self) -> String {
format!("![{}]", self.into_str())
}

pub fn html(&self) -> String {
pub fn html(self) -> String {
format!("<img src='{}'>", self.icon_url())
}

pub fn icon_url(&self) -> String {
pub fn icon_url(self) -> String {
// TODO !!!!!!!!!!!!!!!!!!!!!
format!(
"https://raw.githubusercontent.com/ParadigmMC/mcman/v2/res/icons/{}.png",
Expand Down Expand Up @@ -130,10 +130,7 @@ impl Addon {

Ok(AddonMetadata {
name: proj.name,
link: Some(format!(
"https://hangar.papermc.io/{}",
proj.namespace.to_string()
)),
link: Some(format!("https://hangar.papermc.io/{}", proj.namespace)),
description: Some(proj.description),
source: AddonMetadataSource::Hangar,
version: Some(version.to_owned()),
Expand Down
11 changes: 6 additions & 5 deletions src/api/models/modpack_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
Expand All @@ -9,12 +10,12 @@ pub enum ModpackType {
Unsup,
}

impl ToString for ModpackType {
fn to_string(&self) -> String {
impl fmt::Display for ModpackType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ModpackType::Packwiz => String::from("Packwiz"),
ModpackType::MRPack => String::from("MRPack"),
ModpackType::Unsup => String::from("Unsup"),
Self::Packwiz => write!(f, "Packwiz"),
Self::MRPack => write!(f, "MRPack"),
Self::Unsup => write!(f, "Unsup"),
}
}
}
2 changes: 1 addition & 1 deletion src/api/models/packwiz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl PackwizModUpdate {
}

impl PackwizModDownload {
pub async fn from_steps(steps: &Vec<Step>) -> Self {
pub async fn from_steps(steps: &[Step]) -> Self {

Check warning on line 57 in src/api/models/packwiz/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

associated function `from_steps` is never used

warning: associated function `from_steps` is never used --> src/api/models/packwiz/mod.rs:57:18 | 56 | impl PackwizModDownload { | ----------------------- associated function in this implementation 57 | pub async fn from_steps(steps: &[Step]) -> Self { | ^^^^^^^^^^

Check warning on line 57 in src/api/models/packwiz/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `steps`

warning: unused variable: `steps` --> src/api/models/packwiz/mod.rs:57:29 | 57 | pub async fn from_steps(steps: &[Step]) -> Self { | ^^^^^ help: if this is intentional, prefix it with an underscore: `_steps`
todo!()
}

Check warning on line 59 in src/api/models/packwiz/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `async` for function with no await statements

warning: unused `async` for function with no await statements --> src/api/models/packwiz/mod.rs:57:5 | 57 | / pub async fn from_steps(steps: &[Step]) -> Self { 58 | | todo!() 59 | | } | |_____^ | = help: consider removing the `async` from this function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
}
Expand Down
4 changes: 3 additions & 1 deletion src/api/models/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ impl Server {
if let Some(v) = self.launcher.java_version {
get_java_installation_for(v)
.await
.map_or(String::from("java"), |j| j.path.to_string_lossy().into_owned())
.map_or(String::from("java"), |j| {
j.path.to_string_lossy().into_owned()
})
} else {
String::from("java")
}
Expand Down
37 changes: 10 additions & 27 deletions src/api/models/server/server_flavor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,23 @@ pub enum ServerFlavor {
}

impl ServerFlavor {
pub fn supports_datapacks(&self) -> bool {
match self {
ServerFlavor::Proxy => false,
_ => true,
}
pub const fn supports_datapacks(self) -> bool {
!matches!(self, Self::Proxy)
}

pub fn supports_mods(&self) -> bool {
match self {
ServerFlavor::Modded => true,
_ => false,
}
pub const fn supports_mods(self) -> bool {
matches!(self, Self::Modded)
}

pub fn supports_plugins(&self) -> bool {
match self {
ServerFlavor::Vanilla => false,
ServerFlavor::Modded => false,
ServerFlavor::Patched => true,
ServerFlavor::Proxy => true,
}
pub const fn supports_plugins(self) -> bool {
matches!(self, Self::Patched | Self::Proxy)
}

pub fn supports_nogui(&self) -> bool {
match self {
ServerFlavor::Proxy => false,
_ => true,
}
pub const fn supports_nogui(self) -> bool {
!matches!(self, Self::Proxy)
}

pub fn supports_eula_args(&self) -> bool {
match self {
ServerFlavor::Patched => true,
_ => false,
}
pub const fn supports_eula_args(self) -> bool {
matches!(self, Self::Patched)
}
}
Loading

0 comments on commit 2f8dbf0

Please sign in to comment.