#[non_exhaustive]pub enum FinanceError {
NonFinite {
field: &'static str,
value: f64,
},
InvalidRate {
rate: f64,
},
InvalidPeriod {
period: u32,
periods: u32,
message: &'static str,
},
ZeroValue {
field: &'static str,
},
SameSignValues {
present_value: f64,
future_value: f64,
},
Unsolvable {
message: &'static str,
},
InvalidCashflow {
message: &'static str,
},
EmptyInput {
what: &'static str,
},
LengthMismatch {
left: usize,
right: usize,
context: &'static str,
},
}Expand description
Domain and input errors from finance calculations.
Marked non_exhaustive so new variants can appear in minor releases without
breaking downstream match expressions that include a wildcard arm.
§Examples
Pattern-match for recovery or user-facing messages:
use finance_solution::{payment, FinanceError};
match payment(-1.5, 36, 10_000.0, 0.0, false) {
Ok(fv) => println!("fv = {fv}"),
Err(FinanceError::InvalidRate { rate }) => {
assert!(rate < -1.0);
println!("bad rate: {rate} (code={})", FinanceError::InvalidRate { rate }.code());
}
Err(FinanceError::NonFinite { field, value }) => {
println!("{field} was non-finite ({value})");
}
Err(e) => println!("other finance error: {e}"),
}Propagate with ?:
use finance_solution::{present_value, future_value, FinanceResult};
fn round_trip(rate: f64, n: u32, fv: f64) -> FinanceResult<f64> {
let pv = present_value(rate, n, fv, false)?;
future_value(rate, n, pv, false)
}
let back = round_trip(0.04, 5, 10_000.0).unwrap();
assert!((back.abs() - 10_000.0).abs() < 1e-6);
assert!(round_trip(-2.0, 5, 10_000.0).is_err());Zero-value failure (present value of a zero future value is undefined):
use finance_solution::{present_value, FinanceError};
match present_value(0.05, 10, 0.0, false) {
Err(FinanceError::ZeroValue { field }) => assert_eq!(field, "future_value"),
other => panic!("expected ZeroValue, got {other:?}"),
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
NonFinite
A numeric field was NaN or infinite.
InvalidRate
Periodic rate outside the allowed domain for the formula (typically < -1.0 or <= -1.0).
InvalidPeriod
Period index or count is out of range for the calculation.
ZeroValue
A required money amount was zero (or subnormal) when a nonzero value is required.
SameSignValues
Present and future value have the same sign when opposite signs are required.
Unsolvable
Inputs make the equation unsolvable (e.g. zero periods with nonzero cash difference).
InvalidCashflow
Cashflow / payment constraint violated (sign, missing values, etc.).
EmptyInput
A required collection or series was empty.
LengthMismatch
Two series or slices that must align have different lengths.
Implementations§
Trait Implementations§
Source§impl Clone for FinanceError
impl Clone for FinanceError
Source§fn clone(&self) -> FinanceError
fn clone(&self) -> FinanceError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FinanceError
impl Debug for FinanceError
Source§impl Display for FinanceError
impl Display for FinanceError
Source§impl Error for FinanceError
impl Error for FinanceError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl PartialEq for FinanceError
impl PartialEq for FinanceError
impl StructuralPartialEq for FinanceError
Auto Trait Implementations§
impl Freeze for FinanceError
impl RefUnwindSafe for FinanceError
impl Send for FinanceError
impl Sync for FinanceError
impl Unpin for FinanceError
impl UnsafeUnpin for FinanceError
impl UnwindSafe for FinanceError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more