stateset-primitives
Strongly-typed primitive types for commerce applications in Rust. Prevents entire classes of bugs at compile time by making it impossible to confuse an OrderId with a CustomerId, or to add Money in different currencies.
Features
- Newtype IDs:
OrderId,CustomerId,ProductId,ShipmentId,PaymentId,InventoryItemId,SubscriptionId,CartId, and 9 more — allCopy + Eq + Hash + Serialize + Display - Money: Amount + currency pair that prevents arithmetic across currencies
- CurrencyCode: Three-letter uppercase currency codes with zero-allocation storage (USD, EUR, GBP, ...)
- Sku: Validated product SKU — trimmed, non-empty, at most 128 characters
- Zero unsafe code,
#[deny(unsafe_code)]
Usage
use ;
use dec;
use Uuid;
// Type-safe IDs — can't accidentally swap them
let order_id: OrderId = new_v4.into;
let customer_id: CustomerId = new_v4.into;
// Money is currency-aware
let price = new;
// SKUs are validated and trimmed on construction
let sku = new.unwrap;
assert_eq!;
assert!;
Feature Flags
| Feature | Description | Default |
|---|---|---|
std |
Standard library support (the crate is no_std-capable without it) |
Yes |
arbitrary |
proptest::Arbitrary impls for property-based testing |
No |
sqlx-postgres |
sqlx Type/Encode/Decode impls, for PostgreSQL backends |
No |
rusqlite |
rusqlite conversions, for SQLite backends |
No |
Part of StateSet iCommerce
The foundation every other crate builds on —
stateset-core models,
stateset-db storage, and
stateset-pricing calculations all speak
these types. Part of the
StateSet iCommerce engine.
License
MIT OR Apache-2.0