Skip to content

Commit 8f26b6b

Browse files
committed
Support Spanish IB annual report HTML parsing.
Add support for Interactive brokers HTML annual report in Spanish. Removing once_cell crate and use standard one.
1 parent c67b516 commit 8f26b6b

File tree

5 files changed

+169
-159
lines changed

5 files changed

+169
-159
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "burocratin"
33
version = "0.2.0"
44
authors = ["Jorge Perez Burgos <[email protected]>"]
55
edition = "2021"
6+
rust-version = "1.80"
67

78
[lib]
89
crate-type = ["cdylib", "rlib"]
@@ -36,7 +37,6 @@ js-sys = "0.3"
3637
log = "0.4"
3738
nom = "6.0"
3839
num-format = "0.4.4"
39-
once_cell = "1.5"
4040
pdf-extract = "0.7"
4141
rust_decimal = "1.7"
4242
scraper = "0.12"

src/css.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use dominator::{class, pseudo};
2-
use once_cell::sync::Lazy;
2+
use std::sync::LazyLock;
33

4-
pub static ROOT_CLASS: Lazy<String> = Lazy::new(|| {
4+
pub static ROOT_CLASS: LazyLock<String> = LazyLock::new(|| {
55
class! {
66
.style("padding", "10px")
77
}
88
});
99

10-
pub static ERROR_PARAGRAPH_CLASS: Lazy<String> = Lazy::new(|| {
10+
pub static ERROR_PARAGRAPH_CLASS: LazyLock<String> = LazyLock::new(|| {
1111
class! {
1212
.style("color", "#ba3939")
1313
.style("background", "#ffe0e0")
@@ -16,31 +16,31 @@ pub static ERROR_PARAGRAPH_CLASS: Lazy<String> = Lazy::new(|| {
1616
}
1717
});
1818

19-
pub static FLEX_CONTAINER_CLASS: Lazy<String> = Lazy::new(|| {
19+
pub static FLEX_CONTAINER_CLASS: LazyLock<String> = LazyLock::new(|| {
2020
class! {
2121
.style("display", "flex")
2222
.style("flex-flow", "wrap")
2323
.style("gap", "2px")
2424
}
2525
});
2626

27-
pub static FLEX_CONTAINER_ITEM_20_CLASS: Lazy<String> = Lazy::new(|| {
27+
pub static FLEX_CONTAINER_ITEM_20_CLASS: LazyLock<String> = LazyLock::new(|| {
2828
class! {
2929
.style("flex", "auto")
3030
.style("font-size", "small")
3131
// .style("max-width", "20%")
3232
}
3333
});
3434

35-
pub static FLEX_CONTAINER_ITEM_40_CLASS: Lazy<String> = Lazy::new(|| {
35+
pub static FLEX_CONTAINER_ITEM_40_CLASS: LazyLock<String> = LazyLock::new(|| {
3636
class! {
3737
.style("flex", "40%")
3838
.style("max-width", "40%")
3939
.style("margin", "5px")
4040
}
4141
});
4242

43-
pub static SECTION_HEADER: Lazy<String> = Lazy::new(|| {
43+
pub static SECTION_HEADER: LazyLock<String> = LazyLock::new(|| {
4444
class! {
4545
.style("display", "flex")
4646
.style("flex-direction", "row")
@@ -62,7 +62,7 @@ pub static SECTION_HEADER: Lazy<String> = Lazy::new(|| {
6262
}
6363
});
6464

65-
pub static TABLE_STYLE: Lazy<String> = Lazy::new(|| {
65+
pub static TABLE_STYLE: LazyLock<String> = LazyLock::new(|| {
6666
class! {
6767
.style("overflow", "auto")
6868
.style("width", "100%")
@@ -73,20 +73,20 @@ pub static TABLE_STYLE: Lazy<String> = Lazy::new(|| {
7373
}
7474
});
7575

76-
pub static TABLE_CAPTION: Lazy<String> = Lazy::new(|| {
76+
pub static TABLE_CAPTION: LazyLock<String> = LazyLock::new(|| {
7777
class! {
7878
.style("font-size", "large")
7979
.style("margin", "20px")
8080
}
8181
});
8282

83-
pub static TABLE_HEADER: Lazy<String> = Lazy::new(|| {
83+
pub static TABLE_HEADER: LazyLock<String> = LazyLock::new(|| {
8484
class! {
8585
.style("font-size", "small")
8686
}
8787
});
8888

89-
pub static TABLE_ROW: Lazy<String> = Lazy::new(|| {
89+
pub static TABLE_ROW: LazyLock<String> = LazyLock::new(|| {
9090
class! {
9191
.style("font-size", "small")
9292
.pseudo!(":nth-child(even)", {

src/parsers/ib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{collections::HashMap, str::FromStr, sync::Arc};
1+
use std::{
2+
collections::{HashMap, HashSet},
3+
str::FromStr,
4+
sync::{Arc, LazyLock},
5+
};
26

37
use crate::{
48
data::{
@@ -10,23 +14,24 @@ use crate::{
1014
};
1115
use anyhow::{anyhow, bail, Result};
1216
use chrono::NaiveDate;
13-
use once_cell::sync::Lazy;
1417
use rust_decimal::Decimal;
1518
use scraper::{node::Element, ElementRef, Html, Selector};
1619
use selectors::attr::CaseSensitivity;
1720

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());
2023

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());
2326

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());
2629

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());
3035

3136
enum NoteState {
3237
Invalid,
@@ -41,8 +46,10 @@ pub struct IBParser {
4146
companies_info: HashMap<String, CompanyInfo>,
4247
}
4348

49+
static STOCKS_STRS: LazyLock<HashSet<Option<&'static str>>> =
50+
LazyLock::new(|| HashSet::from([Some("Stocks"), Some("Acciones")]));
51+
4452
impl IBParser {
45-
const STOCKS_STR: &'static str = "Stocks";
4653
const EUR_CURRENCY_STR: &'static str = "EUR";
4754

4855
pub fn new(data: &str, broker: &Arc<BrokerInformation>) -> Result<Self> {
@@ -137,7 +144,7 @@ impl IBParser {
137144
match state {
138145
NoteState::Invalid => {
139146
log::debug!("Invalid state");
140-
if table_row.text().next() == Some(IBParser::STOCKS_STR) {
147+
if STOCKS_STRS.contains(&table_row.text().next()) {
141148
state = NoteState::Stocks;
142149
}
143150
}
@@ -201,8 +208,7 @@ impl IBParser {
201208

202209
if let Some(element) = table_row.first_child().unwrap().value().as_element() {
203210
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());
206212
continue;
207213
}
208214
}
@@ -293,7 +299,7 @@ impl IBParser {
293299
match state {
294300
NoteState::Invalid => {
295301
log::debug!("Invalid state");
296-
if table_row.text().next() == Some(IBParser::STOCKS_STR) {
302+
if STOCKS_STRS.contains(&table_row.text().next()) {
297303
state = NoteState::Stocks;
298304
}
299305
}

src/parsers/ib_csv.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ impl IBCSVParser {
282282
mod tests {
283283
use super::*;
284284

285+
#[ctor::ctor]
286+
fn init() {
287+
let _ = env_logger::builder().is_test(true).try_init();
288+
}
289+
285290
fn compare_vectors_by_item<T>(vec1: &[T], vec2: &[T])
286291
where
287292
T: std::fmt::Debug + std::cmp::PartialEq,

0 commit comments

Comments
 (0)