Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
clippy & fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TOwInOK committed Mar 21, 2024
1 parent 4303154 commit 38b6051
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/config/additions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ fn plugins() -> String {
}
fn configs() -> String {
"./configs".to_string()
}
}
1 change: 1 addition & 0 deletions src/config/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 8 additions & 8 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -21,7 +21,7 @@ pub struct Config {
#[serde(default)]
pub core: Core,
/// Лист плагинов
/// [name]:[Plugin]
/// [name]:[Plugin]
#[serde(default)]
pub plugins: HashMap<String, Plugin>,
/// Additions for git or keys
Expand All @@ -41,4 +41,4 @@ impl Config {

Ok(config)
}
}
}
7 changes: 2 additions & 5 deletions src/config/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use crate::config::Versions;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
pub struct Plugin {
Expand All @@ -20,23 +20,20 @@ pub struct Plugin {
pub force_update: bool,
}


#[derive(Deserialize, Serialize, Debug, Default)]
pub enum Sources {
Bukkit,
Spigot,
Hangar,
#[default]
Modrinth,
CurseForge
CurseForge,
}


#[derive(Deserialize, Serialize, Debug, Default)]
pub enum Channels {
#[default]
Stable,
Beta,
Alpha,
}

9 changes: 1 addition & 8 deletions src/downloader/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 = <Sha1 as Digest1>::new();
Expand Down Expand Up @@ -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 {
Expand Down
71 changes: 49 additions & 22 deletions src/downloader/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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!(),
Expand All @@ -52,22 +60,35 @@ impl Downloader {
}
}
///Check plugins and add it into list for download.
async fn check_plugins(&mut self, plugins: &HashMap<String, Plugin>) -> Result<(), DownloadErrors> {
if plugins.is_empty() {return Ok(());};
async fn check_plugins(
&mut self,
plugins: &HashMap<String, Plugin>,
) -> 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!(),
Sources::Hangar => todo!(),
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!()
}
Expand All @@ -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?;

Expand All @@ -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(),
))
}
}
}
20 changes: 11 additions & 9 deletions src/downloader/models/vanilla.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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))
Expand All @@ -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
))
})
}
}
2 changes: 1 addition & 1 deletion src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod errors;
pub mod errors;
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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}")});
}
downloader
.check(&mut config)
.await
.unwrap_or_else(|e| error!("{e}"));
}

0 comments on commit 38b6051

Please sign in to comment.