crate::ix!();
#[derive(Builder,Clone,Getters,Debug,PartialEq,Eq)]
#[getset(get="pub")]
#[builder(setter(into))]
pub struct HistogramMonthBinTxn {
date: NaiveDate,
amount: MonetaryAmount,
description: String,
}
impl fmt::Display for HistogramMonthBinTxn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} {} {}",
self.date,
self.amount,
self.description
)
}
}
#[derive(Builder,Getters,Debug,PartialEq,Eq)]
#[getset(get="pub")]
#[builder(setter(into))]
pub struct HistogramMonthBin {
price_range: Range<MonetaryAmount>,
txns: Vec<HistogramMonthBinTxn>,
}
impl HistogramMonthBin {
pub fn len(&self) -> usize {
self.txns.len()
}
pub fn price_lo(&self) -> MonetaryAmount {
self.price_range.start
}
pub fn price_hi(&self) -> MonetaryAmount {
self.price_range.end
}
pub fn ntx(&self) -> usize {
self.txns.len()
}
pub fn fmt_bin_header(
&self,
f: &mut fmt::Formatter<'_>) -> fmt::Result
{
let price_lo_str = format!("{}", self.price_lo()).trim().to_string();
let price_hi_str = format!("{}", self.price_hi()).trim().to_string();
let range_str = format!("[ {} to {} ]:", price_lo_str, price_hi_str);
let fixed_width = 30;
let range_str_fixed_width = format!("{:width$}", range_str, width = fixed_width);
writeln!(
f,
"{} {:3} transactions",
range_str_fixed_width,
self.ntx()
);
write!(f, "")
}
pub(crate) fn fmt_short(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt_bin_header(f)?;
write!(f, "")
}
pub(crate) fn fmt_and_show_most_populous_bins(
&self,
f: &mut fmt::Formatter<'_>,
threshold_item_count_in_bin: usize) -> fmt::Result
{
self.fmt_bin_header(f)?;
if self.ntx() >= threshold_item_count_in_bin {
for tx in &self.txns {
if !self.price_range.contains(&tx.amount) {
panic!("bad price {} for range {:?}", tx.amount, self.price_range);
}
writeln!(f, " {}", tx)?;
}
}
write!(f, "")
}
pub fn fmt_and_show_heavy_transactions(
&self,
f: &mut fmt::Formatter<'_>,
maybe_price_threshold: Option<MonetaryAmount>) -> fmt::Result
{
self.fmt_bin_header(f)?;
if maybe_price_threshold.is_none() || self.price_lo() >= maybe_price_threshold.unwrap() {
for tx in &self.txns {
if !self.price_range.contains(&tx.amount) {
panic!("bad price {} for range {:?}", tx.amount, self.price_range);
}
writeln!(f, " {}", tx)?;
}
writeln!(f, " ")?;
}
write!(f, "")
}
}