From dcd0d53077982d3fc886115c7c8dc873a7bcca49 Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:46:55 -0500 Subject: [PATCH 1/6] completed #5 --- Cargo.lock | 61 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/api_calls.rs | 9 +++---- src/config.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 27 ++++++++----------- 5 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 5e64c26..ce6a765 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,8 +8,10 @@ version = "0.1.0" dependencies = [ "futures-util", "reqwest", + "serde", "serde_json", "tokio", + "toml", "zip", ] @@ -1278,6 +1280,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1536,6 +1547,47 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "tower" version = "0.5.2" @@ -1940,6 +1992,15 @@ version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" +[[package]] +name = "winnow" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec" +dependencies = [ + "memchr", +] + [[package]] name = "wit-bindgen-rt" version = "0.33.0" diff --git a/Cargo.toml b/Cargo.toml index 8384b0d..00e011e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2024" [dependencies] futures-util = "0.3.31" reqwest = { version = "0.12.14", features = ["stream"] } +serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.140" tokio = { version = "1.44.0", features = ["full"] } zip = "2.2.3" +toml = "0.8.23" diff --git a/src/api_calls.rs b/src/api_calls.rs index adb807e..41905ed 100644 --- a/src/api_calls.rs +++ b/src/api_calls.rs @@ -4,7 +4,8 @@ use serde_json::Value; pub async fn get_api_search_result( client: Client, fabricmod_id: String, - config: Value, + loader_version: String, + server_version: String, ) -> Result> { let search_result = client .get("https://api.modrinth.com/v2/search") @@ -12,11 +13,7 @@ pub async fn get_api_search_result( ("query", fabricmod_id), ( "facets", - format!( - "[[\"categories:{}\"],[\"versions:{}\"]]", - config["loader_version"].as_str().unwrap(), - config["server_version"].as_str().unwrap() - ), + format!("[[\"categories:{}\"],[\"versions:{}\"]]", loader_version, server_version), ), ]) .send() diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..ea0dc38 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,67 @@ +use std::{ + fs::{self, File}, + io::{self, Read, Write}, + path::{Path, PathBuf}, +}; +use serde::{Deserialize, Serialize}; + +/// A response to the client from the server +#[derive(Deserialize, Serialize, Debug)] +#[serde(default)] +pub struct Config { + /// Maximum filesize in bytes + pub target_path: PathBuf, + + /// Maximum filesize in bytes + pub server_version: String, + + /// Is overwiting already uploaded files with the same hash allowed, or is + + pub loader_version: String, + + #[serde(skip)] + path: PathBuf, +} + +impl Default for Config { + fn default() -> Self { + Self { + target_path: Path::new(".").into(), + server_version: String::new(), + loader_version: String::new(), + path: "./settings.toml".into(), + } + } +} + +impl Config { + pub fn open>(path: &P) -> Result { + let mut input_str = String::new(); + if !path.as_ref().exists() { + let new_self = Self { + path: path.as_ref().to_path_buf(), + ..Default::default() + }; + new_self.save()?; + return Ok(new_self); + } else { + File::open(path).unwrap().read_to_string(&mut input_str)?; + } + + let mut parsed_config: Self = toml::from_str(&input_str).unwrap(); + parsed_config.path = path.as_ref().to_path_buf(); + + Ok(parsed_config) + } + + pub fn save(&self) -> Result<(), io::Error> { + let out_path = &self.path.with_extension("new"); + let mut file = File::create(out_path)?; + file.write_all(&toml::to_string_pretty(self).unwrap().into_bytes())?; + + // Overwrite the original DB with + fs::rename(out_path, &self.path).unwrap(); + + Ok(()) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8caa052..c12a671 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod api_calls; +mod config; use crate::api_calls::{get_api_project_result, get_api_search_result, get_api_version_result}; use futures_util::StreamExt; @@ -7,19 +8,13 @@ use serde_json::Value; use std::fs::{File, read_dir}; use std::io::{Read, Write}; use std::path::Path; +use crate::config::Config; #[tokio::main] async fn main() { - //TODO Change this to a TOML file instead of using JSON. - //********************************************************************** - let mut file = File::open("./src/default.json").unwrap(); - let mut data = String::new(); - file.read_to_string(&mut data).unwrap(); - - let config: Value = serde_json::from_str(&data).unwrap(); - let folder_path = config["target_path"].as_str().unwrap(); - let folder = read_dir(folder_path).unwrap(); - //********************************************************************** + + let config = Config::open(&"./settings.toml").expect("Could not open settings file"); + let folder = read_dir(config.target_path.clone()).unwrap(); for file in folder { let jar_path = file.unwrap().path(); @@ -31,11 +26,11 @@ async fn main() { let client = Client::new(); let project_id = - match get_api_search_result(client.clone(), fabricmod_id, config.clone()).await { + match get_api_search_result(client.clone(), fabricmod_id, config.loader_version.clone(), config.server_version.clone()).await { Ok(search_result) => { if is_compatable( - config["loader_version"].clone(), - config["server_version"].clone(), + Value::String(config.loader_version.clone()), + Value::String(config.server_version.clone()), search_result["hits"][0]["versions"].as_array().unwrap(), None, ) { @@ -71,8 +66,8 @@ async fn main() { { Ok(version_result) => { if is_compatable( - config["loader_version"].clone(), - config["server_version"].clone(), + Value::String(config.loader_version.clone()), + Value::String(config.server_version.clone()), version_result["game_versions"].as_array().unwrap(), Some(version_result["loaders"].as_array().unwrap()), ) { @@ -101,7 +96,7 @@ async fn main() { None => continue, }; - let download_path = format!("./{}{}", folder_path, filename); + let download_path = format!("./{}/{}", config.target_path.clone().display(), filename); match download_files(download_url, &download_path).await { Ok(_) => println!("file downloaded successfully!"), From 016b4fedf50fcda12274c674e747f83dc839ffdf Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:50:21 -0500 Subject: [PATCH 2/6] updated workflow --- .github/workflows/rust.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 01bd5b8..43f6757 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -2,7 +2,6 @@ name: Rust on: push: - branches: [ "master" ] pull_request: branches: [ "master" ] From 2fe6c99c9089f6ef08f1feff990aca0fa5484153 Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:55:50 -0500 Subject: [PATCH 3/6] cleaned to remove old config --- .gitignore | 1 - src/default.json | 5 ----- 2 files changed, 6 deletions(-) delete mode 100644 src/default.json diff --git a/.gitignore b/.gitignore index a6b9c87..5c2b500 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ shell.nix .direnv/ .envrc -src/default.json testing_jars/ diff --git a/src/default.json b/src/default.json deleted file mode 100644 index 74adde4..0000000 --- a/src/default.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "target_path": "path/to/jar.jar", - "server_version": "version", - "loader_version": "loader" -} \ No newline at end of file From b37b442abf9c0a39f6b2c7f51173e538eb4f3ba0 Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:56:37 -0500 Subject: [PATCH 4/6] made clippy happy --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c12a671..2ec20d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use futures_util::StreamExt; use reqwest::{Client, get}; use serde_json::Value; use std::fs::{File, read_dir}; -use std::io::{Read, Write}; +use std::io::Write; use std::path::Path; use crate::config::Config; From 1de7c3283984cda48e895f6558ba9f7fad4cd805 Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Fri, 27 Jun 2025 23:57:22 -0500 Subject: [PATCH 5/6] ran formatter --- src/api_calls.rs | 5 ++++- src/config.rs | 5 ++--- src/main.rs | 45 +++++++++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/api_calls.rs b/src/api_calls.rs index 41905ed..13ece2d 100644 --- a/src/api_calls.rs +++ b/src/api_calls.rs @@ -13,7 +13,10 @@ pub async fn get_api_search_result( ("query", fabricmod_id), ( "facets", - format!("[[\"categories:{}\"],[\"versions:{}\"]]", loader_version, server_version), + format!( + "[[\"categories:{}\"],[\"versions:{}\"]]", + loader_version, server_version + ), ), ]) .send() diff --git a/src/config.rs b/src/config.rs index ea0dc38..c7e43fa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,9 @@ +use serde::{Deserialize, Serialize}; use std::{ fs::{self, File}, io::{self, Read, Write}, path::{Path, PathBuf}, }; -use serde::{Deserialize, Serialize}; /// A response to the client from the server #[derive(Deserialize, Serialize, Debug)] @@ -16,7 +16,6 @@ pub struct Config { pub server_version: String, /// Is overwiting already uploaded files with the same hash allowed, or is - pub loader_version: String, #[serde(skip)] @@ -64,4 +63,4 @@ impl Config { Ok(()) } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 2ec20d4..ee12796 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,17 +2,16 @@ mod api_calls; mod config; use crate::api_calls::{get_api_project_result, get_api_search_result, get_api_version_result}; +use crate::config::Config; use futures_util::StreamExt; use reqwest::{Client, get}; use serde_json::Value; use std::fs::{File, read_dir}; use std::io::Write; use std::path::Path; -use crate::config::Config; #[tokio::main] async fn main() { - let config = Config::open(&"./settings.toml").expect("Could not open settings file"); let folder = read_dir(config.target_path.clone()).unwrap(); @@ -25,25 +24,31 @@ async fn main() { let client = Client::new(); - let project_id = - match get_api_search_result(client.clone(), fabricmod_id, config.loader_version.clone(), config.server_version.clone()).await { - Ok(search_result) => { - if is_compatable( - Value::String(config.loader_version.clone()), - Value::String(config.server_version.clone()), - search_result["hits"][0]["versions"].as_array().unwrap(), - None, - ) { - search_result["hits"][0]["project_id"] - .as_str() - .unwrap() - .to_string() - } else { - continue; //ToDo add information to console log - } + let project_id = match get_api_search_result( + client.clone(), + fabricmod_id, + config.loader_version.clone(), + config.server_version.clone(), + ) + .await + { + Ok(search_result) => { + if is_compatable( + Value::String(config.loader_version.clone()), + Value::String(config.server_version.clone()), + search_result["hits"][0]["versions"].as_array().unwrap(), + None, + ) { + search_result["hits"][0]["project_id"] + .as_str() + .unwrap() + .to_string() + } else { + continue; //ToDo add information to console log } - Err(_) => continue, - }; + } + Err(_) => continue, + }; let mut version_id_array = match get_api_project_result(client.clone(), project_id.as_str().to_string()).await { From fa112d4c6d8b4a9177f064cc0b9af4bc73850a0c Mon Sep 17 00:00:00 2001 From: Skywalker8510 <91810623+Skywalker8510@users.noreply.github.com> Date: Sat, 28 Jun 2025 00:28:30 -0500 Subject: [PATCH 6/6] fixed comments --- src/config.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index c7e43fa..d110f03 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,17 +5,16 @@ use std::{ path::{Path, PathBuf}, }; -/// A response to the client from the server #[derive(Deserialize, Serialize, Debug)] #[serde(default)] pub struct Config { - /// Maximum filesize in bytes + /// Path to the folder containing the mod jar's pub target_path: PathBuf, - /// Maximum filesize in bytes + /// Minecraft Server version as a string pub server_version: String, - /// Is overwiting already uploaded files with the same hash allowed, or is + /// Mod Loader that is being used as a string pub loader_version: String, #[serde(skip)] @@ -59,7 +58,7 @@ impl Config { file.write_all(&toml::to_string_pretty(self).unwrap().into_bytes())?; // Overwrite the original DB with - fs::rename(out_path, &self.path).unwrap(); + fs::rename(out_path, &self.path)?; Ok(()) }