tktax-amazon 0.2.2

High-performance crate for parsing, fusing, and exporting Amazon transaction data for financial recordkeeping.
Documentation
// ---------------- [ File: tktax-amazon/src/amazon_csv.rs ]
crate::ix!();

pub trait AmazonTxn {}

pub fn parse_amazon_csv<A: AmazonTxn + for<'a> Deserialize<'a>>(file_contents: &[u8]) 
-> Result<Vec<A>, AmazonError> 
{
    let input_reader = Cursor::new(file_contents);

    let mut reader = ReaderBuilder::new()
        .has_headers(true)
        .flexible(true)
        .quote(b'"')
        .escape(Some(b'"'))
        .terminator(csv::Terminator::Any(b'\n'))
        .from_reader(input_reader);

    let mut transactions = Vec::new();

    for result in reader.byte_records() {
        let byte_record = result?;
        let record = byte_record.deserialize::<A>(None)?;
        transactions.push(record);
    }

    Ok(transactions)
}

pub fn fuse_amazon_transactions_from_two_epochs(
    epoch1: Vec<AmazonTx1>, 
    epoch2: Vec<AmazonTx2>) 
    -> Vec<AmazonTx> 
{
    let mut txns1 = epoch1.iter().map(|tx| AmazonTx::from(tx)).collect::<Vec<_>>();
    let mut txns2 = epoch2.iter().map(|tx| AmazonTx::from(tx)).collect::<Vec<_>>();

    txns1.extend(txns2);

    txns1
}

/// whether this does anything depends what we have in our config.toml
///
pub fn maybe_fuse_amazon_csv_from_two_epochs(config: &AmazonConfig) -> Result<(),AmazonError>
{
    let contents1 = std::fs::read(&config.epoch1file())?;
    let contents2 = std::fs::read(&config.epoch2file())?;

    let txns1 = parse_amazon_csv::<AmazonTx1>(&contents1)?;
    let txns2 = parse_amazon_csv::<AmazonTx2>(&contents2)?;

    let fused_txns = fuse_amazon_transactions_from_two_epochs(txns1, txns2);

    write_to_csv(fused_txns, config.txfile());

    Ok(())
}