From bfcf16040946e0ef561700394da9c9cab33c8779 Mon Sep 17 00:00:00 2001 From: TYTheBeast Date: Wed, 9 Oct 2024 22:16:49 +0200 Subject: [PATCH] Added automatic config fetching --- src/args/item_search_args.rs | 2 +- src/config/build_config.rs | 16 +++++++++++++ src/items/api_items/api_to_internal_items.rs | 24 +++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/args/item_search_args.rs b/src/args/item_search_args.rs index 2e80fe8..1d3d4c9 100644 --- a/src/args/item_search_args.rs +++ b/src/args/item_search_args.rs @@ -3,7 +3,7 @@ use std::fmt::Display; use clap::{Parser, ValueEnum}; #[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] +#[command(author, version, about, long_about)] pub struct ItemSearchArgs { /// Apparel type #[arg(short, long)] diff --git a/src/config/build_config.rs b/src/config/build_config.rs index 1163df8..35e7ee4 100644 --- a/src/config/build_config.rs +++ b/src/config/build_config.rs @@ -97,10 +97,26 @@ pub struct ThresholdFifth { } pub async fn load_config(path: impl AsRef) -> Result { + // Check if the file exists + if !path.as_ref().exists() { + // Fetch the default config from https://raw.githubusercontent.com/TYTheBeast/WynnBuilderTools-Rekindled/refs/heads/master/config/config.toml + let url = "https://raw.githubusercontent.com/TYTheBeast/WynnBuilderTools-Rekindled/refs/heads/master/config/config.toml"; + let request = reqwest::get(url) + .await + .unwrap() + .text() + .await + .unwrap(); + + // Write the default config to the file + tokio::fs::write(path.as_ref(), request).await.unwrap(); + } + let mut f = match File::open(path).await { Ok(ok) => Ok(ok), Err(err) => Err(err.to_string()), }?; + let mut buffer = Vec::new(); match f.read_to_end(&mut buffer).await { diff --git a/src/items/api_items/api_to_internal_items.rs b/src/items/api_items/api_to_internal_items.rs index 1543d77..1ce91c6 100644 --- a/src/items/api_items/api_to_internal_items.rs +++ b/src/items/api_items/api_to_internal_items.rs @@ -8,7 +8,7 @@ use std::{ use crate::items::Items; use crate::Item; -use super::{ApiItems, StatOrInt}; +use super::{ApiItems, StatOrInt, ApiItem}; fn string_to_i32_hash(s: &str) -> i32 { let mut hasher = DefaultHasher::new(); @@ -28,8 +28,19 @@ impl From for Items { fn from(api_items: ApiItems) -> Self { let mut items = Items { items: Vec::new() }; - for (name, api_item) in api_items { - let ids = api_item.identifications.as_ref(); + for (_, api_item) in api_items { + let item: Item = api_item.into(); + + items.items.push(item); + } + + items + } +} + +impl From for Item { + fn from(api_item: ApiItem) -> Self { + let ids = api_item.identifications.as_ref(); let max_or_int = |option: Option| match option { Some(v) => match v { @@ -137,9 +148,6 @@ impl From for Items { xpb: ids.and_then(|ids| max_or_int(ids.xp_bonus)), }; - items.items.push(item); - } - - items + item } -} +} \ No newline at end of file