foundation tier

foundation-money

Immutable money type with decimal precision for financial calculations

Immutable money type with decimal precision for financial calculations

Tierfoundation
Roleunclassified (baselined)
Pathcrates/foundation/money
Edition2021
Targetsfoundation_money
Public items43 across 1 module
Tests51

What it is for

# foundation-money

Immutable money type with decimal precision for financial calculations.

Core Features

Example

use foundation_money::{Money, dec};
use rust_decimal::Decimal;

// Create money values
let price = Money::usd(dec!(19.99));
let quantity = Decimal::from(3);
let subtotal = price * quantity;  // $59.97

// Tax calculation with quantization
let tax_rate = dec!(0.0825);  // 8.25%
let tax = (subtotal * tax_rate).quantized();  // Rounds to $4.95

// Currency safety - this would return an error:
// let usd = Money::usd(dec!(100));
// let eur = Money::eur(dec!(100));
// let sum = (usd + eur)?;  // CurrencyMismatch error

Capabilities

Decimal

# foundation-money

Item
Decimal :: fn mul(self, money : Money) -> Self::Output

Money:Money

# foundation-money

Item
pub struct Money

Money:abs

# foundation-money

Item
Money :: fn abs(& self) -> Self

Money:add

# foundation-money

Item
Money :: fn add(self, other : Self) -> Self::Output

Money:amount

# foundation-money

Item
Money :: fn amount(& self) -> Decimal

Money:aud

# foundation-money

Item
Money :: fn aud(amount : Decimal) -> Self

Money:btc

# foundation-money

Item
Money :: fn btc(amount : Decimal) -> Self

Money:cad

# foundation-money

Item
Money :: fn cad(amount : Decimal) -> Self

Money:chf

# foundation-money

Item
Money :: fn chf(amount : Decimal) -> Self

Money:cny

# foundation-money

Item
Money :: fn cny(amount : Decimal) -> Self

Money:currency

# foundation-money

Item
Money :: fn currency(& self) -> & 'static str

Money:decimals

# foundation-money

Item
Money :: fn decimals(& self) -> u32

Money:deserialize

# foundation-money

Item
Money :: fn deserialize <D>(deserializer : D) -> Result <Self, D::Error> where D : Deserializer <'de>,

Money:eq

# foundation-money

Item
Money :: fn eq(& self, other : & Self) -> bool

Money:eur

# foundation-money

Item
Money :: fn eur(amount : Decimal) -> Self

Money:fmt

# foundation-money

