From f53f77d748a5fbe02e45357e6952f399bf95f318 Mon Sep 17 00:00:00 2001 From: nanov Date: Mon, 18 Nov 2024 19:50:52 +0200 Subject: [PATCH] chore: better build, code celanup --- Cargo.lock | 2 +- Cargo.toml | 5 ++++- src/main.rs | 18 +++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 146f5ff..e98d85d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1118,7 +1118,7 @@ dependencies = [ [[package]] name = "rae-cli" -version = "0.1.0" +version = "0.1.1" dependencies = [ "clap", "const_format", diff --git a/Cargo.toml b/Cargo.toml index 195252b..70f46d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rae-cli" -version = "0.1.0" +version = "0.1.1" edition = "2021" [dependencies] @@ -12,3 +12,6 @@ reqwest = { version = "0.12.8", features = ["blocking"] } scraper = "0.20.0" terminal-menu = "3.0.0" termsize = "0.1.9" + +[profile.release] +lto = true diff --git a/src/main.rs b/src/main.rs index 85bb4a8..332b756 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, usize}; +use std::env; use std::fmt::Display; use const_format::concatcp; use inquire::InquireError; @@ -11,16 +11,11 @@ use scraper::{Html, Selector}; use reqwest::{self, StatusCode}; use html2text::config; -use std::cell::LazyCell; - use clap::{arg, Command}; const VERSION: &str = env!("CARGO_PKG_VERSION"); const NAME: &str = env!("CARGO_PKG_NAME"); const CLI_USER_AGENT: &str = concatcp!(NAME, "/", VERSION); -const DIV_RESULTS_SELECTOR: LazyCell = LazyCell::new(|| { Selector::parse(r#"div[id="resultados"]"#).unwrap() }); -const RESULT_OR_SUGGESTION_SELECTOR: LazyCell = LazyCell::new(|| { Selector::parse(r#"article, div[class="item-list"]"#).unwrap() }); -const OPTIONS_SELECTOR: LazyCell = LazyCell::new(|| { Selector::parse("a").unwrap() }); #[derive(Debug)] enum RaeError { @@ -94,8 +89,10 @@ fn extract_definition(definicion_html: ElementRef) -> RaeResult { fn handle_suggestions(options_list: ElementRef) -> RaeResult { use inquire::Select; + let options_selector = Selector::parse("a").unwrap(); + let suggestion_list = options_list - .select(&OPTIONS_SELECTOR) + .select(&options_selector) .filter_map(|x| x.text().next()) .collect::>(); @@ -111,7 +108,9 @@ fn handle_suggestions(options_list: ElementRef) -> RaeResult { } fn try_get_definition(page_core: ElementRef) -> RaeResult { - match page_core.select(&RESULT_OR_SUGGESTION_SELECTOR).next() { + let result_or_suggestion_selector = Selector::parse(r#"article, div[class="item-list"]"#).unwrap(); + + match page_core.select(&result_or_suggestion_selector).next() { Some(w) => match w.value().name() { "article" => extract_definition(page_core), "div" => handle_suggestions(w), @@ -135,10 +134,11 @@ fn buschar_palabra(palabra: &str) -> RaeResult { if !response.status().is_success() { Err(RaeError::ResponseError(response.status())) } else { // I hate it that i have to use else here + let div_results_selector:Selector = Selector::parse(r#"div[id="resultados"]"#).unwrap(); let raw_page = response.text()?; let dom_fragment = Html::parse_document(&raw_page); - match dom_fragment.select(&DIV_RESULTS_SELECTOR).next() { + match dom_fragment.select(&div_results_selector).next() { Some(c) => try_get_definition(c), _ => Err(RaeError::UnexpectedSiteStructure), }