Skip to content

Commit 0060fc4

Browse files
committed
chore: Refactor code to improve readability and maintainability
1 parent 216273c commit 0060fc4

File tree

2 files changed

+72
-66
lines changed

2 files changed

+72
-66
lines changed

src-tauri/src/main.rs

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,15 @@
11
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
22
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
33
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
4-
use reqwest::get;
5-
use serde_json::{Value, json};
6-
use std::collections::HashMap;
74

85
#[macro_use]
96
extern crate maplit;
107

11-
// Function to fetch type effectiveness
12-
async fn fetch_type_data(url: &str) -> Result<Value, String> {
13-
match get(url).await {
14-
Ok(response) => {
15-
if response.status().is_success() {
16-
let json: Value = response.json().await.unwrap();
17-
Ok(json)
18-
} else {
19-
Err("Failed to fetch type data".to_string())
20-
}
21-
}
22-
Err(_) => Err("Failed to connect to PokéAPI".to_string()),
23-
}
24-
}
8+
mod weakness_helpers;
259

26-
// Helper function to generate icon URL based on type ID
27-
fn generate_icon_url(type_id: &str) -> String {
28-
format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id)
29-
}
30-
31-
// Function to calculate weaknesses based on Pokémon types
32-
async fn calculate_weaknesses(types: &Vec<Value>) -> Result<HashMap<String, Vec<HashMap<String, String>>>, String> {
33-
let mut weaknesses: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
34-
35-
for pokemon_type in types {
36-
//let type_name = pokemon_type["type"]["name"].as_str().unwrap();
37-
//let type_id = type_url.split('/').filter(|&s| !s.is_empty()).last().unwrap(); // Extract the type ID
38-
let type_url = pokemon_type["type"]["url"].as_str().unwrap();
39-
40-
if let Ok(type_data) = fetch_type_data(type_url).await {
41-
let damage_relations = &type_data["damage_relations"];
42-
43-
for (key, effectiveness) in &[
44-
("2x", "double_damage_from"),
45-
("0.5x", "half_damage_from"),
46-
("0x", "no_damage_from"),
47-
] {
48-
if let Some(damage_types) = damage_relations[effectiveness].as_array() {
49-
for damage_type in damage_types {
50-
let type_name = damage_type["name"].as_str().unwrap().to_string();
51-
let type_id = damage_type["url"]
52-
.as_str()
53-
.unwrap()
54-
.split('/')
55-
.filter(|&s| !s.is_empty())
56-
.last()
57-
.unwrap();
58-
let icon_url = generate_icon_url(type_id);
59-
weaknesses.entry(key.to_string()).or_default().push(
60-
hashmap!{
61-
"type".to_string() => type_name,
62-
"icon".to_string() => icon_url
63-
}
64-
);
65-
}
66-
}
67-
}
68-
} else {
69-
return Err("Failed to fetch type data".to_string());
70-
}
71-
}
72-
73-
Ok(weaknesses)
74-
}
10+
use reqwest::get;
11+
use serde_json::{Value, json};
12+
use weakness_helpers::calculate_weaknesses;
7513

7614
// Fetch input from PokéAPI and include weaknesses
7715
#[tauri::command]

src-tauri/src/weakness_helpers.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use reqwest::get;
2+
use serde_json::Value;
3+
use std::collections::HashMap;
4+
5+
// Function to fetch type effectiveness
6+
pub async fn fetch_type_data(url: &str) -> Result<Value, String> {
7+
match get(url).await {
8+
Ok(response) => {
9+
if response.status().is_success() {
10+
let json: Value = response.json().await.unwrap();
11+
Ok(json)
12+
} else {
13+
Err("Failed to fetch type data".to_string())
14+
}
15+
}
16+
Err(_) => Err("Failed to connect to PokéAPI".to_string()),
17+
}
18+
}
19+
20+
// Helper function to generate icon URL based on type ID
21+
pub fn generate_icon_url(type_id: &str) -> String {
22+
format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id)
23+
}
24+
25+
// Function to calculate weaknesses based on Pokémon types
26+
pub async fn calculate_weaknesses(types: &Vec<Value>) -> Result<HashMap<String, Vec<HashMap<String, String>>>, String> {
27+
let mut weaknesses: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
28+
29+
for pokemon_type in types {
30+
//let type_name = pokemon_type["type"]["name"].as_str().unwrap();
31+
//let type_id = type_url.split('/').filter(|&s| !s.is_empty()).last().unwrap(); // Extract the type ID
32+
let type_url = pokemon_type["type"]["url"].as_str().unwrap();
33+
34+
if let Ok(type_data) = fetch_type_data(type_url).await {
35+
let damage_relations = &type_data["damage_relations"];
36+
37+
for (key, effectiveness) in &[
38+
("2x", "double_damage_from"),
39+
("0.5x", "half_damage_from"),
40+
("0x", "no_damage_from"),
41+
] {
42+
if let Some(damage_types) = damage_relations[effectiveness].as_array() {
43+
for damage_type in damage_types {
44+
let type_name = damage_type["name"].as_str().unwrap().to_string();
45+
let type_id = damage_type["url"]
46+
.as_str()
47+
.unwrap()
48+
.split('/')
49+
.filter(|&s| !s.is_empty())
50+
.last()
51+
.unwrap();
52+
let icon_url = generate_icon_url(type_id);
53+
weaknesses.entry(key.to_string()).or_default().push(
54+
hashmap!{
55+
"type".to_string() => type_name,
56+
"icon".to_string() => icon_url
57+
}
58+
);
59+
}
60+
}
61+
}
62+
} else {
63+
return Err("Failed to fetch type data".to_string());
64+
}
65+
}
66+
67+
Ok(weaknesses)
68+
}

0 commit comments

Comments
 (0)