first commit
This commit is contained in:
135
src-tauri/src/main.rs
Normal file
135
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
#![cfg_attr(
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wana_kana::to_katakana::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
struct SearchResult {
|
||||
id: i64,
|
||||
dex_index: String,
|
||||
jap_name: String,
|
||||
eng_name: String,
|
||||
types: String,
|
||||
hp: i64,
|
||||
attack: i64,
|
||||
defense: i64,
|
||||
special_attack: i64,
|
||||
special_defense: i64,
|
||||
speed: i64,
|
||||
thumb_small: String,
|
||||
thumb_big: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
struct Autosearch {
|
||||
id: i64,
|
||||
name: String,
|
||||
}
|
||||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
let connection = sqlite::open("./pokemon.db").unwrap();
|
||||
let query = "SELECT types FROM pokemon WHERE eng_name = ?";
|
||||
let mut result = "".to_string();
|
||||
for row in connection
|
||||
.prepare(query)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.bind((1, name))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
{
|
||||
result = row.read::<&str, _>("types").to_string();
|
||||
}
|
||||
format!("Hello, {}! You've been greeted from Rust! {}", name, result)
|
||||
}
|
||||
#[tauri::command]
|
||||
fn autosearch(keyword: &str) -> serde_json::Value {
|
||||
|
||||
let connection = sqlite::open("./pokemon.db").unwrap();
|
||||
let query = "SELECT * FROM pokemon WHERE LOWER(eng_name) like ? or jap_name like ? or jap_name like ? or LOWER(dex_index) like ? LIMIT 5";
|
||||
let mut result: Vec<Autosearch> = vec![];
|
||||
let aste_keyword = format!("%{}%", keyword.to_lowercase());
|
||||
let katakana = format!("%{}%", to_katakana(keyword));
|
||||
println!("{}", katakana);
|
||||
for row in connection
|
||||
.prepare(query)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.bind((1, aste_keyword.as_str()))
|
||||
.unwrap()
|
||||
.bind((2, aste_keyword.as_str()))
|
||||
.unwrap()
|
||||
.bind((3, katakana.as_str()))
|
||||
.unwrap()
|
||||
.bind((4, aste_keyword.as_str()))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
{
|
||||
let row_result = Autosearch {
|
||||
id: row.read::<i64, _>("id"),
|
||||
name: format!(
|
||||
"{} - {} ({})",
|
||||
row.read::<&str, _>("dex_index").to_string(),
|
||||
row.read::<&str, _>("jap_name").to_string(),
|
||||
row.read::<&str, _>("eng_name").to_string()
|
||||
),
|
||||
};
|
||||
result.push(row_result);
|
||||
}
|
||||
println!("{:?}", result);
|
||||
serde_json::to_value(result).unwrap_or(serde_json::json!("[]"))
|
||||
}
|
||||
#[tauri::command]
|
||||
fn search(index: i64) -> SearchResult {
|
||||
let connection = sqlite::open("./pokemon.db").unwrap();
|
||||
let query = "SELECT * FROM pokemon WHERE id = ?";
|
||||
for row in connection
|
||||
.prepare(query)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.bind((1, index))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
{
|
||||
let row_result = SearchResult {
|
||||
id: row.read::<i64, _>("id"),
|
||||
dex_index: row.read::<&str, _>("dex_index").to_string(),
|
||||
eng_name: row.read::<&str, _>("eng_name").to_string(),
|
||||
jap_name: row.read::<&str, _>("jap_name").to_string(),
|
||||
types: row.read::<&str, _>("types").to_string(),
|
||||
thumb_big: row.read::<&str, _>("thumb_big").to_string(),
|
||||
thumb_small: row.read::<&str, _>("thumb_small").to_string(),
|
||||
hp: row.read::<i64, _>("hp"),
|
||||
attack: row.read::<i64, _>("attack"),
|
||||
defense: row.read::<i64, _>("defense"),
|
||||
special_attack: row.read::<i64, _>("special_attack"),
|
||||
special_defense: row.read::<i64, _>("special_defense"),
|
||||
speed: row.read::<i64, _>("speed"),
|
||||
};
|
||||
return row_result
|
||||
}
|
||||
return SearchResult {
|
||||
id: 0,
|
||||
dex_index: "#???".to_string(),
|
||||
eng_name: "Missigno".to_string(),
|
||||
jap_name: "Missigno".to_string(),
|
||||
types: "[]".to_string(),
|
||||
thumb_big: "".to_string(),
|
||||
thumb_small: "".to_string(),
|
||||
hp: 0,
|
||||
attack: 0,
|
||||
defense: 0,
|
||||
special_attack: 0,
|
||||
special_defense: 0,
|
||||
speed: 0,
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![greet, autosearch, search])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Reference in New Issue
Block a user