use bitcode::{Decode, Encode};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Encode, Decode, Clone)]
pub struct Transaction {
pub quantity: i32,
pub price: f64,
}
impl Transaction {
pub fn new(quantity: i32, price: f64) -> Self {
Transaction { quantity, price }
}
pub fn from_bin(bytes: &[u8]) -> Self {
bitcode::decode(bytes).unwrap()
}
pub fn from_csv(csv: &str) -> Self {
let parts: Vec<&str> = csv.split(';').collect();
let quantity: i32 = parts[0].parse().unwrap();
let price: f64 = parts[1].parse().unwrap();
Transaction { quantity, price }
}
pub fn to_bin(&self) -> Vec<u8> {
bitcode::encode(self)
}
pub fn to_csv(&self) -> String {
format!("{};{};", self.quantity, self.price)
}
pub fn to_hash_map(&self) -> HashMap<&str, String> {
let mut hm = HashMap::new();
hm.insert("quantity", self.quantity.to_string());
hm.insert("price", self.price.to_string());
hm
}
pub fn value(&self) -> f64 {
self.price * self.quantity as f64
}
}
impl std::fmt::Display for Transaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Transaction={}x{}", self.quantity, self.price)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new() {
let t = Transaction::new(10, 325.5);
assert_eq!(t.quantity, 10);
assert_eq!(t.price, 325.5);
}
#[test]
fn csv() {
let t = Transaction::new(10, 325.5);
let csv = t.to_csv();
assert_eq!(csv, "10;325.5;");
let from_csv = Transaction::from_csv(&csv);
assert_eq!(t, from_csv);
}
#[test]
fn bin() {
let t = Transaction::new(10, 325.5);
let bytes = t.to_bin();
let decoded = Transaction::from_bin(&bytes);
assert_eq!(t, decoded);
}
}