1+ // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
12// Prevents additional console window on Windows in release, DO NOT REMOVE!!
23#![ cfg_attr( not( debug_assertions) , windows_subsystem = "windows" ) ]
34use reqwest:: get;
@@ -7,12 +8,6 @@ use std::collections::HashMap;
78#[ macro_use]
89extern crate maplit;
910
10- // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
11- #[ tauri:: command]
12- fn greet ( name : & str ) -> String {
13- format ! ( "{} does not exist yet!" , name)
14- }
15-
1611// Function to fetch type effectiveness
1712async fn fetch_type_data ( url : & str ) -> Result < Value , String > {
1813 match get ( url) . await {
@@ -28,62 +23,46 @@ async fn fetch_type_data(url: &str) -> Result<Value, String> {
2823 }
2924}
3025
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+
3131// Function to calculate weaknesses based on Pokémon types
3232async fn calculate_weaknesses ( types : & Vec < Value > ) -> Result < HashMap < String , Vec < HashMap < String , String > > > , String > {
3333 let mut weaknesses: HashMap < String , Vec < HashMap < String , String > > > = HashMap :: new ( ) ;
3434
3535 for pokemon_type in types {
36- let type_name = pokemon_type[ "type" ] [ "name" ] . as_str ( ) . unwrap ( ) ;
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
3738 let type_url = pokemon_type[ "type" ] [ "url" ] . as_str ( ) . unwrap ( ) ;
38- let type_id = type_url. split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ; // Extract the type ID
39-
40- let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
4139
4240 if let Ok ( type_data) = fetch_type_data ( type_url) . await {
4341 let damage_relations = & type_data[ "damage_relations" ] ;
4442
45- // Group by 2x effectiveness
46- if let Some ( double_damage_from) = damage_relations[ "double_damage_from" ] . as_array ( ) {
47- for damage_type in double_damage_from {
48- let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
49- let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
50- let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
51- weaknesses. entry ( "2x" . to_string ( ) ) . or_default ( ) . push (
52- hashmap ! {
53- "type" . to_string( ) => type_name,
54- "icon" . to_string( ) => icon_url
55- }
56- ) ;
57- }
58- }
59-
60- // Group by 0.5x effectiveness
61- if let Some ( half_damage_from) = damage_relations[ "half_damage_from" ] . as_array ( ) {
62- for damage_type in half_damage_from {
63- let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
64- let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
65- let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
66- weaknesses. entry ( "0.5x" . to_string ( ) ) . or_default ( ) . push (
67- hashmap ! {
68- "type" . to_string( ) => type_name,
69- "icon" . to_string( ) => icon_url
70- }
71- ) ;
72- }
73- }
74-
75- // Group by 0x effectiveness
76- if let Some ( no_damage_from) = damage_relations[ "no_damage_from" ] . as_array ( ) {
77- for damage_type in no_damage_from {
78- let type_name = damage_type[ "name" ] . as_str ( ) . unwrap ( ) . to_string ( ) ;
79- let type_id = damage_type[ "url" ] . as_str ( ) . unwrap ( ) . split ( '/' ) . filter ( |& s| !s. is_empty ( ) ) . last ( ) . unwrap ( ) ;
80- let icon_url = format ! ( "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png" , type_id) ;
81- weaknesses. entry ( "0x" . to_string ( ) ) . or_default ( ) . push (
82- hashmap ! {
83- "type" . to_string( ) => type_name,
84- "icon" . to_string( ) => icon_url
85- }
86- ) ;
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+ }
8766 }
8867 }
8968 } else {
@@ -123,7 +102,7 @@ async fn search_pokemon(name: &str) -> Result<String, String> {
123102
124103fn main ( ) {
125104 tauri:: Builder :: default ( )
126- . invoke_handler ( tauri:: generate_handler![ greet , search_pokemon] )
105+ . invoke_handler ( tauri:: generate_handler![ search_pokemon] )
127106 . run ( tauri:: generate_context!( ) )
128107 . expect ( "error while running tauri application" ) ;
129108}
0 commit comments