Skip to content

Commit ea17f45

Browse files
committed
Add AppraisalOutput::is_valid helper to check whether output should be filtered
1 parent a6311af commit ea17f45

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/simulation/investment/appraisal.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,23 @@ impl AppraisalOutput {
9191
/// depending on the user's platform (e.g. macOS ARM vs. Windows). We want to avoid this, if
9292
/// possible, which is why we use a more approximate comparison.
9393
pub fn compare_metric(&self, other: &Self) -> Ordering {
94+
assert!(
95+
self.is_valid() && other.is_valid(),
96+
"Cannot compare non-valid outputs"
97+
);
9498
assert!(
9599
!(self.metric.value().is_nan() || other.metric.value().is_nan()),
96100
"Appraisal metric cannot be NaN"
97101
);
98102
self.metric.compare(other.metric.as_ref())
99103
}
104+
105+
/// Whether this [`AppraisalOutput`] is a valid output.
106+
///
107+
/// Specifically, it checks whether the calculated capacity is greater than zero.
108+
pub fn is_valid(&self) -> bool {
109+
self.capacity.total_capacity() > Capacity(0.0)
110+
}
100111
}
101112

102113
/// Supertrait for appraisal metrics that can be serialised and compared.
@@ -355,9 +366,8 @@ fn compare_asset_fallback(asset1: &Asset, asset2: &Asset) -> Ordering {
355366
/// Assets with zero capacity are filtered out before sorting,
356367
/// as their metric would be `NaN` and could cause the program to panic. So the length
357368
/// of the returned vector may be less than the input.
358-
///
359369
pub fn sort_appraisal_outputs_by_investment_priority(outputs_for_opts: &mut Vec<AppraisalOutput>) {
360-
outputs_for_opts.retain(|output| output.capacity.total_capacity() > Capacity(0.0));
370+
outputs_for_opts.retain(AppraisalOutput::is_valid);
361371
outputs_for_opts.sort_by(|output1, output2| match output1.compare_metric(output2) {
362372
// If equal, we fall back on comparing asset properties
363373
Ordering::Equal => compare_asset_fallback(&output1.asset, &output2.asset),

0 commit comments

Comments
 (0)