This commit is contained in:
Hiers 2023-12-17 16:31:05 +00:00
parent b3d8a06fdf
commit 220a164c67
4 changed files with 34 additions and 58 deletions

View file

@ -7,7 +7,7 @@ use std::{
use colored::*; use colored::*;
use kradical_parsing::radk; use kradical_parsing::radk;
pub fn search_by_radical(mut query: &mut String){ pub fn search_by_radical(query: &mut String){
let mut result: HashSet<_> = HashSet::new(); let mut result: HashSet<_> = HashSet::new();
let mut aux: HashSet<_> = HashSet::new(); let mut aux: HashSet<_> = HashSet::new();
let path = get_radkfile_path(); let path = get_radkfile_path();
@ -20,7 +20,7 @@ pub fn search_by_radical(mut query: &mut String){
let mut rad = query.chars().nth(1).unwrap(); let mut rad = query.chars().nth(1).unwrap();
if rad == '*' || rad == '' { if rad == '*' || rad == '' {
/* if search_by_strokes returned an error then something is very wrong */ /* if search_by_strokes returned an error then something is very wrong */
rad = search_by_strokes(&mut query, &radk_list, 1).expect("Couldn't parse input"); rad = search_by_strokes(query, &radk_list, 1).expect("Couldn't parse input");
} }
for k in radk_list.iter() { for k in radk_list.iter() {
@ -36,7 +36,7 @@ pub fn search_by_radical(mut query: &mut String){
for (i, mut rad) in query.clone().chars().skip(2).enumerate() { for (i, mut rad) in query.clone().chars().skip(2).enumerate() {
if rad == '*' || rad == '' { if rad == '*' || rad == '' {
/* if search_by_strokes returned an error then something is very wrong */ /* if search_by_strokes returned an error then something is very wrong */
rad = search_by_strokes(&mut query, &radk_list, i+2).expect("Couldn't parse input"); rad = search_by_strokes(query, &radk_list, i+2).expect("Couldn't parse input");
} }
for k in radk_list.iter() { for k in radk_list.iter() {

View file

@ -57,45 +57,37 @@ fn main() -> Result<(), ureq::Error> {
} }
} else { } else {
query = options.query.clone(); query = options.query.clone();
if query.trim().is_empty() || query.trim() == ":" || query.trim() == "" { if query.trim().is_empty() || query.trim() == ":" || query.trim() == "" || query.trim() == "_" || query.trim() == "_" {
return Ok(()); return Ok(());
} }
} }
query = query.trim().to_string(); query = query.trim().to_string();
let mut lines_output = 0; let mut lines_output = 0;
let mut output = String::with_capacity(5242880); /* Give output 5MiB of buffer; Should be enough to avoid reallocs*/ let mut output = String::with_capacity(51200); /* Give output 50KiB of buffer; Should be enough to avoid reallocs*/
if query.starts_with(':') || query.starts_with('') { /* Kanji search */ if query.starts_with(':') || query.starts_with('') { /* Kanji search */
search_by_radical(&mut query); search_by_radical(&mut query);
} else if query.starts_with('_') || query.starts_with('_'){ /* Sentence search */ } else if query.starts_with('_') || query.starts_with('_') { /* Sentence search */
/* Do API eng->jpn request */ let bytes = query.chars().next().unwrap().len_utf8();
let body: Value = ureq::get(&format!(TATOEBA_URL_ENG_QUERY!(), &query[1..]))
.call()?.into_json()?;
match sentence_search(&options, body, &mut output) { /* Do API request */
Ok(r) => lines_output += r, let body: Value = if query.chars().nth(1).unwrap().len_utf8() == 1 { /* Check if the query is jpn->eng or eng->jpn */
Err(e) => match e { ureq::get(&format!(TATOEBA_URL_ENG_QUERY!(), &query[bytes..]))
-1 => { .call()?.into_json()?
eprintln!("error: invalid json returned"); } else {
return Ok(()); ureq::get(&format!(TATOEBA_URL_JPN_QUERY!(), &query[bytes..]))
}, .call()?.into_json()?
_ => { /* Valid response, but nothing useful */ };
/* Do a jpn->eng request in case input is in japansese */
let body: Value = ureq::get(&format!(TATOEBA_URL_JPN_QUERY!(), &query[1..]))
.call()?.into_json()?;
match sentence_search(&options, body, &mut output) { if let Some(r) = sentence_search(&options, body, &mut output) {
Ok(r) => lines_output += r, lines_output += r;
Err(e) => if e == -1 { } else {
eprintln!("Error: invalid json returned"); eprintln!("error: invalid json returned");
return Ok(()); return Ok(());
}
}
}
}
} }
} else { /* Word search */ } else { /* Word search */
// Do API request // Do API request
let body: Value = ureq::get(&format!(JISHO_URL!(), query)) let body: Value = ureq::get(&format!(JISHO_URL!(), query))

View file

@ -3,17 +3,13 @@ use crate::aux::*;
use serde_json::Value; use serde_json::Value;
use colored::*; use colored::*;
pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> Result<usize, i8>{ pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> Option<usize>{
let mut lines = 0; let mut lines = 0;
let body = value_to_arr({ let body = value_to_arr({
let body = body.get("results"); let body = body.get("results");
if body.is_none() { body?
return Err(-1);
}
body.unwrap()
}); });
let mut i = 1; let mut i = 1;
@ -27,28 +23,23 @@ pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> R
break; break;
} }
/* json nonsense */
let translations = value_to_arr({ let translations = value_to_arr({
let translations = entry.get("translations"); let translations = entry.get("translations");
let translations = value_to_arr(translations?).get(0);
if translations.is_none() { translations?
return Err(-1);
}
let translations = value_to_arr(translations.unwrap()).get(0);
if translations.is_none() {
return Err(-1);
}
translations.unwrap()
}); });
for translation in translations.iter() { for translation in translations.iter() {
let index_str = format!("{}.", i).bright_black(); let index_str = format!("{}.", i).bright_black();
/* prefer to keep japanese sentences on top */ /* Prefer to keep japanese sentences on top */
if entry.get("lang").unwrap() == "eng" { if entry.get("lang")? == "eng" {
*output += &format!("{} {}\n {}\n\n", index_str, value_to_str(translation.get("text").unwrap()).replace("\"", ""), value_to_str(entry.get("text").unwrap()).replace("\"", "")); *output += &format!("{} {}\n {}\n\n", index_str, value_to_str(translation.get("text")?), value_to_str(entry.get("text")?));
} else { } else {
*output += &format!("{} {}\n {}\n\n", index_str, value_to_str(entry.get("text").unwrap()).replace("\"", ""), value_to_str(translation.get("text").unwrap()).replace("\"", "")); *output += &format!("{} {}\n {}\n\n", index_str, value_to_str(entry.get("text")?), value_to_str(translation.get("text")?));
} }
i += 1; i += 1;
@ -56,8 +47,5 @@ pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> R
} }
} }
if !output.is_empty() { Some(lines)
return Ok(lines)
}
Err(1)
} }

View file

@ -3,18 +3,14 @@ use crate::aux::*;
use serde_json::Value; use serde_json::Value;
use colored::*; use colored::*;
pub fn word_search(options: &Options, body: Value, query: &String, mut output: &mut String) -> Option<usize> { pub fn word_search(options: &Options, body: Value, query: &str, output: &mut String) -> Option<usize> {
let mut lines_output = 0; let mut lines_output = 0;
// Try to get the data json-object // Try to get the data json-object
let body = value_to_arr({ let body = value_to_arr({
let body = body.get("data"); let body = body.get("data");
if body.is_none() { body?
return None;
}
body.unwrap()
}); });
/* Iterate over meanings and print them */ /* Iterate over meanings and print them */
@ -22,7 +18,7 @@ pub fn word_search(options: &Options, body: Value, query: &String, mut output: &
if i >= options.limit && options.limit != 0 { if i >= options.limit && options.limit != 0 {
break; break;
} }
if let Some(r) = print_item(&query, entry, &mut output) { if let Some(r) = print_item(query, entry, output) {
lines_output += r; lines_output += r;
} }