1
- use std:: { env, usize } ;
1
+ use std:: env;
2
2
use std:: fmt:: Display ;
3
3
use const_format:: concatcp;
4
4
use inquire:: InquireError ;
@@ -11,16 +11,11 @@ use scraper::{Html, Selector};
11
11
use reqwest:: { self , StatusCode } ;
12
12
use html2text:: config;
13
13
14
- use std:: cell:: LazyCell ;
15
-
16
14
use clap:: { arg, Command } ;
17
15
18
16
const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
19
17
const NAME : & str = env ! ( "CARGO_PKG_NAME" ) ;
20
18
const CLI_USER_AGENT : & str = concatcp ! ( NAME , "/" , VERSION ) ;
21
- const DIV_RESULTS_SELECTOR : LazyCell < Selector > = LazyCell :: new ( || { Selector :: parse ( r#"div[id="resultados"]"# ) . unwrap ( ) } ) ;
22
- const RESULT_OR_SUGGESTION_SELECTOR : LazyCell < Selector > = LazyCell :: new ( || { Selector :: parse ( r#"article, div[class="item-list"]"# ) . unwrap ( ) } ) ;
23
- const OPTIONS_SELECTOR : LazyCell < Selector > = LazyCell :: new ( || { Selector :: parse ( "a" ) . unwrap ( ) } ) ;
24
19
25
20
#[ derive( Debug ) ]
26
21
enum RaeError {
@@ -94,8 +89,10 @@ fn extract_definition(definicion_html: ElementRef) -> RaeResult {
94
89
fn handle_suggestions ( options_list : ElementRef ) -> RaeResult {
95
90
use inquire:: Select ;
96
91
92
+ let options_selector = Selector :: parse ( "a" ) . unwrap ( ) ;
93
+
97
94
let suggestion_list = options_list
98
- . select ( & OPTIONS_SELECTOR )
95
+ . select ( & options_selector )
99
96
. filter_map ( |x| x. text ( ) . next ( ) )
100
97
. collect :: < Vec < & str > > ( ) ;
101
98
@@ -111,7 +108,9 @@ fn handle_suggestions(options_list: ElementRef) -> RaeResult {
111
108
}
112
109
113
110
fn try_get_definition ( page_core : ElementRef ) -> RaeResult {
114
- match page_core. select ( & RESULT_OR_SUGGESTION_SELECTOR ) . next ( ) {
111
+ let result_or_suggestion_selector = Selector :: parse ( r#"article, div[class="item-list"]"# ) . unwrap ( ) ;
112
+
113
+ match page_core. select ( & result_or_suggestion_selector) . next ( ) {
115
114
Some ( w) => match w. value ( ) . name ( ) {
116
115
"article" => extract_definition ( page_core) ,
117
116
"div" => handle_suggestions ( w) ,
@@ -135,10 +134,11 @@ fn buschar_palabra(palabra: &str) -> RaeResult {
135
134
if !response. status ( ) . is_success ( ) {
136
135
Err ( RaeError :: ResponseError ( response. status ( ) ) )
137
136
} else { // I hate it that i have to use else here
137
+ let div_results_selector: Selector = Selector :: parse ( r#"div[id="resultados"]"# ) . unwrap ( ) ;
138
138
let raw_page = response. text ( ) ?;
139
139
let dom_fragment = Html :: parse_document ( & raw_page) ;
140
140
141
- match dom_fragment. select ( & DIV_RESULTS_SELECTOR ) . next ( ) {
141
+ match dom_fragment. select ( & div_results_selector ) . next ( ) {
142
142
Some ( c) => try_get_definition ( c) ,
143
143
_ => Err ( RaeError :: UnexpectedSiteStructure ) ,
144
144
}
0 commit comments