46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
use rust_decimal::Decimal;
|
|
use std::collections::HashMap;
|
|
|
|
pub struct CommoditiesPriceOracle {
|
|
commodities: HashMap<String, HashMap<String, Decimal>>,
|
|
}
|
|
|
|
impl CommoditiesPriceOracle {
|
|
pub fn new() -> CommoditiesPriceOracle {
|
|
CommoditiesPriceOracle {
|
|
commodities: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn insert(&mut self, commodity: String, date: String, price: Decimal) {
|
|
match self.commodities.contains_key(&commodity) {
|
|
false => {
|
|
self.commodities.insert(commodity.clone(), HashMap::new());
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
println!("inserting {} {} {}", commodity, date, price);
|
|
self.commodities
|
|
.get_mut(&commodity)
|
|
.unwrap()
|
|
.insert(date, price);
|
|
}
|
|
|
|
pub fn lookup(&self, commodity: &String, date: &String) -> Decimal {
|
|
match self.commodities.get(commodity) {
|
|
None => {
|
|
println!("no price history data for commodity: {}", commodity);
|
|
return Decimal::new(1, 0);
|
|
}
|
|
Some(price_history) => match price_history.get(date) {
|
|
None => {
|
|
println!("[WARNING] no date history for {} on {}", commodity, date);
|
|
return Decimal::new(0, 0);
|
|
}
|
|
Some(price) => price.clone(),
|
|
},
|
|
}
|
|
}
|
|
}
|