Skip to main content

FinanceError

Enum FinanceError 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

NonFinite

A numeric field was NaN or infinite.

Fields

§field: &'static str
§value: f64
§

InvalidRate

Periodic rate outside the allowed domain for the formula (typically < -1.0 or <= -1.0).

Fields

§rate: f64
§

InvalidPeriod

Period index or count is out of range for the calculation.

Fields

§period: u32
§periods: u32
§message: &'static str
§

ZeroValue

A required money amount was zero (or subnormal) when a nonzero value is required.

Fields

§field: &'static str
§

SameSignValues

Present and future value have the same sign when opposite signs are required.

Fields

§present_value: f64
§future_value: f64
§

Unsolvable

Inputs make the equation unsolvable (e.g. zero periods with nonzero cash difference).

Fields

§message: &'static str
§

InvalidCashflow

Cashflow / payment constraint violated (sign, missing values, etc.).

Fields

§message: &'static str
§

EmptyInput

A required collection or series was empty.

Fields

§what: &'static str
§

LengthMismatch

Two series or slices that must align have different lengths.

Fields

§left: usize
§right: usize
§context: &'static str

Implementations§

Source§

impl FinanceError

Source

pub fn code(&self) -> &'static str

Stable snake_case code for logs and metrics (not localized).

Trait Implementations§

Source§

impl Clone for FinanceError

Source§

fn clone(&self) -> FinanceError

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FinanceError

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for FinanceError

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Error for FinanceError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for FinanceError

Source§

fn eq(&self, other: &FinanceError) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FinanceError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.