From 0515548682a95db643a008146105d8ecdb446e58 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 26 Dec 2022 21:57:03 +0100 Subject: changed config to toml, autocreate config; better error handling --- .gitignore | 1 + Cargo.lock | 310 ++++++++++++---------------------------------------------- Cargo.toml | 4 +- config.ini | 7 -- src/config.rs | 40 +++++--- src/error.rs | 33 ++++++- src/main.rs | 2 +- 7 files changed, 124 insertions(+), 273 deletions(-) delete mode 100644 config.ini diff --git a/.gitignore b/.gitignore index 6aebc8a..f0303eb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .planmodlist.autosave.xopp data.db.cp export.toml +config.toml diff --git a/Cargo.lock b/Cargo.lock index 6fb495c..5cb5308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ahash" version = "0.7.6" @@ -22,23 +37,27 @@ dependencies = [ "libc", ] -[[package]] -name = "async-trait" -version = "0.1.59" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6e93155431f3931513b243d371981bb2770112b370c82745a1d19d2f99364" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.13.1" @@ -51,15 +70,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "block-buffer" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" -dependencies = [ - "generic-array", -] - [[package]] name = "bumpalo" version = "3.11.1" @@ -109,25 +119,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "config" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d379af7f68bfc21714c6c7dea883544201741d2ce8274bb12fa54f89507f52a7" -dependencies = [ - "async-trait", - "json5", - "lazy_static", - "nom", - "pathdiff", - "ron", - "rust-ini", - "serde", - "serde_json", - "toml", - "yaml-rust", -] - [[package]] name = "core-foundation" version = "0.9.3" @@ -144,25 +135,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" -[[package]] -name = "cpufeatures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" -dependencies = [ - "libc", -] - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - [[package]] name = "cxx" version = "1.0.83" @@ -208,28 +180,22 @@ dependencies = [ ] [[package]] -name = "digest" -version = "0.10.6" +name = "encoding_rs" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" dependencies = [ - "block-buffer", - "crypto-common", + "cfg-if", ] [[package]] -name = "dlv-list" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" - -[[package]] -name = "encoding_rs" -version = "0.8.31" +name = "error-chain" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" dependencies = [ - "cfg-if", + "backtrace", + "version_check", ] [[package]] @@ -335,16 +301,6 @@ dependencies = [ "slab", ] -[[package]] -name = "generic-array" -version = "0.14.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.8" @@ -356,6 +312,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "gimli" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec7af912d60cdbd3677c1af9352ebae6fb8394d165568a2234df0fa00f87793" + [[package]] name = "h2" version = "0.3.15" @@ -547,17 +509,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "json5" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" -dependencies = [ - "pest", - "pest_derive", - "serde", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -590,12 +541,6 @@ dependencies = [ "cc", ] -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "lock_api" version = "0.4.9" @@ -628,10 +573,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" [[package]] -name = "minimal-lexical" -version = "0.2.1" +name = "miniz_oxide" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" +dependencies = [ + "adler", +] [[package]] name = "mio" @@ -647,10 +595,10 @@ dependencies = [ [[package]] name = "modlist" -version = "0.6.0" +version = "0.6.2" dependencies = [ "chrono", - "config", + "error-chain", "futures-util", "reqwest", "rusqlite", @@ -678,16 +626,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "nom" -version = "7.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" -dependencies = [ - "memchr", - "minimal-lexical", -] - [[package]] name = "num-integer" version = "0.1.45" @@ -717,6 +655,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "239da7f290cfa979f43f85a8efeee9a8a76d0827c356d37f9d3d7254d6b537fb" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.16.0" @@ -768,16 +715,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "ordered-multimap" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" -dependencies = [ - "dlv-list", - "hashbrown", -] - [[package]] name = "parking_lot" version = "0.12.1" @@ -801,62 +738,12 @@ dependencies = [ "windows-sys 0.42.0", ] -[[package]] -name = "pathdiff" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" - [[package]] name = "percent-encoding" version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" -[[package]] -name = "pest" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc8bed3549e0f9b0a2a78bf7c0018237a2cdf085eecbbc048e52612438e4e9d0" -dependencies = [ - "thiserror", - "ucd-trie", -] - -[[package]] -name = "pest_derive" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc078600d06ff90d4ed238f0119d84ab5d43dbaad278b0e33a8820293b32344" -dependencies = [ - "pest", - "pest_generator", -] - -[[package]] -name = "pest_generator" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28a1af60b1c4148bb269006a750cff8e2ea36aff34d2d96cf7be0b14d1bed23c" -dependencies = [ - "pest", - "pest_meta", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "pest_meta" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fec8605d59fc2ae0c6c1aefc0c7c7a9769732017c0ce07f7a9cfffa7b4404f20" -dependencies = [ - "once_cell", - "pest", - "sha1", -] - [[package]] name = "pin-project-lite" version = "0.2.9" @@ -949,17 +836,6 @@ dependencies = [ "winreg", ] -[[package]] -name = "ron" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" -dependencies = [ - "base64", - "bitflags", - "serde", -] - [[package]] name = "rusqlite" version = "0.28.0" @@ -975,14 +851,10 @@ dependencies = [ ] [[package]] -name = "rust-ini" -version = "0.18.0" +name = "rustc-demangle" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" -dependencies = [ - "cfg-if", - "ordered-multimap", -] +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" [[package]] name = "ryu" @@ -1078,17 +950,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha1" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -1157,26 +1018,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "thiserror" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "time" version = "0.1.45" @@ -1299,18 +1140,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" -[[package]] -name = "typenum" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" - -[[package]] -name = "ucd-trie" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" - [[package]] name = "unicode-bidi" version = "0.3.8" @@ -1598,12 +1427,3 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" dependencies = [ "winapi", ] - -[[package]] -name = "yaml-rust" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] diff --git a/Cargo.toml b/Cargo.toml index 13280a1..c1de0fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.6.1" +version = "0.6.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,8 +10,8 @@ reqwest = { version = "0.11", features = ["json", "stream"] } tokio = { version = "1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.87" -config = "0.13.2" rusqlite = { version = "0.28.0", features = ["bundled"] } futures-util = "0.3.14" chrono = "0.4.22" toml = "0.5.10" +error-chain = "0.12.4" diff --git a/config.ini b/config.ini deleted file mode 100644 index 345a13a..0000000 --- a/config.ini +++ /dev/null @@ -1,7 +0,0 @@ -data = "./" -clean_remove = false -downloads = "./dl" - -[apis] -modrinth = "http://localhost:8080/" -;modrinth = "https://api.modrinth.com/v2/" diff --git a/src/config.rs b/src/config.rs index ad59963..99d2ec2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,27 +1,39 @@ -use config::{Config, File, FileFormat}; -use serde::Deserialize; +use std::{fs::File, io::{Read, Write}}; -#[derive(Debug, Clone, Deserialize)] +use serde::{Serialize, Deserialize}; + +use crate::error::MLE; + +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { pub data: String, - pub downloads: String, - pub clean_remove: bool, pub apis: Apis, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Apis { pub modrinth: String, } impl Cfg { - pub fn init(path: &str) -> Self { - //TODO Error Handling - Config::builder() - .add_source(File::new(path, FileFormat::Ini)) - .build() - .unwrap() - .try_deserialize() - .unwrap() + pub fn init(path: &str) -> MLE { + let mut file = match File::open(path) { + Ok(file) => file, + Err(err) => { + if err.kind() == std::io::ErrorKind::NotFound { + println!("No config file found, creating one"); + let default_cfg = Cfg { data: String::from("./"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/") } }; + let mut file = File::create(path)?; + file.write_all(&toml::to_string(&default_cfg)?.as_bytes())?; + File::open(path)? + } else { + return Err(err.into()); + } + } + }; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let config = toml::from_str::(&content)?; + Ok(config) } } diff --git a/src/error.rs b/src/error.rs index c82688c..1ac2a48 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,16 +1,20 @@ use core::fmt; +use serde::Deserialize; pub type MLE = Result; -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub struct MLError { etype: ErrorType, message: String, } -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub enum ErrorType { - ArgumentError + ArgumentError, + ConfigError, + LibToml, + IoError, } impl std::error::Error for MLError { @@ -22,11 +26,32 @@ impl std::error::Error for MLError { impl fmt::Display for MLError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.etype { - ErrorType::ArgumentError => write!(f, "ARGS") + ErrorType::ArgumentError => write!(f, "ARGS"), + ErrorType::ConfigError => write!(f, "CONFIG"), + ErrorType::LibToml => write!(f, "TOML"), + ErrorType::IoError => write!(f, "IO") } } } +impl From for MLError { + fn from(error: toml::de::Error) -> Self { + Self { etype: ErrorType::LibToml, message: error.to_string() } + } +} + +impl From for MLError { + fn from(error: toml::ser::Error) -> Self { + Self { etype: ErrorType::LibToml, message: error.to_string() } + } +} + +impl From for MLError { + fn from(error: std::io::Error) -> Self { + Self { etype: ErrorType::IoError, message: error.to_string() } + } +} + impl MLError { pub fn new(etype: ErrorType, message: &str) -> Self { Self { etype, message: String::from(message) } diff --git a/src/main.rs b/src/main.rs index 957e5aa..a093bb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use modlist::{config::Cfg, input::get_input}; #[tokio::main] async fn main() { - let config = Cfg::init("config.ini"); + let config = Cfg::init("config.toml").unwrap(); //TODO Error Handling get_input(config).await.unwrap(); } -- cgit v1.2.3