tktax-histogram 0.2.2

A Rust crate that provides robust monthly histogram utilities for financial transactions, using specialized types from the TKTAX ecosystem.
Documentation
// ---------------- [ File: tktax-histogram/src/histogram_month.rs ]
crate::ix!();

#[derive(Getters,Debug,PartialEq,Eq)]
#[getset(get="pub")]
pub struct HistogramMonth {
    month_number: u32,
    bins:         Vec<HistogramMonthBin>,
}            

impl HistogramMonth {

    pub fn new(month_number: u32) -> Self {

        assert!(month_number >= 1 && month_number <= 12);

        Self {
            month_number,
            bins:  vec![],
        }
    }

    pub fn push(&mut self, bin: HistogramMonthBin) {
        self.bins.push(bin);
    }

    pub fn month(&self) -> Month {

        Month::from_u32(self.month_number).unwrap()
    }

    pub fn month_name(&self) -> String {

        Month::from_u32(self.month_number).unwrap().name().to_string()
    }

    pub(crate) fn fmt_with(
        &self, 
        f:        &mut fmt::Formatter<'_>, 
        strategy: &HistogramDisplayStrategy) -> fmt::Result 
    {
        match strategy {
            HistogramDisplayStrategy::Short => {
                self.fmt_short(f)
            }
            HistogramDisplayStrategy::ShowTransactionsInBinsWithManyItems { threshold_item_count_in_bin } => {
                self.fmt_and_show_most_populous_bins(f, *threshold_item_count_in_bin)
            }
            HistogramDisplayStrategy::ShowHeavyTransactionsBasedOnPrice { maybe_price_threshold } => {
                self.fmt_and_show_heavy_transactions(f, *maybe_price_threshold)
            }
        }
    }

    fn fmt_short(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

        writeln!(f, "Month: {}", self.month_name())?;

        for bin in self.bins.iter().filter(|bin| bin.len() > 0) 
        {
            bin.fmt_short(f)?;
        }

        write!(f, "")
    }

    fn fmt_and_show_most_populous_bins(
        &self, 
        f: &mut fmt::Formatter<'_>, 
        threshold_item_count_in_bin: usize) -> fmt::Result 
    {
        writeln!(f, "Month: {}", self.month_name())?;

        for bin in self.bins.iter().filter(|bin| bin.len() > 0) 
        {
            bin.fmt_and_show_most_populous_bins(f, threshold_item_count_in_bin)?;
        }

        write!(f, "")
    }

    fn fmt_and_show_heavy_transactions(
        &self, 
        f: &mut fmt::Formatter<'_>, 
        maybe_price_threshold: Option<MonetaryAmount>) -> fmt::Result 
    {
        writeln!(f, "[{}]", self.month_name())?;

        for bin in self.bins.iter().filter(|bin| bin.len() > 0) 
        {
            bin.fmt_and_show_heavy_transactions(f, maybe_price_threshold)?;
        }

        write!(f, "")
    }
}