simple_money 0.3.0

Down-to-earth money and currency implementation written in Rust.
Documentation
  • Coverage
  • 100%
    32 out of 32 items documented6 out of 22 items with examples
  • Size
  • Source code size: 37.34 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.49 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • afischl
simple_money-0.3.0 has been yanked.

Brief description

"Down-to-earth" (explained further below) money [^fowler] and currency implementation written in Rust.

See further below for more detailed design considerations. [^fowler]: Money consists of an amount and a Currency, as described by Martin Fowler in his Money EAA Pattern.

Usage

// You create a Currency either with a string code ...
let eur = Currency::new("EUR", 2)?;
// ... or a byte array ...
let kwt = Currency::new_using_bytes(b"KWD", 2)?;
// ... you need a byte array also to create a constant (compiler doesn't allow unwrap) ...
const EUR: Currency = Currency::new_using_bytes_const(b"EUR", 2);

// ... and use either to instantiate Money instances:
let money1 = Money { full_amount_as_minor: 12_95, currency: eur };
let money2 = Money { full_amount_as_minor: 15_99, currency: EUR };
let result =  money1 + money2;

Other implementations

Simple Money has similar implementations in other programming languages. There's a Simple Money project page on BitBucket, but I haven't published all of them.

Features and design considerations

Features in common with all implementations: see Design Considerations

Comparison to similar libraries

Here are two alternatives I've found:

  • steel-cent
    • uses an i64 for the amount, just like this implementation
    • has the same precision as the minor amount allows (no "half Cents")
    • its currency has the Copy trait and is as small as mine
    • the ISO currencies are provided, but IntelliJ's Rust plugin fails to see them (works in the terminal though)
  • rusty-money:
    • uses its own 128 representation for the amount
    • has more precision than available by the minor amount
    • its currency...
      • has lots of fields, ...
      • ... but the ISO currencies are provided ready to use
      • has the Copy trait, but is eighty bytes in size
    • is bristling with features (e.g. exchange rates?)