From 38b605193b041babd261e41f8a9cddb0b7dff7c1 Mon Sep 17 00:00:00 2001 From: TOwInOK <60252419+TOwInOK@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:15:37 +0700 Subject: [PATCH] clippy & fmt --- src/config/additions.rs | 2 +- src/config/lock.rs | 1 + src/config/mod.rs | 16 +++---- src/config/plugins.rs | 7 +--- src/downloader/hash.rs | 9 +--- src/downloader/mod.rs | 71 ++++++++++++++++++++++---------- src/downloader/models/vanilla.rs | 20 +++++---- src/errors/mod.rs | 2 +- src/main.rs | 11 +++-- 9 files changed, 81 insertions(+), 58 deletions(-) diff --git a/src/config/additions.rs b/src/config/additions.rs index f088a8a..125b9cf 100644 --- a/src/config/additions.rs +++ b/src/config/additions.rs @@ -30,4 +30,4 @@ fn plugins() -> String { } fn configs() -> String { "./configs".to_string() -} \ No newline at end of file +} diff --git a/src/config/lock.rs b/src/config/lock.rs index e69de29..8b13789 100644 --- a/src/config/lock.rs +++ b/src/config/lock.rs @@ -0,0 +1 @@ + diff --git a/src/config/mod.rs b/src/config/mod.rs index 50d8508..ee4db40 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,18 +1,18 @@ -pub mod plugins; -pub mod versions; -pub mod core; pub mod additions; +pub mod core; mod lock; +pub mod plugins; +pub mod versions; use crate::errors::errors::ConfigErrors; use additions::Additions; -use std::collections::HashMap; use core::Core; -use versions::Versions; use log::info; +use plugins::Plugin; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use tokio::fs; -use plugins::Plugin; +use versions::Versions; /// Структура для инициализации конфигурации /// #[derive(Deserialize, Serialize, Debug, Default)] @@ -21,7 +21,7 @@ pub struct Config { #[serde(default)] pub core: Core, /// Лист плагинов - /// [name]:[Plugin] + /// [name]:[Plugin] #[serde(default)] pub plugins: HashMap, /// Additions for git or keys @@ -41,4 +41,4 @@ impl Config { Ok(config) } -} \ No newline at end of file +} diff --git a/src/config/plugins.rs b/src/config/plugins.rs index e0bd01d..5182b88 100644 --- a/src/config/plugins.rs +++ b/src/config/plugins.rs @@ -1,5 +1,5 @@ -use serde::{Deserialize, Serialize}; use crate::config::Versions; +use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Debug)] pub struct Plugin { @@ -20,7 +20,6 @@ pub struct Plugin { pub force_update: bool, } - #[derive(Deserialize, Serialize, Debug, Default)] pub enum Sources { Bukkit, @@ -28,10 +27,9 @@ pub enum Sources { Hangar, #[default] Modrinth, - CurseForge + CurseForge, } - #[derive(Deserialize, Serialize, Debug, Default)] pub enum Channels { #[default] @@ -39,4 +37,3 @@ pub enum Channels { Beta, Alpha, } - diff --git a/src/downloader/hash.rs b/src/downloader/hash.rs index 6c2f49c..e97de3c 100644 --- a/src/downloader/hash.rs +++ b/src/downloader/hash.rs @@ -5,8 +5,6 @@ use sha2::Digest as Digest256; use sha2::Sha256; use tokio::io::AsyncReadExt; - - #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)] pub enum ChooseHash { SHA1(String), @@ -15,10 +13,7 @@ pub enum ChooseHash { } impl ChooseHash { - pub async fn calculate_hash( - self, - mut reader: impl tokio::io::AsyncRead + Unpin, - ) -> bool { + pub async fn calculate_hash(self, mut reader: impl tokio::io::AsyncRead + Unpin) -> bool { match self { ChooseHash::SHA1(e) => { let mut hashed = ::new(); @@ -60,8 +55,6 @@ impl ChooseHash { } } - - impl std::fmt::Display for ChooseHash { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { diff --git a/src/downloader/mod.rs b/src/downloader/mod.rs index b6ff4fa..52e525c 100644 --- a/src/downloader/mod.rs +++ b/src/downloader/mod.rs @@ -1,12 +1,18 @@ mod hash; -mod downloader; mod models; -use std::collections::HashMap; use log::{debug, info}; +use std::collections::HashMap; -use crate::{config::{additions::Additions, core::{Core, Provider}, plugins::{Plugin, Sources}, Config}, errors::errors::DownloadErrors}; use self::{hash::ChooseHash, models::vanilla::Vanilla}; +use crate::{ + config::{ + core::{Core, Provider}, + plugins::{Plugin, Sources}, + Config, + }, + errors::errors::DownloadErrors, +}; type Name = String; type Link = String; @@ -17,31 +23,33 @@ type Link = String; pub struct Downloader(); impl Downloader { - - pub async fn new () -> Self { + pub async fn new() -> Self { Self {} } - + pub async fn check(self, config: &mut Config) -> Result<(), DownloadErrors> { info!("Start check fn"); - self.check_core(&config.core, &config.additions.path_to_core).await?; - // self.check_plugins(&config.plugins).await?; + self.check_core(&config.core, &config.additions.path_to_core) + .await?; + // self.check_plugins(&config.plugins).await?; todo!() } ///Check core and add it into list for download. - async fn check_core(self, core: &Core, path: &String) -> Result<(), DownloadErrors> { + async fn check_core(self, core: &Core, path: &str) -> Result<(), DownloadErrors> { info!("Check freeze and force_update"); - if core.freeze && !core.force_update {return Ok(());}; + if core.freeze && !core.force_update { + return Ok(()); + }; info!("Start to match provider of core"); match &core.provider { Provider::Vanilla => { info!("Find vanilla!"); - let (link,hash) = Vanilla::find(&core.version).await?; + let (link, hash) = Vanilla::find(&core.version).await?; debug!("Find vanilla link: {}, hash: {}", &link, &hash); info!("Start to download core!"); self.download_core("Vanilla", link, hash, path).await - }, + } Provider::Bukkit => todo!(), Provider::Spigot => todo!(), Provider::Paper => todo!(), @@ -52,11 +60,18 @@ impl Downloader { } } ///Check plugins and add it into list for download. - async fn check_plugins(&mut self, plugins: &HashMap) -> Result<(), DownloadErrors> { - if plugins.is_empty() {return Ok(());}; + async fn check_plugins( + &mut self, + plugins: &HashMap, + ) -> Result<(), DownloadErrors> { + if plugins.is_empty() { + return Ok(()); + }; - for (name,plugin) in plugins.iter() { - if plugin.freeze && !plugin.force_update {return Ok(());}; + for (name, plugin) in plugins.iter() { + if plugin.freeze && !plugin.force_update { + return Ok(()); + }; match plugin.source { Sources::Bukkit => todo!(), Sources::Spigot => todo!(), @@ -64,10 +79,16 @@ impl Downloader { Sources::Modrinth => todo!(), Sources::CurseForge => todo!(), } - } + } todo!() } - async fn download_core(self, name: &str, link: String, hash: ChooseHash, download_dir: &String) -> Result<(), DownloadErrors> { + async fn download_core( + self, + _name: &str, + link: String, + hash: ChooseHash, + download_dir: &str, + ) -> Result<(), DownloadErrors> { get_file(link, hash, download_dir).await?; todo!() } @@ -76,11 +97,15 @@ impl Downloader { } } -use std::path::Path; use std::fs::File; use std::io::Write; +use std::path::Path; -async fn get_file(link: String, hash: ChooseHash, download_dir: &str) -> Result<(), DownloadErrors> { +async fn get_file( + link: String, + hash: ChooseHash, + download_dir: &str, +) -> Result<(), DownloadErrors> { let response = reqwest::get(&link).await?; let content = response.bytes().await?; @@ -94,6 +119,8 @@ async fn get_file(link: String, hash: ChooseHash, download_dir: &str) -> Result< file.write_all(&content)?; //write Ok(()) } else { - Err(DownloadErrors::DownloadCorrupt("Hash doesn't match".to_string())) + Err(DownloadErrors::DownloadCorrupt( + "Hash doesn't match".to_string(), + )) } -} \ No newline at end of file +} diff --git a/src/downloader/models/vanilla.rs b/src/downloader/models/vanilla.rs index 470d6f2..1047471 100644 --- a/src/downloader/models/vanilla.rs +++ b/src/downloader/models/vanilla.rs @@ -1,12 +1,12 @@ +use crate::config::versions::Versions; +use crate::downloader::hash::ChooseHash; +use crate::errors::errors::ConfigErrors; use log::debug; use log::info; use log::trace; use log::warn; use serde::Deserialize; use serde::Serialize; -use crate::downloader::hash::ChooseHash; -use crate::errors::errors::ConfigErrors; -use crate::config::versions::Versions; type OuterLink = String; @@ -101,13 +101,12 @@ impl Vanilla { trace!("Start find version of core!"); let response = reqwest::get(LINK).await?; let vanilla: Vanilla = response.json().await?; - let local_version: &str; - match version { - Versions::Version(e) => local_version = e, - Versions::Latest => local_version = &vanilla.latest.release, + let local_version: &String = match version { + Versions::Version(e) => e, + Versions::Latest => &vanilla.latest.release, }; info!("Need to find: {}", &local_version); - vanilla + vanilla .versions .iter() .find(|x| x.version.contains(local_version)) @@ -116,7 +115,10 @@ impl Vanilla { x.url.clone() }) .ok_or_else(|| { - ConfigErrors::LoadCorrupt(format!("No one version like: {}, not found", local_version)) + ConfigErrors::LoadCorrupt(format!( + "No one version like: {}, not found", + local_version + )) }) } } diff --git a/src/errors/mod.rs b/src/errors/mod.rs index 18e02b2..629e98f 100644 --- a/src/errors/mod.rs +++ b/src/errors/mod.rs @@ -1 +1 @@ -pub mod errors; \ No newline at end of file +pub mod errors; diff --git a/src/main.rs b/src/main.rs index 767f001..4503747 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ mod config; -mod errors; mod downloader; +mod errors; use config::Config; use downloader::Downloader; -use log::{error, trace}; +use log::error; #[tokio::main] async fn main() { pretty_env_logger::formatted_builder() @@ -20,5 +20,8 @@ async fn main() { }); log::debug!("{:#?}", config); let downloader = Downloader::new().await; - downloader.check(&mut config).await.unwrap_or_else(|e| {error!("{e}")}); -} \ No newline at end of file + downloader + .check(&mut config) + .await + .unwrap_or_else(|e| error!("{e}")); +}