diff --git a/src/overhead_allocation.rs b/src/overhead_allocation.rs index 898a149..7c43661 100644 --- a/src/overhead_allocation.rs +++ b/src/overhead_allocation.rs @@ -4,6 +4,7 @@ use std::{ }; use csv::Reader; +use futures::SinkExt; use itertools::Itertools; use nalgebra::{DMatrix, Dynamic, LU}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; @@ -331,12 +332,12 @@ where .is_some() && !overhead_ccs.contains(&line.department) && (show_from - // When we write out the final amounts rather than changes, - // ensure we still output departments that won't be receiving - // any costs. - || !overhead_other_total - .iter() - .any(|(_, to_department, _)| *to_department == line.department)) + // When we write out the final amounts rather than changes, + // ensure we still output departments that won't be receiving + // any costs. + || !overhead_other_total + .iter() + .any(|(_, to_department, _)| *to_department == line.department)) { if show_from { output.serialize(MovedAmount { @@ -409,7 +410,8 @@ where summed_department_costs: total_cost, }) .collect(), - if show_from { Some(output) } else { None }, + output, + show_from, zero_threshold, )?; @@ -509,7 +511,8 @@ fn remove_quote_and_padding(s: &str) -> String { fn reciprocal_allocation_impl( allocations: Vec, account_costs: Vec, - movement_writer: Option<&mut impl RecordSerializer>, + movement_writer: &mut impl RecordSerializer, + show_from: bool, zero_threshold: f64, ) -> anyhow::Result> { let overhead_department_mappings = get_rules_indexes(&allocations, DepartmentType::Overhead); @@ -546,6 +549,7 @@ fn reciprocal_allocation_impl( overhead_department_mappings, allocations, movement_writer, + show_from, zero_threshold, ) } else { @@ -555,6 +559,7 @@ fn reciprocal_allocation_impl( overhead_department_mappings, allocations, movement_writer, + show_from, zero_threshold, ) } @@ -588,7 +593,8 @@ fn do_solve_reciprocal( account_costs: Vec, overhead_department_mappings: HashMap, allocations: Vec, - temp_writer: Option<&mut impl RecordSerializer>, + temp_writer: &mut impl RecordSerializer, + show_from: bool, zero_threshold: f64, ) -> anyhow::Result> { let operating_department_mappings = get_rules_indexes(&allocations, DepartmentType::Operating); @@ -611,7 +617,7 @@ fn do_solve_reciprocal( overhead_department_mappings.len(), operating_overhead_mappings, ); - if let Some(temp_writer) = temp_writer { + if show_from { solve_reciprocal_with_from( solver, account_costs, @@ -718,10 +724,10 @@ fn solve_reciprocal_no_from( .map(|cost| TotalDepartmentCost { department: cost.department, value: cost.value, // + if new_cost == 0_f64 || diff == 0_f64 { - // 0_f64 - // } else { - // cost.value / new_cost * diff - // }, + // 0_f64 + // } else { + // cost.value / new_cost * diff + // }, }) .filter(|cost| cost.value != 0_f64) .collect(), @@ -778,12 +784,12 @@ fn solve_reciprocal_with_from( temp_writer.serialize(moved_amount)?; } } + temp_writer.flush()?; Ok(()) } #[cfg(test)] mod tests { - use crate::reciprocal_allocation; use crate::AccountCost; use crate::DepartmentType; @@ -871,10 +877,11 @@ mod tests { let result = reciprocal_allocation_impl( allocation_rules, initial_totals, - Some(&mut movement_writer), + &mut movement_writer, + false, 0.00001, ) - .unwrap(); + .unwrap(); assert_eq!(expected_final_allocations, result); }