-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added storing/caching some of exchange rates from NBP (#135)
* - 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
Showing
7 changed files
with
3,888 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use std::env; | |
|
||
mod de; | ||
mod logging; | ||
mod nbp; | ||
mod pl; | ||
mod us; | ||
|
||
|
Oops, something went wrong.