style: run cargo fmt

This commit is contained in:
RafayAhmad7548 2026-08-31 01:00:11 +05:00
parent f45493737a
commit 2f057425c3
Signed by: RafayAhmad
SSH key fingerprint: SHA256:WURX8viobA1uawb4dWM3LqYrY+XPcZcXhAXAlrYdhtE
3 changed files with 63 additions and 42 deletions

View file

@ -1,8 +1,11 @@
mod models; mod models;
mod utils; mod utils;
use crate::{models::{vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}}, utils::{with_progressbar, with_spinner}};
use crate::models::fabric::FabricVersions; use crate::models::fabric::FabricVersions;
use crate::{
models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions},
utils::{with_progressbar, with_spinner},
};
use clap::{ use clap::{
Parser, Subcommand, Parser, Subcommand,
builder::styling::{self}, builder::styling::{self},
@ -10,7 +13,7 @@ use clap::{
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme}; use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
use futures_util::StreamExt; use futures_util::StreamExt;
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use std::{error::Error, fmt::format}; use std::error::Error;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
fn styles() -> styling::Styles { fn styles() -> styling::Styles {
@ -55,7 +58,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
let res = with_spinner("", "", || async { let res = with_spinner("", "", || async {
let res: VanillaVersions = reqwest::get(URL).await?.json().await?; let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
Ok(res) Ok(res)
}).await?; })
.await?;
let release_versions = res let release_versions = res
.versions .versions
@ -73,32 +77,39 @@ async fn main() -> Result<(), Box<dyn Error>> {
.default(0) .default(0)
.interact()?; .interact()?;
let version_detail: VanillaVersionDetail = let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
with_spinner("", "", || async { let version_detail: VanillaVersionDetail =
let version_detail: VanillaVersionDetail = reqwest::get(release_versions[version_index].url.clone())
reqwest::get(release_versions[version_index].url.clone()) .await?
.await? .json()
.json() .await?;
.await?; Ok(version_detail)
Ok(version_detail) })
}).await?; .await?;
let start_msg = format!( let start_msg = format!(
"downloading server.jar for {} {}", "downloading server.jar for {} {}",
loaders[loader_index], release_version_names[version_index] loaders[loader_index], release_version_names[version_index]
); );
with_progressbar(&start_msg, "download finished", version_detail.downloads.server.size, |pb| async move { with_progressbar(
let server_bytes = reqwest::get(version_detail.downloads.server.url).await?; &start_msg,
let mut file = tokio::fs::File::create("server.jar").await?; "download finished",
let mut stream = server_bytes.bytes_stream(); version_detail.downloads.server.size,
|pb| async move {
let server_bytes =
reqwest::get(version_detail.downloads.server.url).await?;
let mut file = tokio::fs::File::create("server.jar").await?;
let mut stream = server_bytes.bytes_stream();
while let Some(chunk) = stream.next().await { while let Some(chunk) = stream.next().await {
let chunk = chunk?; let chunk = chunk?;
file.write_all(&chunk).await?; file.write_all(&chunk).await?;
pb.inc(chunk.len() as u64); pb.inc(chunk.len() as u64);
} }
Ok(()) Ok(())
}).await?; },
)
.await?;
with_spinner("verifying sha1 hash", "sha1 hash verified", || async { with_spinner("verifying sha1 hash", "sha1 hash verified", || async {
let data = tokio::fs::read("server.jar").await?; let data = tokio::fs::read("server.jar").await?;
@ -112,16 +123,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Err("sha1 verifaction failed for downloaded file".into()); return Err("sha1 verifaction failed for downloaded file".into());
} }
Ok(()) Ok(())
}).await?; })
.await?;
} }
1 => { 1 => {
const URL: &str = "https://meta.fabricmc.net/v2/versions"; const URL: &str = "https://meta.fabricmc.net/v2/versions";
let res = with_spinner("", "", || async { let res = with_spinner("", "", || async {
let res: FabricVersions = reqwest::get(URL).await?.json().await?; let res: FabricVersions = reqwest::get(URL).await?.json().await?;
Ok(res) Ok(res)
}).await?; })
.await?;
let game_version_names = res.game let game_version_names = res
.game
.iter() .iter()
.filter(|game| game.stable) .filter(|game| game.stable)
.map(|game| game.version.clone()) .map(|game| game.version.clone())
@ -133,7 +147,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.default(0) .default(0)
.interact()?; .interact()?;
let loader_version_names = res.loader let loader_version_names = res
.loader
.iter() .iter()
.filter(|loader| loader.stable) .filter(|loader| loader.stable)
.map(|loader| loader.version.clone()) .map(|loader| loader.version.clone())
@ -147,16 +162,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
let game_version = game_version_names[game_version_index].clone(); let game_version = game_version_names[game_version_index].clone();
let loader_version = loader_version_names[loader_version_index].clone(); let loader_version = loader_version_names[loader_version_index].clone();
let installer_version = res.installer let installer_version = res
.installer
.iter() .iter()
.find_map(|installer| { .find_map(|installer| {
if installer.stable { Some(installer.version.clone()) } if installer.stable {
else { None } Some(installer.version.clone())
}).ok_or("No Installer Version Found")?; } else {
None
}
})
.ok_or("No Installer Version Found")?;
let start_msg = format!( let start_msg = format!(
"downloading server.jar for {} {}, {}", "downloading server.jar for {} {}, {}",
loaders[loader_index], loader_version_names[loader_version_index], loaders[loader_index],
loader_version_names[loader_version_index],
game_version_names[game_version_index], game_version_names[game_version_index],
); );
with_spinner(&start_msg, "download finished", || async { with_spinner(&start_msg, "download finished", || async {
@ -173,8 +194,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
} }
Ok(()) Ok(())
}).await?; })
.await?;
} }
_ => return Err("Invalid selection".into()), _ => return Err("Invalid selection".into()),
} }

View file

@ -1,2 +1,2 @@
pub mod vanilla;
pub mod fabric; pub mod fabric;
pub mod vanilla;

View file

@ -1,5 +1,5 @@
use std::{error::Error, future::Future};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use std::{error::Error, future::Future};
pub async fn with_spinner<F, Fut, T>( pub async fn with_spinner<F, Fut, T>(
start_msg: &str, start_msg: &str,
@ -19,7 +19,6 @@ where
pb.finish_and_clear(); pb.finish_and_clear();
if !finish_msg.is_empty() { if !finish_msg.is_empty() {
println!("{} {}", dialoguer::console::style("✔").green(), finish_msg); println!("{} {}", dialoguer::console::style("✔").green(), finish_msg);
} }
result result
@ -31,14 +30,15 @@ pub async fn with_progressbar<F, Fut, T>(
len: u64, len: u64,
f: F, f: F,
) -> Result<T, Box<dyn Error>> ) -> Result<T, Box<dyn Error>>
where where
F: FnOnce(ProgressBar) -> Fut, F: FnOnce(ProgressBar) -> Fut,
Fut: Future<Output = Result<T, Box<dyn Error>>> Fut: Future<Output = Result<T, Box<dyn Error>>>,
{ {
let pb = ProgressBar::new(len); let pb = ProgressBar::new(len);
pb.set_style(ProgressStyle::default_bar().template( pb.set_style(
"{msg}\n{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})", ProgressStyle::default_bar()
)?); .template("{msg}\n{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})")?,
);
pb.set_message(start_msg.to_string()); pb.set_message(start_msg.to_string());
let result = f(pb.clone()).await; let result = f(pb.clone()).await;