Skip to content

Commit

Permalink
chore: better build, code celanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nanov committed Nov 18, 2024
1 parent bab7c92 commit f53f77d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rae-cli"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand All @@ -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
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, usize};
use std::env;
use std::fmt::Display;
use const_format::concatcp;
use inquire::InquireError;
Expand All @@ -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<Selector> = LazyCell::new(|| { Selector::parse(r#"div[id="resultados"]"#).unwrap() });
const RESULT_OR_SUGGESTION_SELECTOR: LazyCell<Selector> = LazyCell::new(|| { Selector::parse(r#"article, div[class="item-list"]"#).unwrap() });
const OPTIONS_SELECTOR: LazyCell<Selector> = LazyCell::new(|| { Selector::parse("a").unwrap() });

#[derive(Debug)]
enum RaeError {
Expand Down Expand Up @@ -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::<Vec<&str>>();

Expand All @@ -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),
Expand All @@ -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),
}
Expand Down

0 comments on commit f53f77d

Please sign in to comment.