Expand description
Domain newtypes — zero-cost wrappers that make invalid values harder to pass by accident.
§Design (see rust_design_patterns Newtype)
- Private fields; construction is fallible (
TryFrom/ associatedtry_constructors). - Not type aliases:
Rateis not interchangeable with baref64at the type level. - Additive for 0.1.x / 0.2: existing free functions still take
f64/u32. Newtypes are for call sites and TA config that want compile-time clarity + one-time validation. - Extract with
.get()/Into<f64>when calling f64-based APIs.
§When to use
| Type | Prefer when |
|---|---|
Rate | You validated a rate once and pass it through several calls |
Periods | Period counts should not mix with money amounts |
PositivePrice | Equity prices / volumes that must be > 0 |
Money | Finite signed amounts (loans, payments) without unit claims |
PeriodLength | TA lookbacks (SMA(20), stoch k_period) — usize ≥ 1 |
§Examples
use finance_solution::{future_value, PositivePrice, Rate, Periods, FinanceResult};
fn grow(rate: Rate, n: Periods, pv: f64) -> FinanceResult<f64> {
future_value(rate.get(), n.get(), pv, false)
}
let r = Rate::tvm(0.05)?;
let n = Periods::new(10)?;
assert!(grow(r, n, -1_000.0).is_ok());
assert!(Rate::tvm(-1.5).is_err());
assert!(PositivePrice::new(0.0).is_err());Structs§
- Money
- Finite signed monetary amount (no currency unit — pure magnitude + sign).
- Period
Length - Positive lookback / window length for technical indicators (
usize ≥ 1). - Periods
- Count of compounding / payment periods (
u32). - Positive
Price - Strictly positive finite price (or similar quantity used as a price level).
- Rate
- Periodic or continuous interest / return rate as a decimal (e.g.
0.05= 5%).