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

Commit

Permalink
Сделан нормальный парсер для toml
Browse files Browse the repository at this point in the history
Сделана основа для скачивания файлов.
  • Loading branch information
TOwInOK committed Mar 2, 2024
1 parent f8eb0fd commit 8e1bace
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 116 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}
126 changes: 124 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = { version = "0.4.21", features = ["serde"] }
pretty_env_logger = "0.5.0"
reqwest = "0.11.22"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.114"
thiserror = "1.0.57"
tokio = { version = "1.34.0", features = ["full"] }
toml = "0.8.8"
19 changes: 9 additions & 10 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#Example of config file

[version]
#which version you want? [spigot, paper, purpur]
core = "paper"
#any version if they have.
version = "1.20.1"
#lock you version of version
frozen = false
#which version you want? [Spigot, Paper, Purpur]
core = "Paper"
#First: any version if they have.
#Second: lock version. (stop update)
version = ["1.20.1", false]
#Example: version = ["1.20.1", false]
#We are loading version 1.20.1 with the tag FFFw2f
#Because the flag is "false", we no longer update this version to newer versions of same verion.

[plugins]
#load plugins from Modrinth
modrinth = [
"Chunky",
"Discordsrv",
"CoreProtect",
"NbtApi",
"WorldEdit",
"Simple Voice Chat",
]
#load plugins from SpigotMC
spigot = []
Expand Down
25 changes: 25 additions & 0 deletions src/config/datapack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serde::{Serialize, Deserialize};

///Lists of Datapacks
#[derive(Deserialize, Serialize, Debug)]
pub struct Datapack {
//list to download from https://modrinth.com/
modrinth: Option<Vec<String>>,
//list to download from https://www.spigotmc.org/
spigot: Option<Vec<String>>,
//list to download from https://hangar.papermc.io/
paper: Option<Vec<String>>,
//List of plugins to stop updating
frozen: Option<Vec<String>>,
}

impl Datapack {
fn new(modrinth: Option<Vec<String>>, spigot: Option<Vec<String>>, paper: Option<Vec<String>>, frozen: Option<Vec<String>>) -> Self {
Self { modrinth, spigot, paper, frozen }
}
pub fn default() -> Self {
Datapack::new(None, None, None, None)
}
}


16 changes: 16 additions & 0 deletions src/config/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DownloadErrors {
#[error("Загрузка прекращенна потому что: {0}")]
DownloadCorrapt(String),

}


#[derive(Error, Debug)]
pub enum ConfigErrors {
#[error("Загрузка файла не была успешна: {0}")]
LoadCorrapt(String),

}

102 changes: 102 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
mod datapack;
mod errors;
mod plugin;
mod version;
mod model;


use std::default;
use log::{error, info};
use tokio::fs;
use errors::*;
use serde::{Serialize, Deserialize};
use version::Versions;
use datapack::*;
use plugin::Plugin;


///Struct to load config from toml file.
#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
version: Versions,
plugins: Option<Plugin>,
datapacks: Option<Datapack>,
}

impl Config {
fn new(version: Versions, plugins: Option<Plugin>, datapacks: Option<Datapack>) -> Self {
Self { version, plugins, datapacks }
}


fn default() -> Self {
Config::new(Versions::default(), None, None)
}

pub async fn load_config(path: String) -> Config {
let toml = {
info!("Загрузка конфигурационного файла...");
let result = fs::read_to_string(path).await;
match result {
Ok(content) => {
info!("Файл успешно загружен.");
content
}
Err(e) => {
error!(
"Ваш конфигурационный файл не был обнаружен, загружаю стандартные настройки.\nПричина ошибки: {e}"
);
return Config::default();
}
}
};
info!("Инициализация конфигурационного файла...");
let config: Config = match toml::from_str(&toml) {
Ok(parsed_config) => {
info!("Конфигурация успешно инициализированна.");
parsed_config
}
Err(e) => {
error!("Не удалось загрузить конфигурацию, загружаю стандартные настройки.\nПричина ошибки: {e}");
return Config::default();
}
};
config
}



pub async fn download(config: Config) -> Result<(), DownloadErrors> {
let file = config.download_core().await;
todo!()
}

async fn download_plugins() -> Result<(), DownloadErrors> {
todo!()
}
async fn download_mods() -> Result<(), DownloadErrors> {
todo!()
}
async fn download_datapacks() -> Result<(), DownloadErrors> {
todo!()
}

async fn download_core(self) -> Result<Option<()>, DownloadErrors> {
match self.version {
Versions::Purpur(ver, freez) => {
if !freez {
//We don't need to download
return Ok(None) ;
}
//use if error
Err(DownloadErrors::DownloadCorrapt("ff".to_string()))
},
Versions::Paper(ver, freez) => todo!(),
Versions::Spigot(ver, freez) => todo!(),
Versions::Bucket(ver, freez) => todo!(),
Versions::Vanila(ver, freez) => todo!(),
}
}

}

Loading

0 comments on commit 8e1bace

Please sign in to comment.