tktax_histogram/
histogram_month.rs1crate::ix!();
3
4#[derive(Getters,Debug,PartialEq,Eq)]
5#[getset(get="pub")]
6pub struct HistogramMonth {
7 month_number: u32,
8 bins: Vec<HistogramMonthBin>,
9}
10
11impl HistogramMonth {
12
13 pub fn new(month_number: u32) -> Self {
14
15 assert!(month_number >= 1 && month_number <= 12);
16
17 Self {
18 month_number,
19 bins: vec![],
20 }
21 }
22
23 pub fn push(&mut self, bin: HistogramMonthBin) {
24 self.bins.push(bin);
25 }
26
27 pub fn month(&self) -> Month {
28
29 Month::from_u32(self.month_number).unwrap()
30 }
31
32 pub fn month_name(&self) -> String {
33
34 Month::from_u32(self.month_number).unwrap().name().to_string()
35 }
36
37 pub(crate) fn fmt_with(
38 &self,
39 f: &mut fmt::Formatter<'_>,
40 strategy: &HistogramDisplayStrategy) -> fmt::Result
41 {
42 match strategy {
43 HistogramDisplayStrategy::Short => {
44 self.fmt_short(f)
45 }
46 HistogramDisplayStrategy::ShowTransactionsInBinsWithManyItems { threshold_item_count_in_bin } => {
47 self.fmt_and_show_most_populous_bins(f, *threshold_item_count_in_bin)
48 }
49 HistogramDisplayStrategy::ShowHeavyTransactionsBasedOnPrice { maybe_price_threshold } => {
50 self.fmt_and_show_heavy_transactions(f, *maybe_price_threshold)
51 }
52 }
53 }
54
55 fn fmt_short(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56
57 writeln!(f, "Month: {}", self.month_name())?;
58
59 for bin in self.bins.iter().filter(|bin| bin.len() > 0)
60 {
61 bin.fmt_short(f)?;
62 }
63
64 write!(f, "")
65 }
66
67 fn fmt_and_show_most_populous_bins(
68 &self,
69 f: &mut fmt::Formatter<'_>,
70 threshold_item_count_in_bin: usize) -> fmt::Result
71 {
72 writeln!(f, "Month: {}", self.month_name())?;
73
74 for bin in self.bins.iter().filter(|bin| bin.len() > 0)
75 {
76 bin.fmt_and_show_most_populous_bins(f, threshold_item_count_in_bin)?;
77 }
78
79 write!(f, "")
80 }
81
82 fn fmt_and_show_heavy_transactions(
83 &self,
84 f: &mut fmt::Formatter<'_>,
85 maybe_price_threshold: Option<MonetaryAmount>) -> fmt::Result
86 {
87 writeln!(f, "[{}]", self.month_name())?;
88
89 for bin in self.bins.iter().filter(|bin| bin.len() > 0)
90 {
91 bin.fmt_and_show_heavy_transactions(f, maybe_price_threshold)?;
92 }
93
94 write!(f, "")
95 }
96}