1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Fixed-point decimal arithmetic with compile-time precision.
//!
//! `nexus-decimal` provides [`Decimal<B, DECIMALS>`] — a generic
//! fixed-point type parameterized by backing integer and decimal
//! places. Operations are `const fn` where possible, zero-allocation,
//! and designed for financial workloads.
//!
//! # Choosing Your Type
//!
//! Define aliases that match your domain:
//!
//! ```
//! use nexus_decimal::Decimal;
//!
//! type Price = Decimal<i64, 8>; // 8dp, range ±92B — traditional finance
//! type Quantity = Decimal<i64, 4>; // 4dp, range ±922T
//! type CryptoPrice = Decimal<i128, 12>; // 12dp, range ±39T — DeFi
//! type Usd = Decimal<i64, 2>; // 2dp cents
//! ```
//!
//! | Backing | Max Decimals | Max Range | Use case |
//! |---------|-------------|-----------|----------|
//! | `i32` | 9 | ±2.1B / SCALE | Embedded, space-constrained |
//! | `i64` | 18 | ±9.2e18 / SCALE | Traditional finance |
//! | `i128` | 38 | ±1.7e38 / SCALE | Cryptocurrency, DeFi |
//!
//! # Quick Start
//!
//! ```
//! use nexus_decimal::Decimal;
//! use core::str::FromStr;
//!
//! type D64 = Decimal<i64, 8>;
//!
//! let price = D64::from_str("123.45").unwrap();
//! let qty = D64::from_i32(10).unwrap();
//!
//! let notional = price * qty;
//! assert_eq!(notional.to_string(), "1234.5");
//!
//! let bid = D64::from_str("100.00").unwrap();
//! let ask = D64::from_str("100.50").unwrap();
//! let mid = bid.midpoint(ask);
//! assert_eq!(mid.to_string(), "100.25");
//! ```
//!
//! # Integer Conversions
//!
//! `From<IntType>` is implemented for primitive integer types whenever the
//! conversion is sound — i.e., `IntType::MAX * 10^DECIMALS` fits the backing.
//! Otherwise, `TryFrom<i64>` and `TryFrom<u64>` provide fallible conversions.
//! Smaller types that don't fit must be widened explicitly.
//!
//! ```
//! use nexus_decimal::Decimal;
//! type D64 = Decimal<i64, 8>;
//!
//! // Sound combinations: infallible.
//! let qty: D64 = 100_i32.into();
//! let count: D64 = 42_u16.into();
//!
//! // Unsound combinations: fallible.
//! let huge: Result<D64, _> = i64::MAX.try_into();
//! assert!(huge.is_err());
//! ```
//!
//! Use [`Decimal::from_scaled`] for tick-size construction:
//!
//! ```
//! use nexus_decimal::Decimal;
//! type D64 = Decimal<i64, 8>;
//!
//! let tick = D64::from_scaled(1, 5).unwrap(); // 0.00001
//! ```
//!
//! # Compile-Time Constants
//!
//! ```
//! use nexus_decimal::Decimal;
//!
//! type D64 = Decimal<i64, 8>;
//!
//! const PRICE: D64 = D64::new(100, 50_000_000); // 100.50
//! const FEE: D64 = D64::from_raw(500_000); // 0.005
//! const TOTAL: D64 = match PRICE.checked_add(FEE) {
//! Some(v) => v,
//! None => panic!("overflow"),
//! };
//! ```
//!
//! # Arithmetic Variants
//!
//! Every arithmetic operation comes in four flavors:
//!
//! | Variant | Returns | On overflow |
//! |---------|---------|-------------|
//! | `checked_*` | `Option<Self>` | `None` |
//! | `try_*` | `Result<Self, SpecificError>` | Typed error |
//! | `saturating_*` | `Self` | Clamps to `MIN`/`MAX` |
//! | `wrapping_*` | `Self` | Wraps around |
//!
//! Operators (`+`, `-`, `*`, `/`, `%`) always panic on overflow
//! in both debug and release builds.
//!
//! # Error Types
//!
//! Errors are scoped per operation — no catch-all enum:
//!
//! | Error | Used by | Variants |
//! |-------|---------|----------|
//! | [`OverflowError`] | `try_add`, `try_mul`, etc. | (unit struct) |
//! | [`DivError`] | `try_div` | `Overflow`, `DivisionByZero` |
//! | [`ParseError`] | `from_str_exact`, `FromStr` | `InvalidFormat`, `Overflow`, `PrecisionLoss` |
//! | [`ConvertError`] | `from_f64`, `TryFrom` | `Overflow`, `PrecisionLoss` |
//!
//! # Feature Flags
//!
//! | Feature | Dependencies | Provides |
//! |---------|-------------|----------|
//! | `std` (default) | — | `Error` trait impls |
//! | `serde` | `serde` | Serialize/Deserialize (string for JSON, raw for binary) |
//! | `num-traits` | `num-traits` | Zero, One, Num, Signed, Bounded, Checked*, ToPrimitive |
//!
//! # `no_std` Support
//!
//! Disable default features for `no_std`:
//! ```toml
//! nexus-decimal = { version = "0.1", default-features = false }
//! ```
//!
//! # Migration from fixdec
//!
//! ```ignore
//! // Before:
//! use fixdec::D64;
//!
//! // After:
//! use nexus_decimal::Decimal;
//! type D64 = Decimal<i64, 8>;
//! ```
//!
//! API differences:
//! - `mul_i64` / `mul_i128` → `mul_int` (takes the backing type)
//! - `DecimalError` → per-method error types ([`OverflowError`], [`DivError`], etc.)
//! - No predefined aliases — define your own (`type Price = Decimal<i64, 8>`)
//! - New: financial methods (`midpoint`, `spread`, `round_to_tick`, etc.)
extern crate std;
pub use Backing;
pub use Decimal;
pub use ;