pub mod extensions;
use std::path::PathBuf;
use serde::Deserialize;
#[allow(unused)]
#[derive(Debug, Default, Deserialize)]
pub struct SymbolMetadata {
pub namespace: Option<String>,
pub symbol: String,
pub currency: Option<String>,
pub updater: Option<String>,
pub updater_symbol: Option<String>,
pub ledger_symbol: Option<String>,
pub ib_symbol: Option<String>,
pub remarks: Option<String>
}
#[allow(unused)]
impl SymbolMetadata {
pub fn new() -> Self {
Self::default()
}
pub fn get_symbol(&self) -> String {
match &self.ledger_symbol {
Some(ls) => ls.to_owned(),
None => self.symbol.to_owned(),
}
}
pub fn symbol_w_namespace(&self) -> String {
match &self.namespace {
Some(namespace) => format!("{}:{}", namespace, self.symbol),
None => self.symbol.to_string(),
}
}
}
pub fn read_symbols(path: &PathBuf) -> anyhow::Result<Vec<SymbolMetadata>> {
let mut rdr = csv::Reader::from_path(path)?;
let collection: Vec<SymbolMetadata> = rdr.deserialize()
.map(|result| match result {
Ok(symbol) => symbol,
Err(e) => panic!("Error deserializing: {e}"),
})
.collect();
Ok(collection)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::read_symbols;
#[test_log::test]
fn read_test_file() {
let path = PathBuf::from("tests/dummy.csv");
println!("path: {:?}", path);
let result = read_symbols(&path);
assert!(result.is_ok());
let actual = result.expect("parsed file");
log::debug!("parsed: {:?}", actual);
assert!(!actual.is_empty());
}
#[test_log::test]
fn test_parse() {
let path = PathBuf::from("tests/dummy.csv");
let list = read_symbols(&path).expect("parsed");
assert_eq!(2, list.len());
let actual = &list[0];
assert_eq!("AUD", actual.symbol);
}
#[test_log::test]
#[should_panic(expected="Error deserializing: CSV error: record 3 (line: 3, byte: 165): found record with 7 fields, but the previous record has 8 fields")]
fn test_parsing_error() {
let path = PathBuf::from("tests/dummy2.csv");
let list = read_symbols(&path).expect("parsed");
assert!(!list.is_empty());
}
#[test]
fn test_get_symbol() {
let path = PathBuf::from("tests/dummy.csv");
let result = read_symbols(&path);
let symbols = result.expect("parsed");
let record = symbols.iter().find(|&symbol| symbol.symbol == "EL4X")
.expect("found");
let actual = record.get_symbol();
assert_eq!("EL4X_DE", actual);
assert_eq!("EL4X", record.symbol);
assert_eq!(Some("EL4X_DE".to_owned()), record.ledger_symbol);
}
}