Item
Money :: fn fmt(& self, f : & mut fmt::Formatter <'_>) -> fmt::Result

Money:gbp

# foundation-money

Item
Money :: fn gbp(amount : Decimal) -> Self

Money:hash

# foundation-money

Item
Money :: fn hash <H : Hasher>(& self, state : & mut H)

Money:is

# foundation-money

Item
Money :: fn is_positive(& self) -> bool
Money :: fn is_negative(& self) -> bool
Money :: fn is_zero(& self) -> bool

Money:jpy

# foundation-money

Item
Money :: fn jpy(amount : Decimal) -> Self

Money:krw

# foundation-money

Item
Money :: fn krw(amount : Decimal) -> Self

Money:mul

# foundation-money

Item
Money :: fn mul(self, scalar : Decimal) -> Self::Output
Money :: fn mul(self, scalar : i32) -> Self::Output
Money :: fn mul(self, scalar : i64) -> Self::Output

Money:mxn

# foundation-money

Item
Money :: fn mxn(amount : Decimal) -> Self

Money:neg

# foundation-money

Item
Money :: fn neg(self) -> Self::Output

Money:new

# foundation-money

Item
Money :: fn new(amount : Decimal, currency : & 'static str) -> Self

Money:quantized

# foundation-money

Item
Money :: fn quantized(& self) -> Self

Money:same

# foundation-money

Item
Money :: fn same_currency(& self, other : & Self) -> bool

Money:serialize

# foundation-money

Item
Money :: fn serialize <S>(& self, serializer : S) -> Result <S::Ok, S::Error> where S : Serializer,

Money:sub

# foundation-money

Item
Money :: fn sub(self, other : Self) -> Self::Output

Money:try

# foundation-money

Item
Money :: fn try_add(& self, other : & Self) -> Result <Self, MoneyError>
Money :: fn try_sub(& self, other : & Self) -> Result <Self, MoneyError>
Money :: fn try_cmp(& self, other : & Self) -> Result <Ordering, MoneyError>

Money:usd

# foundation-money

Item
Money :: fn usd(amount : Decimal) -> Self

Money:zero

# foundation-money

Item
Money :: fn zero(currency : & 'static str) -> Self

MoneyError

# foundation-money

Item
pub enum MoneyError
MoneyError :: fn currency_mismatch(operation : & 'static str, left : & str, right : & str) -> Self

i32

# foundation-money

Item
i32 :: fn mul(self, money : Money) -> Self::Output

i64

# foundation-money

Item
i64 :: fn mul(self, money : Money) -> Self::Output

currency_decimals (other)

_No module-level documentation is present in the source._

Item
fn get(currency : & str) -> u32

How to use it

From this crate's own rustdoc:

use foundation_money::{Money, dec};
use rust_decimal::Decimal;

// Create money values
let price = Money::usd(dec!(19.99));
let quantity = Decimal::from(3);
let subtotal = price * quantity;  // $59.97

// Tax calculation with quantization
let tax_rate = dec!(0.0825);  // 8.25%
let tax = (subtotal * tax_rate).quantized();  // Rounds to $4.95

// Currency safety - this would return an error:
// let usd = Money::usd(dec!(100));
// let eur = Money::eur(dec!(100));
// let sum = (usd + eur)?;  // CurrencyMismatch error

Module structure

foundation_money

Public surface

`crate root`

ItemWhat it is
pub enum MoneyErrorMoney-related errors.
MoneyError :: fn currency_mismatch(operation : & 'static str, left : & str, right : & str) -> SelfCreate a currency mismatch error.
pub struct MoneyAn immutable monetary amount with currency
Money :: fn serialize <S>(& self, serializer : S) -> Result <S::Ok, S::Error> where S : Serializer,
Money :: fn deserialize <D>(deserializer : D) -> Result <Self, D::Error> where D : Deserializer <'de>,
Money :: fn new(amount : Decimal, currency : & 'static str) -> SelfCreate a new Money value
Money :: fn usd(amount : Decimal) -> SelfCreate a Money value in USD.
Money :: fn eur(amount : Decimal) -> SelfCreate a Money value in EUR.
Money :: fn gbp(amount : Decimal) -> SelfCreate a Money value in GBP.
Money :: fn jpy(amount : Decimal) -> SelfCreate a Money value in JPY.
Money :: fn mxn(amount : Decimal) -> SelfCreate a Money value in MXN.
Money :: fn cad(amount : Decimal) -> SelfCreate a Money value in CAD.
Money :: fn aud(amount : Decimal) -> SelfCreate a Money value in AUD.
Money :: fn chf(amount : Decimal) -> SelfCreate a Money value in CHF.
Money :: fn cny(amount : Decimal) -> SelfCreate a Money value in CNY.
Money :: fn krw(amount : Decimal) -> SelfCreate a Money value in KRW.
Money :: fn btc(amount : Decimal) -> SelfCreate a Money value in BTC.
Money :: fn zero(currency : & 'static str) -> SelfCreate a zero Money value in the given currency.
Money :: fn amount(& self) -> DecimalGet the amount.
Money :: fn currency(& self) -> & 'static strGet the currency code.
Money :: fn decimals(& self) -> u32Get the number of decimal places for this currency.
Money :: fn is_positive(& self) -> boolCheck if the amount is positive (> 0).
Money :: fn is_negative(& self) -> boolCheck if the amount is negative (< 0).
Money :: fn is_zero(& self) -> boolCheck if the amount is zero.
Money :: fn abs(& self) -> SelfGet the absolute value.
Money :: fn quantized(& self) -> SelfRound to the currency's decimal places using banker's rounding
Money :: fn same_currency(& self, other : & Self) -> boolCheck if this money has the same currency as another.
Money :: fn try_add(& self, other : & Self) -> Result <Self, MoneyError>Try to add two Money values (requires matching currencies)
Money :: fn try_sub(& self, other : & Self) -> Result <Self, MoneyError>Try to subtract two Money values (requires matching currencies)
Money :: fn try_cmp(& self, other : & Self) -> Result <Ordering, MoneyError>Compare two Money values (requires matching currencies)
Money :: fn fmt(& self, f : & mut fmt::Formatter <'_>) -> fmt::Result
Money :: fn eq(& self, other : & Self) -> bool
Money :: fn hash <H : Hasher>(& self, state : & mut H)
Money :: fn add(self, other : Self) -> Self::Output
Money :: fn sub(self, other : Self) -> Self::Output
Money :: fn neg(self) -> Self::Output
Money :: fn mul(self, scalar : Decimal) -> Self::Output
Decimal :: fn mul(self, money : Money) -> Self::Output
Money :: fn mul(self, scalar : i32) -> Self::Output
i32 :: fn mul(self, money : Money) -> Self::Output
Money :: fn mul(self, scalar : i64) -> Self::Output
i64 :: fn mul(self, money : Money) -> Self::Output

`currency_decimals`

ItemWhat it is
fn get(currency : & str) -> u32Get the number of decimal places for a currency.

Re-exports. Exported here, defined elsewhere.

ExportDefined in
decrust_decimal_macros::dec

Boundary

Depends on no other workspace tier.

Shares tier foundation with 27 other crates: foundation-audit-log, foundation-basemodels, foundation-bounded-io, foundation-conversation-closure, foundation-crypto-sign, foundation-decisioning, foundation-encounter-vocabulary, foundation-fs-metadata, … (27 total).

_What this crate deliberately does NOT own is a judgment. No committed registry records one for it, so none is stated here._

Where it sits

Tier (ontology)foundation
Architectural role (taxonomy)unclassified (baselined)
Locationcrates/foundation/money
Vocabulary in force (lexicon)current

Dependencies

Runtime, from outside the workspace.

CrateRequirementFeaturesOptionalOnly on
rust_decimal^1.36serdenoalways
rust_decimal_macros^1noalways
serde^1derivenoalways
serde_json^1noalways
thiserror^2noalways

Development, from outside the workspace.

CrateRequirementFeaturesOptionalOnly on
tokio-test^0.4noalways

Build. None.

Depended on by. 12 workspace crates.

Signal flow — what reaches this crate, and what it reaches.

flowchart LR
  n_application_conversation["application-conversation"] -->|uses| SELF
  n_application_crm["application-crm"] -->|uses| SELF
  n_application_money["application-money"] -->|uses| SELF
  n_domain_billing["domain-billing"] -->|uses| SELF
  n_domain_catalog["domain-catalog"] -->|uses| SELF
  n_domain_crm["domain-crm"] -->|uses| SELF
  n_domain_ledger["domain-ledger"] -->|uses| SELF
  n_domain_legal_matter["domain-legal-matter"] -->|uses| SELF
  n_domain_pricing_rules["domain-pricing-rules"] -->|uses| SELF
  n_operations_control_plane["operations-control-plane"] -->|uses| SELF
  n_platform_privacy_scan_api["platform-privacy-scan-api"] -->|uses| SELF
  n_tools_corpus["tools-corpus"] -->|uses| SELF
  SELF["foundation-money"]
  classDef self fill:#1f883d,stroke:#1f883d,color:#fff;
  class SELF self;

Feature flags

FeatureEnablesOn by default
defaultyes
flowchart LR
  n_default["default"]

Targets

KindNameSource
libfoundation_money`src/lib.rs`

Error model

Error typeNamed by
MoneyErrordeclared, no public signature returns it

Operational characteristics

PropertyEvidence
async public surfacenone detected
async runtimenone detected
database accessnone detected
network I/Onone detected
unsafe codenone detected
environment variablesnone detected

No unsafe block, unsafe fn, unsafe impl or unsafe trait was found by the parser anywhere in this crate's source.

Configuration

No environment variable is read with a literal name anywhere in this crate. A variable whose key is computed at run time cannot be listed here, and is not claimed to be absent.

12 workspace crates depend on this one: application-conversation, application-crm, application-money, domain-billing, domain-catalog, domain-crm, domain-ledger, domain-legal-matter, domain-pricing-rules, operations-control-plane, platform-privacy-scan-api, tools-corpus.

Verification

KindCount
Unit tests51
Integration tests0
Examples0
Doctests1

Evidence by module. How often each public module is named by something executable.

ModuleTestsExamplesConsumers
crate root209
currency_decimals107

What the tests establish, by name:

Documentation coverage

MeasureDocumentedTotal
Public items with rustdoc2943
Public modules with a //! block01
pie showData
    title Public items with rustdoc
    "Documented" : 29
    "No rustdoc detected" : 14

Metrics

MetricValue
Rust source files1
Source lines1012
Code lines687
Public API items43
Public modules1
Tests51
Examples0
Cargo features1
Direct runtime dependencies5
Workspace reverse dependencies12
pie showData
    title Public API by kind
    "enum" : 1
    "function" : 1
    "method" : 40
    "struct" : 1
pie showData
    title Rust source composition
    "Code" : 687
    "Blank or comment" : 325

Generation

Rendered by tools-corpus corpus readme from repository evidence alone, renderer schema 2, lexicon current. No model, network service or database was consulted. Regenerate with tools-corpus corpus readme --write; verify with --check.

All foundation · Manual