Skip to content

Commit

Permalink
Adapt to 2024 report changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vaijira committed Jan 6, 2025
1 parent 4bc7223 commit 9169083
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/parsers/ib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static THEAD_TH_TR_SELECTOR: LazyLock<Selector> =
LazyLock::new(|| Selector::parse(r#"thead tr"#).unwrap());
static TBODY_TR_SELECTOR: LazyLock<Selector> =
LazyLock::new(|| Selector::parse(r#"tbody tr"#).unwrap());
static TBODY_ACCOUNT_TR_SELECTOR: LazyLock<Selector> =
LazyLock::new(|| Selector::parse(r#"tbody tr:not(.subtotal):not(.total)"#).unwrap());
static TR_SELECTOR: LazyLock<Selector> = LazyLock::new(|| Selector::parse(r#"tr"#).unwrap());

enum NoteState {
Expand Down Expand Up @@ -140,7 +142,7 @@ impl IBParser {
}
}

for table_row in transactions.select(&TBODY_TR_SELECTOR) {
for table_row in transactions.select(&TBODY_ACCOUNT_TR_SELECTOR) {
match state {
NoteState::Invalid => {
log::debug!("Invalid state");
Expand Down Expand Up @@ -168,22 +170,32 @@ impl IBParser {
}
NoteState::Note => {
log::debug!("Note state");
let has_class = |x: &Element| {
let has_asset_class = |x: &Element| {
x.has_class("header-asset", CaseSensitivity::AsciiCaseInsensitive)
};
let element = table_row.value();
let has_currency_class = |x: &Element| {
x.has_class("header-currency", CaseSensitivity::AsciiCaseInsensitive)
};
// let element = table_row.value();

if element.has_class("row-summary", CaseSensitivity::AsciiCaseInsensitive) {
result.push(self.parse_account_note(&table_row, with_account_field)?);
} else if table_row
if table_row
.first_child()
.map(|x| x.value())
.unwrap()
.as_element()
.map(has_class)
.map(has_asset_class)
== Some(true)
{
state = NoteState::Invalid;
} else if table_row
.first_child()
.map(|x| x.value())
.unwrap()
.as_element()
.map(has_currency_class)
!= Some(true)
{
result.push(self.parse_account_note(&table_row, with_account_field)?);
}
}
NoteState::Total => {
Expand Down
8 changes: 6 additions & 2 deletions src/parsers/ib_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ impl IBCSVParser {
const TRADE_BEGIN__NO_ACCOUNT_STR: usize = 7;
const TRADE_END_STR: usize = 8;
const TRADE_STOCK_STR: usize = 9;
const STOCK_COMPANY_INFO_SECTOR_START_OLD_STR: usize = 10;

const ES_HEADER_CONTENT: &str = "Statement,Header,Nombre del campo,Valor del campo";

Check warning on line 42 in src/parsers/ib_csv.rs

View workflow job for this annotation

GitHub Actions / Format & Clippy (1.80.1)

`&` without an explicit lifetime name cannot be used here

Check warning on line 42 in src/parsers/ib_csv.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

`&` without an explicit lifetime name cannot be used here

Check warning on line 42 in src/parsers/ib_csv.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

`&` without an explicit lifetime name cannot be used here

const EN_MSGS: &'static [&'static str] = &[
"Financial Instrument Information,Header,Asset Category,Symbol,Description,Conid,Security ID,Listing Exch,Multiplier,Type,Code", // STOCK_COMPANY_INFO_SECTOR_START_STR
"Financial Instrument Information,Header,Asset Category,Symbol,Description,Conid,Security ID,Underlying,Listing Exch,Multiplier,Type,Code", // STOCK_COMPANY_INFO_SECTOR_START_STR
"Financial Instrument Information,Data,Stocks,", // STOCK_COMPANY_INFO_SECTOR_END_STR
"Open Positions,Header,DataDiscriminator,Asset Category,Currency,Symbol,Quantity,Mult,Cost Price,Cost Basis,Close Price,Value,Unrealized P/L,Code", // OPEN_POSITIONS_BEGIN_STR
"Open Positions,Total,,Stocks,EUR,", // OPEN_POSITIONS_END_STR
Expand All @@ -50,7 +51,8 @@ impl IBCSVParser {
"Trades,Header,DataDiscriminator,Asset Category,Currency,Account,Symbol,Date/Time,Quantity,T. Price,C. Price,Proceeds,Comm/Fee,Basis,Realized P/L,MTM P/L,Code", // TRADE_BEGIN_STR
"Trades,Header,DataDiscriminator,Asset Category,Currency,Symbol,Date/Time,Quantity,T. Price,C. Price,Proceeds,Comm/Fee,Basis,Realized P/L,MTM P/L,Code", // TRADE_BEGIN_NO_ACCOUNT_STR
"Trades,Total,", // TRADE_END_STR
"Trades,Data,Order,Stocks," // TRADE_STOCK_STR
"Trades,Data,Order,Stocks,", // TRADE_STOCK_STR
"Financial Instrument Information,Header,Asset Category,Symbol,Description,Conid,Security ID,Listing Exch,Multiplier,Type,Code", // STOCK_COMPANY_INFO_SECTOR_START_OLD_STR
];

const ES_MSGS: &'static [&'static str] = &[
Expand All @@ -64,6 +66,7 @@ impl IBCSVParser {
"Operaciones,Header,DataDiscriminator,Categoría de activo,Divisa,Símbolo,Fecha/Hora,Cantidad,Precio trans.,Precio de cier.,Productos,Tarifa/com.,Básico,PyG realizadas,MTM P/G,Código", // TRADE_BEGIN_NO_ACCOUNT_STR
"Operaciones,Total,", // TRADE_END_STR
"Operaciones,Data,Order,Acciones,", // TRADE_STOCK_STR
"Información de instrumento financiero,Header,Categoría de activo,Símbolo,Descripción,Conid,Id. de seguridad,Merc. de cotización,Multiplicador,Tipo,Código", // STOCK_COMPANY_INFO_SECTOR_START_OLD_STR
];

fn parse_companies_info(
Expand All @@ -75,6 +78,7 @@ impl IBCSVParser {

let start = content
.find(locale[IBCSVParser::STOCK_COMPANY_INFO_SECTOR_START_STR])
.or_else(|| content.find(locale[IBCSVParser::STOCK_COMPANY_INFO_SECTOR_START_OLD_STR]))
.ok_or_else(|| anyhow!("Not found beginning of companies info section"))?;

let end_left = content
Expand Down

0 comments on commit 9169083

Please sign in to comment.