Skip to content

Commit

Permalink
Added storing/caching some of exchange rates from NBP (#135)
Browse files Browse the repository at this point in the history
* - Initil commit with program generating exchange_rates

* - integrated exchange rates (draft)

* - added nbp source

* - Implemented draft

* - Fist UT enabled

* - unit test implemented

* - Added EUR to PLN

* - Extended UT to cover getting EUR from cached exchange rates
  • Loading branch information
jczaja authored Nov 25, 2024
1 parent 3cab75c commit 2fcf301
Show file tree
Hide file tree
Showing 7 changed files with 3,888 additions and 19 deletions.
49 changes: 31 additions & 18 deletions Cargo.lock

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

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ repository = "https://github.com/jczaja/e-trade-tax-return-pl-helper"
homepage = "https://github.com/jczaja/e-trade-tax-return-pl-helper"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "etradeTaxReturnHelper"
path = "src/main.rs"

[[bin]]
name = "gen_exchange_rates"
path = "src/bin/gen_exchange_rates.rs"
required-features = ["gen_exchange_rates"]

[features]
default = ["gui"]
gui = ["fltk"]
gen_exchange_rates = ["serde_json"]

[dependencies]
pdf = "0.7.2"
Expand All @@ -29,3 +39,7 @@ fltk = {version = "=1.3.24", features = ["fltk-bundled"], optional = true}
nom = "7.1.3"
polars = "0.35.4"
csv = "1.3.0"
serde_json = {version = "=1.0.133", optional = true}
holidays = { version = "0.1.0", default-features = false, features = ["PL"] }


11 changes: 11 additions & 0 deletions src/bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Usage
1. Get JSON data with exchange rates. For example USD to PLN throughput year 2024:
```bash
curl https://api.nbp.pl/api/exchangerates/rates/a/usd/2024-01-01/2024-10-31/ > myexchangerates.json
```
2. Run program to get rust source code with embedded exchange rates:
```bash
cargo run --features gen_exchange_rates --bin gen_exchange_rates -- --input myexchangerates.json > myexchange_rates.rs
```
3. Copy generated file to etradeTaxReturnHelper source dir and rebuild project

114 changes: 114 additions & 0 deletions src/bin/gen_exchange_rates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use clap::{App, Arg};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;

#[derive(Deserialize)]
struct Kurs {
no: String,
effectiveDate: String,
mid: f64,
}

#[derive(Deserialize)]
struct Tabela {
table: String,
currency: String,
code: String,
rates: Vec<Kurs>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum Exchange {
EUR(String),
PLN(String),
USD(String),
}

fn main() {
let matches = App::new("gen_exchange_rates")
.version("1.0")
.author("Your Name <[email protected]>")
.about("Consumes NBP exchange rates and produces rust source code with it")
.arg(
Arg::with_name("input")
.short("i")
.long("input")
.value_name("FILE")
.help("Sets the input files")
.takes_value(true)
.multiple(true)
.required(true),
)
.get_matches();

let file_paths = matches.values_of("input").unwrap().collect::<Vec<_>>();
let mut kursy_map: HashMap<Exchange, f64> = HashMap::new();

for file in file_paths {
let file_content =
fs::read_to_string(&file).expect(&format!("Unable to read a file: {file}"));

// Deserializacja JSON do wektora struktur Kurs
let table: Tabela =
serde_json::from_str(&file_content).expect("Unable to parse {file} to JSON format");

// Tworzenie HashMapy
let kursy = table.rates;
match table.code.as_str() {
"USD" => {
for kurs in kursy {
kursy_map.insert(Exchange::USD(kurs.effectiveDate), kurs.mid);
}
}
"EUR" => {
for kurs in kursy {
kursy_map.insert(Exchange::EUR(kurs.effectiveDate), kurs.mid);
}
}
"PLN" => {
for kurs in kursy {
kursy_map.insert(Exchange::PLN(kurs.effectiveDate), kurs.mid);
}
}
_ => {
panic!("Unsupported currency: {}", table.code);
}
}
}

// Generowanie pliku .rs z hashmapą
let mut output_content = String::new();
output_content.push_str("use std::collections::HashMap;\n\n");
output_content.push_str("use etradeTaxReturnHelper::Exchange;\n\n");

output_content.push_str("pub fn get_exchange_rates() -> HashMap<Exchange, f64> {\n");
output_content.push_str(" let mut exchange_rates = HashMap::new();\n");

for (exchange, kurs) in &kursy_map {
match exchange {
Exchange::USD(data) => {
output_content.push_str(&format!(
" exchange_rates.insert(Exchange::USD(\"{}\".to_string()), {});\n",
data, kurs
));
}
Exchange::EUR(data) => {
output_content.push_str(&format!(
" exchange_rates.insert(Exchange::EUR(\"{}\".to_string()), {});\n",
data, kurs
));
}
Exchange::PLN(data) => {
output_content.push_str(&format!(
" exchange_rates.insert(Exchange::PLN(\"{}\".to_string()), {});\n",
data, kurs
));
}
}
}

output_content.push_str(" exchange_rates\n");
output_content.push_str("}\n");
println!("{output_content}");
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::env;

mod de;
mod logging;
mod nbp;
mod pl;
mod us;

Expand Down
Loading

0 comments on commit 2fcf301

Please sign in to comment.