Cleanup.
This commit is contained in:
parent
b3d8a06fdf
commit
220a164c67
4 changed files with 34 additions and 58 deletions
|
@ -7,7 +7,7 @@ use std::{
|
|||
use colored::*;
|
||||
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 aux: HashSet<_> = HashSet::new();
|
||||
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();
|
||||
if rad == '*' || rad == '*' {
|
||||
/* 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() {
|
||||
|
@ -36,7 +36,7 @@ pub fn search_by_radical(mut query: &mut String){
|
|||
for (i, mut rad) in query.clone().chars().skip(2).enumerate() {
|
||||
if rad == '*' || rad == '*' {
|
||||
/* 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() {
|
||||
|
|
42
src/main.rs
42
src/main.rs
|
@ -57,45 +57,37 @@ fn main() -> Result<(), ureq::Error> {
|
|||
}
|
||||
} else {
|
||||
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(());
|
||||
}
|
||||
}
|
||||
query = query.trim().to_string();
|
||||
|
||||
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 */
|
||||
search_by_radical(&mut query);
|
||||
|
||||
} else if query.starts_with('_') || query.starts_with('_'){ /* Sentence search */
|
||||
/* Do API eng->jpn request */
|
||||
let body: Value = ureq::get(&format!(TATOEBA_URL_ENG_QUERY!(), &query[1..]))
|
||||
.call()?.into_json()?;
|
||||
} else if query.starts_with('_') || query.starts_with('_') { /* Sentence search */
|
||||
let bytes = query.chars().next().unwrap().len_utf8();
|
||||
|
||||
match sentence_search(&options, body, &mut output) {
|
||||
Ok(r) => lines_output += r,
|
||||
Err(e) => match e {
|
||||
-1 => {
|
||||
/* Do API request */
|
||||
let body: Value = if query.chars().nth(1).unwrap().len_utf8() == 1 { /* Check if the query is jpn->eng or eng->jpn */
|
||||
ureq::get(&format!(TATOEBA_URL_ENG_QUERY!(), &query[bytes..]))
|
||||
.call()?.into_json()?
|
||||
} else {
|
||||
ureq::get(&format!(TATOEBA_URL_JPN_QUERY!(), &query[bytes..]))
|
||||
.call()?.into_json()?
|
||||
};
|
||||
|
||||
if let Some(r) = sentence_search(&options, body, &mut output) {
|
||||
lines_output += r;
|
||||
} else {
|
||||
eprintln!("error: invalid json returned");
|
||||
return Ok(());
|
||||
},
|
||||
_ => { /* 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) {
|
||||
Ok(r) => lines_output += r,
|
||||
Err(e) => if e == -1 {
|
||||
eprintln!("Error: invalid json returned");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else { /* Word search */
|
||||
// Do API request
|
||||
let body: Value = ureq::get(&format!(JISHO_URL!(), query))
|
||||
|
|
|
@ -3,17 +3,13 @@ use crate::aux::*;
|
|||
use serde_json::Value;
|
||||
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 body = value_to_arr({
|
||||
let body = body.get("results");
|
||||
|
||||
if body.is_none() {
|
||||
return Err(-1);
|
||||
}
|
||||
|
||||
body.unwrap()
|
||||
body?
|
||||
});
|
||||
|
||||
let mut i = 1;
|
||||
|
@ -27,28 +23,23 @@ pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> R
|
|||
break;
|
||||
}
|
||||
|
||||
/* json nonsense */
|
||||
let translations = value_to_arr({
|
||||
let translations = entry.get("translations");
|
||||
let translations = value_to_arr(translations?).get(0);
|
||||
|
||||
if translations.is_none() {
|
||||
return Err(-1);
|
||||
}
|
||||
let translations = value_to_arr(translations.unwrap()).get(0);
|
||||
if translations.is_none() {
|
||||
return Err(-1);
|
||||
}
|
||||
translations.unwrap()
|
||||
translations?
|
||||
});
|
||||
|
||||
|
||||
for translation in translations.iter() {
|
||||
let index_str = format!("{}.", i).bright_black();
|
||||
|
||||
/* prefer to keep japanese sentences on top */
|
||||
if entry.get("lang").unwrap() == "eng" {
|
||||
*output += &format!("{} {}\n {}\n\n", index_str, value_to_str(translation.get("text").unwrap()).replace("\"", ""), value_to_str(entry.get("text").unwrap()).replace("\"", ""));
|
||||
/* Prefer to keep japanese sentences on top */
|
||||
if entry.get("lang")? == "eng" {
|
||||
*output += &format!("{} {}\n {}\n\n", index_str, value_to_str(translation.get("text")?), value_to_str(entry.get("text")?));
|
||||
} 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;
|
||||
|
@ -56,8 +47,5 @@ pub fn sentence_search(options: &Options, body: Value, output: &mut String) -> R
|
|||
}
|
||||
|
||||
}
|
||||
if !output.is_empty() {
|
||||
return Ok(lines)
|
||||
}
|
||||
Err(1)
|
||||
Some(lines)
|
||||
}
|
||||
|
|
|
@ -3,18 +3,14 @@ use crate::aux::*;
|
|||
use serde_json::Value;
|
||||
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;
|
||||
|
||||
// Try to get the data json-object
|
||||
let body = value_to_arr({
|
||||
let body = body.get("data");
|
||||
|
||||
if body.is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
body.unwrap()
|
||||
body?
|
||||
});
|
||||
|
||||
/* 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 {
|
||||
break;
|
||||
}
|
||||
if let Some(r) = print_item(&query, entry, &mut output) {
|
||||
if let Some(r) = print_item(query, entry, output) {
|
||||
lines_output += r;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue