Financial Types
Core financial type definitions for trading systems in Rust.
Overview
financial_types is a lightweight Rust crate providing fundamental enums for
financial applications. These types are the building blocks used across trading
systems, options pricing libraries, and portfolio management tools.
All enums use #[repr(u8)] for compact memory layout (1 byte each) and include
serde serialization support out of the box.
Types
| Type | Variants | Description |
|---|---|---|
UnderlyingAssetType |
Crypto, Stock, Forex, Commodity, Bond, Other | Classification of asset classes |
Action |
Buy, Sell, Other | Trading actions |
Side |
Long, Short | Position directional exposure |
OptionStyle |
Call, Put | Option contract style |
Features
- Compact: All enums are
#[repr(u8)]— 1 byte each - Safe:
#[must_use]on all pure helper methods - Serde: Full serialization/deserialization support
- OpenAPI: Optional
utoipasupport via feature flag - Helpers:
is_*()andopposite()methods on applicable types - Parsing:
FromStr,TryFrom<&str>,TryFrom<u8>on every enum (case-insensitive string parsing, discriminant-basedu8conversion) no_std: Compiles withoutstd; onlyallocis required- Fuzzing: Optional
arbitraryandproptestfeatures generate random variants for property-based testing
Installation
Add this to your Cargo.toml:
[]
= "0.2"
To enable OpenAPI schema support:
[]
= { = "0.2", = ["utoipa"] }
Migration: 0.1 → 0.2
UnderlyingAssetType and Action are now #[non_exhaustive].
Exhaustive match expressions on either enum must include a wildcard
arm:
use Action;
let action = Buy;
match action
Side and OptionStyle remain exhaustive — no migration needed.
Quick Start
use ;
let action = Buy;
let side = Long;
let style = Call;
let asset = Stock;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Helper methods
assert!;
assert!;
assert!;
// Opposite helpers
assert_eq!;
assert_eq!;
API
UnderlyingAssetType
use UnderlyingAssetType;
let asset = Stock;
assert!;
assert!;
Helpers: is_stock(), is_crypto(), is_forex(), is_commodity(), is_bond()
Action
use Action;
let action = Buy;
assert!;
assert!;
Helpers: is_buy(), is_sell()
Side
use Side;
let side = Long;
assert!;
assert_eq!;
Helpers: is_long(), is_short(), opposite()
OptionStyle
use OptionStyle;
let style = Call;
assert!;
assert_eq!;
assert!; // Ord supported
Helpers: is_call(), is_put(), opposite()
Serialization
use Side;
let side = Long;
let json = to_string.unwrap; // "\"Long\""
let parsed: Side = from_str.unwrap;
assert_eq!;
Parsing
use ;
use FromStr;
// FromStr — case-insensitive, trims whitespace
assert_eq!;
assert_eq!;
assert_eq!;
// TryFrom<&str>
let asset: UnderlyingAssetType = "Stock".try_into.unwrap;
assert_eq!;
// TryFrom<u8> — uses #[repr(u8)] discriminants
assert_eq!;
assert!;
Minimum Supported Rust Version (MSRV)
financial_types requires Rust 1.85 or later (edition 2024).
A dedicated CI job builds and tests on the exact MSRV. Bumping the
MSRV is a minor version change — never sneaked into a patch.
Examples
Runnable examples live in examples/:
parse_action— parseActionfrom a CLI string.iterate_assets— iterate everyUnderlyingAssetTypeviaALL.serde_roundtrip— JSON round-trip every variant.utoipa_schema— dump OpenAPI schemas (requires--features utoipa).
Benchmarks
Hot-path criterion benchmarks live in benches/enums.rs
covering as_str, Display, FromStr, TryFrom<u8>, serde JSON
round-trip, and is_* helpers for every public enum.
HTML reports land under target/criterion/.
Changelog
See CHANGELOG.md for the full history of releases.
License
This project is licensed under the MIT License.
Contribution and Contact
We welcome contributions to this project! If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure that the project still builds and all tests pass.
- Commit your changes and push your branch to your forked repository.
- Submit a pull request to the main repository.
If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:
Contact Information
- Author: Joaquín Béjar García
- Email: jb@taunais.com
- Telegram: @joaquin_bejar
- Repository: https://github.com/joaquinbejar/financial_types
- Documentation: https://docs.rs/financial_types
We appreciate your interest and look forward to your contributions!
✍️ License
Licensed under MIT license