1
- use std:: { collections:: HashMap , str:: FromStr , sync:: Arc } ;
1
+ use std:: {
2
+ collections:: { HashMap , HashSet } ,
3
+ str:: FromStr ,
4
+ sync:: { Arc , LazyLock } ,
5
+ } ;
2
6
3
7
use crate :: {
4
8
data:: {
@@ -10,23 +14,24 @@ use crate::{
10
14
} ;
11
15
use anyhow:: { anyhow, bail, Result } ;
12
16
use chrono:: NaiveDate ;
13
- use once_cell:: sync:: Lazy ;
14
17
use rust_decimal:: Decimal ;
15
18
use scraper:: { node:: Element , ElementRef , Html , Selector } ;
16
19
use selectors:: attr:: CaseSensitivity ;
17
20
18
- static OPEN_POSITIONS_SELECTOR : Lazy < Selector > =
19
- Lazy :: new ( || Selector :: parse ( r#"div[id^="tblOpenPositions_"] div table"# ) . unwrap ( ) ) ;
21
+ static OPEN_POSITIONS_SELECTOR : LazyLock < Selector > =
22
+ LazyLock :: new ( || Selector :: parse ( r#"div[id^="tblOpenPositions_"] div table"# ) . unwrap ( ) ) ;
20
23
21
- static CONTRACT_INFO_SELECTOR : Lazy < Selector > =
22
- Lazy :: new ( || Selector :: parse ( r#"div[id^="tblContractInfo"] div table"# ) . unwrap ( ) ) ;
24
+ static CONTRACT_INFO_SELECTOR : LazyLock < Selector > =
25
+ LazyLock :: new ( || Selector :: parse ( r#"div[id^="tblContractInfo"] div table"# ) . unwrap ( ) ) ;
23
26
24
- static TRANSACTIONS_SELECTOR : Lazy < Selector > =
25
- Lazy :: new ( || Selector :: parse ( r#"div[id^="tblTransactions_"] div table"# ) . unwrap ( ) ) ;
27
+ static TRANSACTIONS_SELECTOR : LazyLock < Selector > =
28
+ LazyLock :: new ( || Selector :: parse ( r#"div[id^="tblTransactions_"] div table"# ) . unwrap ( ) ) ;
26
29
27
- static THEAD_TH_TR_SELECTOR : Lazy < Selector > = Lazy :: new ( || Selector :: parse ( r#"thead tr"# ) . unwrap ( ) ) ;
28
- static TBODY_TR_SELECTOR : Lazy < Selector > = Lazy :: new ( || Selector :: parse ( r#"tbody tr"# ) . unwrap ( ) ) ;
29
- static TR_SELECTOR : Lazy < Selector > = Lazy :: new ( || Selector :: parse ( r#"tr"# ) . unwrap ( ) ) ;
30
+ static THEAD_TH_TR_SELECTOR : LazyLock < Selector > =
31
+ LazyLock :: new ( || Selector :: parse ( r#"thead tr"# ) . unwrap ( ) ) ;
32
+ static TBODY_TR_SELECTOR : LazyLock < Selector > =
33
+ LazyLock :: new ( || Selector :: parse ( r#"tbody tr"# ) . unwrap ( ) ) ;
34
+ static TR_SELECTOR : LazyLock < Selector > = LazyLock :: new ( || Selector :: parse ( r#"tr"# ) . unwrap ( ) ) ;
30
35
31
36
enum NoteState {
32
37
Invalid ,
@@ -41,8 +46,10 @@ pub struct IBParser {
41
46
companies_info : HashMap < String , CompanyInfo > ,
42
47
}
43
48
49
+ static STOCKS_STRS : LazyLock < HashSet < Option < & ' static str > > > =
50
+ LazyLock :: new ( || HashSet :: from ( [ Some ( "Stocks" ) , Some ( "Acciones" ) ] ) ) ;
51
+
44
52
impl IBParser {
45
- const STOCKS_STR : & ' static str = "Stocks" ;
46
53
const EUR_CURRENCY_STR : & ' static str = "EUR" ;
47
54
48
55
pub fn new ( data : & str , broker : & Arc < BrokerInformation > ) -> Result < Self > {
@@ -137,7 +144,7 @@ impl IBParser {
137
144
match state {
138
145
NoteState :: Invalid => {
139
146
log:: debug!( "Invalid state" ) ;
140
- if table_row. text ( ) . next ( ) == Some ( IBParser :: STOCKS_STR ) {
147
+ if STOCKS_STRS . contains ( & table_row. text ( ) . next ( ) ) {
141
148
state = NoteState :: Stocks ;
142
149
}
143
150
}
@@ -201,8 +208,7 @@ impl IBParser {
201
208
202
209
if let Some ( element) = table_row. first_child ( ) . unwrap ( ) . value ( ) . as_element ( ) {
203
210
if element. has_class ( "header-asset" , CaseSensitivity :: AsciiCaseInsensitive ) {
204
- start_parsing_symbols =
205
- table_row. text ( ) . next ( ) == Some ( IBParser :: STOCKS_STR ) ;
211
+ start_parsing_symbols = STOCKS_STRS . contains ( & table_row. text ( ) . next ( ) ) ;
206
212
continue ;
207
213
}
208
214
}
@@ -293,7 +299,7 @@ impl IBParser {
293
299
match state {
294
300
NoteState :: Invalid => {
295
301
log:: debug!( "Invalid state" ) ;
296
- if table_row. text ( ) . next ( ) == Some ( IBParser :: STOCKS_STR ) {
302
+ if STOCKS_STRS . contains ( & table_row. text ( ) . next ( ) ) {
297
303
state = NoteState :: Stocks ;
298
304
}
299
305
}
0 commit comments