Skip to main content

fin_primitives/
error.rs

1//! Error types for the fin-primitives crate.
2//!
3//! All errors are named, typed, and propagatable via `thiserror`.
4//! Every variant has at least one test that triggers it.
5
6use rust_decimal::Decimal;
7
8/// All errors that can occur in fin-primitives operations.
9#[derive(Debug, thiserror::Error)]
10pub enum FinError {
11    /// Symbol string was empty or contained whitespace.
12    #[error("Symbol '{0}' is invalid (empty or contains whitespace)")]
13    InvalidSymbol(String),
14
15    /// Price value was zero or negative.
16    #[error("Price must be positive, got {0}")]
17    InvalidPrice(Decimal),
18
19    /// Quantity value was negative.
20    #[error("Quantity must be non-negative, got {0}")]
21    InvalidQuantity(Decimal),
22
23    /// Order book delta arrived out of sequence.
24    #[error("Order book sequence mismatch: expected {expected}, got {got}")]
25    SequenceMismatch { expected: u64, got: u64 },
26
27    /// Not enough resting liquidity to fill the requested quantity.
28    #[error("No liquidity available for requested quantity {0}")]
29    InsufficientLiquidity(Decimal),
30
31    /// OHLCV bar failed internal invariant check (high >= low, etc.).
32    #[error("OHLCV bar invariant violated: {0}")]
33    BarInvariant(String),
34
35    /// A signal has not accumulated enough bars to produce a value.
36    #[error("Signal '{name}' not ready (requires {required} periods, have {have})")]
37    SignalNotReady {
38        name: String,
39        required: usize,
40        have: usize,
41    },
42
43    /// Position lookup failed for the given symbol.
44    #[error("Position not found for symbol '{0}'")]
45    PositionNotFound(String),
46
47    /// Ledger cash balance insufficient to cover the fill cost.
48    #[error("Insufficient funds: need {need}, have {have}")]
49    InsufficientFunds {
50        need: Decimal,
51        have: Decimal,
52    },
53
54    /// Timeframe duration was zero or negative.
55    #[error("Timeframe duration must be positive")]
56    InvalidTimeframe,
57
58    /// CSV or text parse failure.
59    #[error("CSV parse error: {0}")]
60    CsvParse(String),
61
62    /// A Decimal arithmetic operation overflowed.
63    #[error("Arithmetic overflow in financial calculation")]
64    ArithmeticOverflow,
65}